配置
配置全局用户:
1 2 3 4 5
| $ git config --global user.name "Your Name" $ git config --global user.email "email@example.com" $ git config --global color.ui true $ git config --global core.editor vim $ git config -l
|
用户的 git 配置文件路径 ~/.gitconfig
项目的 git 配置文件路径 project/.git/config
项目配置会覆盖全局配置
配置本地(当前repository)用户:
1 2
| $ git config --local user.name "Your Name" $ git config --local user.email "email@example.com"
|
删除全局用户配置:
1 2
| $ git config --global --unset user.name $ git config --global --unset user.email
|
删除本地用户配置:
1 2
| $ git config --local --unset user.name $ git config --local --unset user.email
|
创建版本库
1 2 3 4
| $ mkdir learngit $ cd learngit $ pwd /Users/admin/learngit
|
初始化版本库与提交
1 2 3 4 5 6 7 8
| $ git init Initialized empty Git repository in /Users/admin/learngit/.git/ $ git add <file> $ git add . $ git commit -m "wrote a readme file" [master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt
|
常用操作
查看、添加、提交、删除、找回,重置修改文件
查看文件
1 2 3 4 5 6 7 8 9 10
| $ git show $ git show $id $ 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
| $ git log $ git log <file> $ git log -p <file> $ git log -p -2 $ git 本地分支管理
|
查看、切换、创建和删除分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ git branch $ git branch -r $ git branch <new_branch> $ git branch -v $ git branch --merged $ git branch --no-merged $ git branch -d <branch> $ git branch -D <branch> $ git checkout <branch> $ git checkout -b <new_branch> $ git checkout -b <new_branch> <branch> $ git checkout $id $ git checkout $id -b <new_branch> $ git checkout -- <file> $ git checkout .
|
分支合并和rebase
1 2 3
| $ git merge <branch> $ git merge origin/master --no-ff $ git rebase master <branch>
|
git暂存管理
1 2 3 4
| $ git stash $ git stash list $ git stash apply $ git stash drop
|
git远程分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ git pull $ git pull --no-ff $ git fetch origin $ git merge origin/master $ git checkout --track origin/branch $ git checkout -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> $ git rm <file> $ git rm -rf <file> $ git rm <file> --cached $ git reset <file> $ git reset -- . $ git reset --hard $ git reset --hard HEAD^
|
git远程仓库管理
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
| $ git remote -v $ git remote show origin $ git remote add origin git@github.com:whuhacker/Unblock-Youku-Firefox.git $ git remote set-url origin git@github.com:whuhacker/Unblock-Youku-Firefox.git $ git remote rm <repository> $ git commit <file> $ git commit . $ git commit -a $ git commit -am "some comments" $ git commit --amend $ git revert <$id> $ git revert HEAD $ git status $ git log $ git log --pretty=oneline $ git reflog $ git merge dev $ git merge --no-ff -m "merge with no-ff" dev $ ssh-keygen -t rsa -C "youremail@example.com" $ git remote add origin git@github.com:opstrip/opstrip.github.io.git $ git remote set-url origin git@github.com:opstrip/opstrip.github.io.git $ git push -u origin master $ git clone git@github.com:opstrip/opstrip.github.io.git $ git push origin branch-name $ git pull
|
设置跟踪远程库和本地库
1 2
| $ git branch --set-upstream master origin/master $ git branch --set-upstream develop origin/develop
|
- 如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name。