Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。 Git 是 Linus Torvalds 为了帮助管理 Linux 内核 开发而开发的一个开放源码的版本控制软件。
Git跟踪并管理的是修改,而非文件。
二、git的安装(linux)
命令安装:
yum install git git-svn git-email git-gui gitk
安装完成后,还需要最后一步设置,在命令行输入:
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。
三、创建版本库
(1)创建目录
- [root@www admin]# mkdir KEY
- [root@www admin]# cd KEY
- [root@www KEY]# pwd
- /home/admin/KEY
(2)通过git init
命令把这个目录变成Git可以管理的仓库:
- [root@www KEY]# git init
- Initialized empty Git repository in /home/admin/KEY/.git
当前目录下多了一个.git
的目录,这个目录是Git来跟踪管理版本库的
- [root@www KEY]# ls -a
- . .. .git
四、把文件添加到仓库中
版本库(Repository):
工作区有一个隐藏目录.git
,这个不算工作区,而是Git的版本库。版本库里面包含一个暂存区,创建的第一个分支master,指向master的指针head。
第一步是用git add
把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。
- [root@www KEY]# vim test.c
- [root@www KEY]# git add test.c
- [root@www KEY]# git commit -m "add a file"
- [master (root-commit) 2ff3615] add a file
- 1 files changed, 6 insertions(+), 0 deletions(-)
- create mode 100644 test.c
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("hello world\n");
- return 0;
- }
- [root@www KEY]# vim test.c
- [root@www KEY]# git add test.c
- [root@www KEY]# git status
- # On branch master
- # Changes to be committed:
- # (use "git reset HEAD <file>..." to unstage)
- #
- # modified: test.c
- #
- [root@www KEY]# vim LICENSE
- [root@www KEY]# git add LICENSE
- [root@www KEY]# git status
- # On branch master
- # Changes to be committed:
- # (use "git reset HEAD <file>..." to unstage)
- #
- # new file: LICENSE
- # modified: test.c
- #
- [root@www KEY]# git commit -m "modify 2 file"
- [master 7050e08] modify 2 file
- 2 files changed, 2 insertions(+), 1 deletions(-)
- create mode 100644 LICENSE
- [root@www KEY]# git status
- # On branch master
- nothing to commit (working directory clean)
git commit
命令,-m
后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
五、版本回退
(1)用
git log
查看修改内容
- [root@www KEY]# git log
- commit 87dfe8d2260bce1ad3c95d2bcab79662792346b3
- Author: youngyoungla <634713275@qq.com>
- Date: Sat Jun 11 00:37:15 2016 -0700
-
- modify
-
- commit 7050e08c5656a53f6f85389be61a50b1d999fd63
- Author: youngyoungla <634713275@qq.com>
- Date: Sat Jun 11 00:31:07 2016 -0700
-
- modify 2 file
-
- commit 2ff3615299e183d970e94315fa2467bd605c51eb
- Author: youngyoungla <634713275@qq.com>
- Date: Sat Jun 11 00:19:00 2016 -0700
-
- add a file
如果嫌麻烦,可以加上
--pretty=oneline
- [root@www KEY]# git log --pretty=oneline
- 87dfe8d2260bce1ad3c95d2bcab79662792346b3 modify
- 7050e08c5656a53f6f85389be61a50b1d999fd63 modify 2 file
- 2ff3615299e183d970e94315fa2467bd605c51eb add a file
前面那一串是commit id,每提交一个新版本,实际上Git就会把它们自动串成一条时间线.
(2)版本回退
首先,Git必须知道当前版本是哪个版本,在Git中,用
HEAD
表示当前版本,也就是最新的提 交
3628164...882e1e0
(注意我的提交ID和你的肯定不一样),
上一个版本就是HEAD^
,上上一个版本就 是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
回退到上个版本:
1.
git reset --hard HEAD^
命令
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- printf("I Love You\n");
- return 0;
- }
- [root@www KEY]# git reset --hard HEAD^
- HEAD is now at 7050e08 modify 2 file
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- <span style="white-space:pre"> </span>printf("we are young\n");
- <span style="white-space:pre"> </span>return 0;
- }
2.
使用commit id回退
- [root@www KEY]# git reset --hard 87dfe8d2260bce1ad3c95d2bcab79662792346b3
- HEAD is now at 87dfe8d modify
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- printf("I Love You\n");
- return 0;
- }
(3)Git提供了一个命令git reflog
用来记录你的每一次命令:
- [root@www KEY]# git reflog
- 87dfe8d HEAD@{0}: 87dfe8d2260bce1ad3c95d2bcab79662792346b3: updating HEAD
- 7050e08 HEAD@{1}: HEAD^: updating HEAD
- 87dfe8d HEAD@{2}: commit: modify
- 7050e08 HEAD@{3}: commit: modify 2 file
- 2ff3615 HEAD@{4}: commit (initial): add a file</span>
-
- </span>
六、撤销修改
(1)命令
git checkout -- test.c
意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。 总之,就是让这个文件回到最近一次git commit或git add时的状态。
(2)git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。
七、删除文件
(1)在工作区上删除文件 rm test.c
- <span style="font-size:18px;color:#666666;">[root@www KEY]# rm LICENSE
- rm:是否删除普通文件 "LICENSE"?y
- [root@www KEY]# ls
- test.c</span>
这时只是工作区上删除了文件,版本库上并没有删除,如果删错了,可以
git checkout -- test.c
还原。
- [root@www KEY]# git checkout -- LICENSE
- [root@www KEY]# ls
- LICENSE test.c
- [root@www KEY]# cat LICENSE
- lllalala
(2)从版本库上删除
- [root@www KEY]# git rm LICENSE
- rm 'LICENSE'
- [root@www KEY]# ls
- test.c
- [root@www KEY]# git checkout -- LICENSE
- error: pathspec 'LICENSE' did not match any file(s) known to git.
- [root@www KEY]# ls</span>
- test.c
这时版本库上的文件已经被删除了,不能进行使用git checkout -- LICENSE还原,可以使用
git reset --hard LICENSE
回退版本。git checkout
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
- [root@www KEY]# git reset --hard HEAD^
- HEAD is now at 7050e08 modify 2 file
- [root@www KEY]# ls
- LICENSE test.c
八、远程仓库
(1)创建SSH KEY
ssh-keygen -t rsa -C "youremail@example.com"
如果一切顺利的话,可以在用户主目录里找到.ssh
目录,里面有id_rsa
和id_rsa.pub
两个文件,这两个就是SSH Key的秘钥对,id_rsa
是私钥,不能泄露出去,id_rsa.pub
是公钥,可以放心地告诉任何人。
第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub
文件的内容:
点“Add Key”,你就应该看到已经添加的Key:
(2)添加远程库
首先,登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库KEY,
在本地的KEY仓库下运行命令:
[root@www KEY]# git remote add origin git@github.com:youngyoungla/KEY.git
将本地库传送到远程库上:
- [root@www KEY]# git push -u origin master
- Counting objects: 7, done.
- Compressing objects: 100% (5/5), done.
- Writing objects: 100% (7/7), 599 bytes, done.
- Total 7 (delta 0), reused 0 (delta 0)
- To git@github.com:youngyoungla/KEY.git
- * [new branch] master -> master
- Branch master set up to track remote branch master from origin.
把本地库的内容推送到远程,用git push
命令,实际上是把当前分支master
推送到远程。
由于远程库是空的,我们第一次推送master
分支时,加上了-u
参数,Git不但会把本地的master
分支内容推送的远程新的master
分支,还会把本地的master
分支和远程的master
分支关联起来,在以后的推送或者拉取时就可以简化命令。
推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样;
(3)远程库克隆
首先,登陆GitHub,创建一个新的仓库,名字叫gitskills1
我们勾选Initialize this repository with a README
,这样GitHub会自动为我们创建一个README.md
文件。创建完毕后,可以看到README.md
文件;
远程库克隆:
- [root@www KEY]# git clone git@github.com:youngyoungla/gitskills1.git
- Initialized empty Git repository in /home/admin/KEY/gitskills1/.git/
- Warning: Permanently added the RSA host key for IP address '192.30.252.121' to the list of known hosts.
- remote: Counting objects: 3, done.
- remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
- Receiving objects: 100% (3/3), done.
- [root@www KEY]# ls
- gitskills1 LICENSE test.c
九、分支管理
(1)创建分支 git checkout -b dev
每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。
- [root@www KEY]# git checkout -b dev
- Switched to a new branch 'dev'
- [root@www KEY]# git branch
- * dev
- master
- [root@www KEY]# vim test.c
- [root@www KEY]# git add test.c
- [root@www KEY]# git commit -m "modify"
- [dev 86a10c8] modify
- 1 files changed, 1 insertions(+), 0 deletions(-)
- [root@www KEY]# git checkout master
- Switched to branch 'master'
- [root@www KEY]# git merge dev
- Updating 7050e08..86a10c8
- Fast-forward
- test.c | 1 +
- 1 files changed, 1 insertions(+), 0 deletions(-)
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- printf("I love you\n");
- return 0;
- }
- [root@www KEY]# git branch -d dev
- Deleted branch dev (was 86a10c8).
(2)分支冲突
- [root@www KEY]# git checkout -b feature1
- Switched to a new branch 'feature1'
- [root@www KEY]# vim test.c
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- return 0;
- }
- [root@www KEY]# git add test.c
- [root@www KEY]# git commit -m "modify"
- [feature1 341a39d] modify
- 1 files changed, 0 insertions(+), 1 deletions(-)
- [root@www KEY]# git checkout master
- Switched to branch 'master'
- Your branch is ahead of 'origin/master' by 1 commit.
- [root@www KEY]# vim test.c
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- printf("I love you\n");
- printff("love you");
- return 0;
- }
- [root@www KEY]# git add test.c
- [root@www KEY]# git commit -m "modify"
- [master 8de8474] modify
- 1 files changed, 1 insertions(+), 0 deletions(-)
- [root@www KEY]# git merge feature1
- Auto-merging test.c
- CONFLICT (content): Merge conflict in test.c
- Automatic merge failed; fix conflicts and then commit the result.
- [root@www KEY]# git status
- # On branch master
- # Your branch is ahead of 'origin/master' by 2 commits.
- #
- # Unmerged paths:
- # (use "git add/rm <file>..." as appropriate to mark resolution)
- #
- # both modified: test.c
- #
- # Untracked files:
- # (use "git add <file>..." to include in what will be committed)
- #
- # gitskills1/
- no changes added to commit (use "git add" and/or "git commit -a")
- [root@www KEY]# cat test.c
- #include<stdio.h>
- int main()
- {
- printf("we are young\n");
- <<<<<<< HEAD
- printf("I love you\n");
- printff("love you");
- =======
- >>>>>>> feature1
- return 0;
- }
- [root@www KEY]# git add test.c
- [root@www KEY]# git commit -m "conflict fixed"
- [master aa9d39c] conflict fixed
- [root@www KEY]# git merge feature1
- Already up-to-date.
- [root@www KEY]# git log --graph --pretty=oneline --abbrev-commit
- * aa9d39c conflict fixed
- |\
- | * 341a39d modify
- * | 8de8474 modify
- |/
- * 86a10c8 modify
- * 7050e08 modify 2 file
- * 2ff3615 add a file
- [root@www KEY]# git branch -d feature1
- Deleted branch feature1 (was 341a39d).
十、解决本地仓库与远程仓库的冲突
(1)
git fetch:
取回远程主机某个分支的更新,再与本地的指定分支合并
- [root@localhost BinaryTree]# git fetch origin master
- [root@localhost BinaryTree]# git log -p master..origin/master
- [root@localhost BinaryTree]# git merge origin/master
或者这样更清晰
git fetch origin master:tmp
git diff tmp
git merge tmp
(2)git pull
git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。
$ git pull <远程主机名> <远程分支名>:<本地分支名>
比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。
$ git pull origin next:master
如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
$ git pull origin next
上面命令表示,取回origin/next分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。
$ git fetch origin
$ git merge origin/next
在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。
Git也允许手动建立追踪关系。
git branch --set-upstream master origin/next
上面命令指定master分支追踪origin/next分支。
如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名。
$ git pull origin
上面命令表示,本地的当前分支自动与对应的origin主机”追踪分支”(remote-tracking branch)进行合并。
如果当前分支只有一个追踪分支,连远程主机名都可以省略。
git pull命令表示,当前分支自动与唯一一个追踪分支进行合并。
如果合并需要采用rebase模式,可以使用–rebase选项。
$ git pull --rebase <远程主机名> <远程分支名>:<本地分支名>