Git备忘
常用命令
- 将分支以远程分支为准:
git checkout -B master origin/master
其实是以远程分支为准reset本地分支
- 查看远程仓库:
git remote -v
- 查看本地分支:
git branch -v
- 推送:
git push [remote-name] [branch-name]
- 取消文件的修改:
git checkout - - a.rb
- 取消本地所有更改:
git checkout .
- 取消Add之后的文件
git reset HEAD file
- 删除远程分支
git push origin --delete 分支名
- 推送一个空分支,相当于删除
git push origin :分支名
- 设置commit时默认的editor:
git config core.editor = vim
- 将amend的commit回退:
git reset --soft HEAD@{1}
HEAD@{1} gives you "the commit that HEAD pointed at before
it was moved to where it currently points at".
- 拉下来远程的tag列表
git pull origin --tags
- 删除多个分支:
git branch | grep 'KAP*' | xargs git branch -D
- 要443端口clone代码
ssh://git@ssh.github.com:443/yourname/reponame.git
- 取消merge
git merge --abort
- 更好看的git log
git log --graph --pretty=oneline
- 取git的最新tag
git tag | xargs -I@ git log --format=format:"%ci %h @%n" -1 @ | sort | tail -1 | awk {'print $5'}
搭建一个git服务器
在服务器安装git 加一个git用户:sudo adduser git 用git用户登陆服务器,在home目录新建.ssh目录,里面新建authorized_keys文件 在需要登陆的用户的机器运行ssh-keygen,生成id_rsa和id_rsa.pub,把id_rsa.pub的内容拷贝到上面的authorized_keys里面,一个用户占一行 在git用户的home目录,新建项目 sudo git init --bare sample.git 在客户端,clone下来 git clone git@[server]:sample.git
[server]为服务器IP或/etc/hosts文件里面映射的域名
然后在客户端这边加代码,推送上去
合并多个commit为一个
找到最下面一个commit hash值,
git rebase -i hash
除了第一个,其他的都改成squash或s