
Git是一个分布式版本控制/软件配置管理软件,原来是linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为了更好地管理linux内核开发而创立的。
##Git配置
1 2 3 4 5 6 7 8
| git config --global user.name "yourname" git config --global user.email "youemail@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 -l # 列举所有配置
|
用户的git配置文件在~/.gitconfig
,我的配置:
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 35 36 37
| cat .gitconfig [user] email = workwsl@163.com name = flow [color] ui = auto [color "branch"] current = yellow reverse local = yellow remote = green [color "diff"] meta = yellow bold frag = magenta bold old = red bold new = green bold [color "status"] added = yellow changed = green untracked = cyan [alias] st = "status" co = checkout ls = "ls-files" ci = commit br = branch rt = reset --hard unstage = reset HEAD uncommit = reset --soft HEAD^ l = log --pretty=oneline --abbrev-commit --graph --decorate amend = commit --amend who = shortlog -n -s --no-merges g = grep -n --color -E cp = cherry-pick -x cb = checkout -b [core] filemode = true autocrlf = false
|
##Git常用命令
查看、帮助命令
1 2 3
| git help <command> git show git show $id
|
查看提交记录
1 2 3 4 5 6 7 8 9 10
| git log git log <file> git log -p <file> git log -p -2 git log --stat git log --since="6 hours" git log --before="2 days" git log -1 HEAD~3 git log -1 HEAD^^^ git reflog
|
添加、提交、删除、找回,重置修改文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| git co -- <file> # 抛弃工作区修改 git co . # 抛弃工作区修改 git co HEAD <file> # 抛弃工作目录区中文件的修改 git add <file> # 将工作文件修改提交到本地暂存区 git add . # 将所有修改过的工作文件提交暂存区 git rm <file> # 从版本库中删除文件 git rm <file> --cached # 从版本库中删除文件,但不删除文件 git reset <file> # 从暂存区恢复到工作文件 git reset -- . # 从暂存区恢复到工作文件 git reset --hard HEAD^ # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改 git reset --hard <commit id> # 恢复到某一次提交的状态 git reset HEAD <file> # 抛弃暂存区中文件的修改 git ci <file> git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做 git ci -am "some comments" git ci --amend # 修改最后一次提交记录 git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建了一次提交对象 git revert HEAD # 恢复最后一次提交的状态
|
查看文件diff
1 2 3 4 5 6 7
| git diff <file> git diff git diff <$id1> <$id2> git diff <branch1>..<branch2> git diff --staged git diff --cached git diff --stat
|
##Git 本地分支管理
查看、切换、创建和删除分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git br -r git br -v git br -a git br --merged git br --no-merged git br <new_branch> git br <new_branch> <start_point> git br -f <existing_branch> <start_point> git br -d <branch> git br -D <branch> git co <branch> git co -b <new_branch> git co -b <new_branch> <branch> git co -m <existing_branch> <new_branch> git co -M <existing_branch> <new_branch> git co $id git co $id -b <new_branch>
|
分支合并和rebase
1 2 3 4 5 6 7
| git merge <branch> git merge origin/master --no-ff git merge --no-commit <branch> git merge --squash <branch> git rebase master <branch> git co <branch> && git rebase master && git co master && git merge <branch>
|
Git补丁管理
1 2 3 4
| git diff > ../sync.patch # 生成补丁 git apply ../sync.patch # 打补丁 git apply --check ../sync.patch # 测试补丁能否成功 git format-patch -X # 根据提交的log生成patch,X为数字,表示最近的几个日志
|
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
| git pull git pull --no-ff git fetch origin git fetch origin remote-branch:local-branch git fetch origin --tags git checkout -b <new-branch> <remote_tag> 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>
|
Git远程仓库管理
1 2 3 4 5 6 7 8 9
| git remote -v git remote show origin git remote add origin git@github:XXX/test.git git remote set-url origin git@github.com:XXX/test.git git remote rm <repository> git remote set-head origin master git branch --set-upstream master origin/master git branch --set-upstream develop origin/develop
|
##实例
打patch过程
add .1 2 3
| git status git diff --cached >XXX.patch git ci -m 'add patch'
|
来源