Git 常用命令
配置
1 2 3 4 5 6 7 8 9 10 11 12
| git config --global user.name "Vitan" git config --global user.email "ivitan95@gmail.com" ssh-keygen -t rsa -C "ivitan95@gmail.com" git config --global color.ui true git config --global alias.co checkout git config --global alias.ci commit git config --global alias.st status git config --global alias.br branch git config --global core.editor "vim"
git config --global core.quotepath false
|
常用命令
查看、添加、提交、删除、找回,重置修改文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| git co -- <file>
git co .
git add <file>
git add .
git rm <file>
git rm <file> --cached
git reset <file>
git reset -- .
git reset --hard
git ci <file> git ci . git ci -a
git ci -am "some comments" git ci --amend
git revert <$id>
git revert HEAD
|
查看文件 diff
1 2 3 4 5 6 7 8 9 10 11 12 13
| git diff <file>
git diff git diff <$id1> <$id2>
git diff <branch1>..<branch2>
git diff --staged
git diff --cached
git diff --stat
|
查看提交记录
1 2 3 4 5 6 7
| git log git log <file>
git log -p <file>
git log -p -2
|
- Mac 上可以使用 tig 代替 diff 和 log,brew install tig
分支管理
本地分支管理
查看、切换、创建和删除分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| git br -r
git br <new_branch>
git br -v
git br --merged
git br --no-merged
git co <branch>
git co -b <new_branch>
git co -b <new_branch> <branch>
git co $id
git co $id -b <new_branch>
git br -d <branch>
git br -D <branch>
|
分支合并和 rebase
1 2 3 4 5 6 7 8 9
| git merge <branch>
git merge origin/master --no-ff
git rebase master <branch>
git co <branch> && git rebase master && git co master && git merge <branch>
|
补丁管理 (方便在多台机器上开发同步时用)
1 2 3
| git diff > ../sync.patch # 生成补丁 git apply ../sync.patch # 打补丁 git apply --check ../sync.patch # 测试补丁能否成功
|
暂存管理
1 2 3 4 5
| git stash # 暂存 git stash list # 列所有stash git stash apply # 恢复暂存的内容 git stash drop # 删除暂存区 git stash clear
|
远程分支管理
基础命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| git pull
git pull --no-ff
git fetch origin
git merge origin/master
git co --track origin/branch
git co -b <local_branch> origin/<remote_branch>
git push
git push origin master
git push -u origin master
git push origin <local_branch>
git push origin <local_branch>:<remote_branch>
git push origin :<remote_branch>
|
远程仓库管理
1 2 3 4 5 6 7 8
| git remote -v
git remote show origin
git remote add origin git@github:stormzhang/demo.git
git remote set-url origin git@github.com:stormzhang/demo.git
|
创建远程仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git clone --bare robbin_site robbin_site.git
scp -r my_project.git git@git.csdn.net:~
mkdir robbin_site.git && cd robbin_site.git && git --bare init
git remote add origin git@github.com:robbin/robbin_site.git
git push -u origin master
git push -u origin develop
git remote set-head origin master
|
1 2
| git branch --set-upstream master origin/master git branch --set-upstream develop origin/develop
|
Via