常见版本控制器
集中式版本控制器
- CSV/SVN
- 速度慢,必须联⽹
- 只有⼀个中央数据仓库,如果中央数据仓库挂了或者⽆法访问,所有的使⽤者⽆法使⽤SVN,⽆法进⾏提交或备份操作
分布式版本控制器
- Git
- 无中央服务器,每个人的电脑都是一个完整的版本库
- 安全性能更高
- 通常有一台充当“中央服务器”的电脑,仅仅作为方便“交换”大家的修改
安装使用
1 2 3 4 5
| git config git config --global user.name "kinmfer" git config --global user.email "kinmfer@foxmail.com" git config --global color.ui true git config --list
|
1 2 3 4
| git init
git init [project-name]
|
概念介绍

工作区
暂存区
常规使用
⼀般来说,⽇常使⽤只需要记住下图6个命令。但是熟练使⽤,恐怕要记住60-100个命令。

创建并初始化版本库
1 2 3 4
| [root@node1 ~] ah Initialized empty Git repository in /root/firstgit/.git/ . .. .git
|
把文件添加到仓库中
1 2 3 4 5 6
| [root@node1 firstgit] [root@node1 firstgit] [root@node1 firstgit] [master (root‐commit) e380d2c] write a readme.txt file 1 file changed, 1 insertion(+) create mode 100644 readme.txt
|
修改文件内容,查询状态并提交
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
| [root@node1 firstgit] [root@node1 firstgit]
no changes added to commit (use "git add" and/or "git commit ‐a") [root@node1 firstgit] diff ‐‐git a/readme.txt b/readme.txt index aa982b7..8d0e700 100644 ‐‐‐ a/readme.txt +++ b/readme.txt @@ ‐1 +1 @@ ‐hello world\n hello git +hello eagleslab [root@node1 firstgit] [root@node1 firstgit]
[root@node1 firstgit] [master 7b92afb] change messages 1 file changed, 1 insertion(+), 1 deletion(‐) [root@node1 firstgit]
nothing to commit, working directory clean
|
查询历史记录并退回
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
| [root@node1 firstgit] commit 3efa7f0fc058f00b0a98171fb77b44ab31481c87 Author: Cokeku <1154283293@qq.com> Date: Mon Mar 18 22:42:38 2019 ‐0400 add zj commit 7b92afbb9f118f7ae10c49abc3114e4ee91ed26a Author: Cokeku <1154283293@qq.com> Date: Mon Mar 18 22:37:05 2019 ‐0400 change messages commit e380d2cc936e01e17065408000c42c21f114321a Author: Cokeku <1154283293@qq.com> Date: Mon Mar 18 22:31:25 2019 ‐0400 write a readme.txt file [root@node1 firstgit] HEAD is now at e380d2c write a readme.txt file [root@node1 firstgit] hello world [root@node1 firstgit] HEAD is now at 3efa7f0 add zj [root@node1 firstgit] hello eagleslab zhengjiang! 记录每⼀次的命令: [root@node1 firstgit] 3efa7f0 HEAD@{0}: reset: moving to 3efa7f0fc058f00b0a98171fb77b44ab31481c87 e380d2c HEAD@{1}: reset: moving to e380d2cc936e01e17065408000c42c21f114321a 3efa7f0 HEAD@{2}: commit: add zj 7b92afb HEAD@{3}: commit: change messages e380d2c HEAD@{4}: commit (initial): write a readme.txt file
|
命令清单
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
git config --global color.ui true git config --global color.status auto git config --global color.diff auto git config --global color.branch auto git config --global color.interactive auto git config --global --unset http.proxy
|
增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
$ git rm [file1] [file2] ...
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
|
代码提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| $ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit -am 'message'
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
分支

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 38 39 40 41 42 43 44 45 46 47 48 49 50
| $ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
$ git checkout v2.0
$ git checkout -b devel origin/develop
git checkout -- README
|
标签
- 标签也是指向了⼀次commit提交,是⼀个⾥程碑式的标签,回滚打标签直接加标签号,不需要加唯⼀
字符串。
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
| git tag -a v1.0 -m " version 1.0"
git tag -a v2.0 [哈希值] -m "version 2.0"
git tag
git show v1.0
git tag -d v1.010
git reset --hard v2.0
$ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
|
查看信息
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| $ git status
$ git log11
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]12
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
|
远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force13
$ git push [remote] --all
|
撤销
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 checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
其他
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| git init git config --global user.name "xxx" git config --global user.email "xxx@xxx.com" git config --global color.ui true git config --global color.status auto git config --global color.diff auto git config --global color.branch auto git config --global color.interactive auto git config --global --unset http.proxy git clone git+ssh://git@192.168.53.168/VT.git git status git add xyz git add . git commit -m 'xxx' git commit --amend -m 'xxx' git commit -am 'xxx' git rm xxx git rm -r * git log git log -1 git log -515 git log --stat git log -p -m git show dfb02e6e4f2f7b573337763e5c0013802e392818 git show dfb02 git show HEAD git show HEAD^ git tag git tag -a v2.0 -m 'xxx' git show v2.0 git log v2.0 git diff git diff --cached git diff HEAD^ git diff HEAD -- ./lib git diff origin/master..master git diff origin/master..master --stat git remote add origin git+ssh://git@192.168.53.168/VT.git git branch git branch --contains 50089 git branch -a git branch -r git branch --merged git branch --no-merged git branch -m master master_copy git checkout -b master_copy git checkout -b master master_copy git checkout features/performance git checkout --track hotfixes/BJVEP933 git checkout v2.0 git checkout -b devel origin/develop git checkout -- README git merge origin/master git cherry-pick ff44785404a8e git push origin master git push origin :hotfixes/BJVEP933 git push --tags git fetch git fetch --prune git pull origin master git mv README README2 git reset --hard HEAD git rebase git branch -d hotfixes/BJVEP933 git branch -D hotfixes/BJVEP933 git ls-files git show-branch git show-branch --all git whatchanged git revert dfb02e6e4f2f7b573337763e5c0013802e392818 git ls-tree HEAD git rev-parse v2.0 git reflog git show HEAD@{5} git show master@{yesterday} git log --pretty=format:'%h %s' --graph git show HEAD~3 git show -s --pretty=raw 2be7fcb476 git stash git stash list git stash show -p stash@{0} git stash apply stash@{0} git grep "delete from" git grep -e '#define' --and -e SORT_DIRENT18 git gc git fsck
$ git archive
|