git 如今的火爆程度非同通常,它被普遍地用在大型开源项目,团队开发,以及独立开发者,甚至学生之中。node
初学者很是容易被各类命令,参数吓哭。但实际上刚上手你并不须要了解全部命令的用途。你能够从掌握一些简单,强大的命令开始,逐步去学习。(这就是这篇文章要讲的)。好了,上来!linux
git命令是一些命令行工具的集合,它能够用来跟踪,记录文件的变更。好比你能够进行保存,比对,分析,合并等等。这个过程被称之为版本控制。已经有一系列的版本控制系统,好比SVN, Mercurial, Perforce, CVS, Bitkeepe等等。git
Git是分布式的,这意味着它并不依赖于中心服务器,任何一台机器均可以有一个本地版本的控制系统,咱们称之为仓库。若是是多人协做的话,你须要还须要一个线上仓库,用来同步信息。这就是GitHub, BitBucket的工做。github
安装git很是直接:windows
Shell浏览器
1服务器 |
sudo apt-get install git-all分布式 |
Shellide
1工具 |
brew install git |
若是你是在是先用图形工具的话,那么推荐你使用Github desktop,Sourcetree。但我仍是推荐你使用命令行,下面的内容就都是命令行的。
安装完git,首要任务是配置咱们的信息,最重要的是用户名及邮箱,打开终端,执行如下命令。
Shell
1 2 |
$ git config --global user.name "My Name" $ git config --global user.email myEmail@example.com |
配置好这两项,用户就能知道谁作了什么,而且一切都更有组织性了不是吗?
git 会把全部文件以及历史记录保存在你的项目中,建立一个新的仓库,首先要去到项目路径,执行 git init。而后git会建立一个隐藏的文件夹.git,全部的信息都储存在其中。
在桌面建立一个联系文件夹 git_exercise, 打开终端:
Shell
1 2 |
$ cd Desktop/git_exercise/ $ git init |
OK,如今项目还什么都没有,新建一个 hello.txt 文件试试~
git status 是另外一个很是重要的命令,它会告诉咱们创库的当前状态:是否为最新代码,有什么更新等等执行git status:
Shell
1 2 3 4 5 6 7 8 9 10 |
$ git status
On branch master
Initial commit
Untracked files: (use "git add ..." to include in what will be committed)
hello.txt |
git 告诉咱们,hello.txt还没有跟踪,这是由于这个文件是新的,git不知道是应该跟踪它的变更呢,仍是直接忽略无论呢。为了跟踪咱们的新文件,咱们须要暂存它。
git 有个概念叫 暂存区,你能够把它当作一块空白帆布,包裹着全部你可能会提交的变更。它一开始为空,你能够经过 git add 命令添加内容,并使用 git commit 提交。
这个例子中只有一个文件:
Shell
1 |
$ git add hello.txt |
若是须要提交目录下的全部内容,能够这样:
Shell
1 |
$ git add -A |
再次使用git status查看:
Shell
1 2 3 4 5 6 7 8 9 10 |
$ git status
On branch master
Initial commit
Changes to be committed: (use "git rm --cached ..." to unstage)
new file: hello.txt |
咱们的文件已经提交了。状态信息还会告诉咱们暂存区文件发生了什么变更,不过这里咱们提交的是一个全新文件。
一次提交表明着咱们的仓库到了一个交付状态,一般是完成了某一块小功能。它就像是一个快照,容许咱们像使用时光机同样回到旧时光。
建立提交,须要咱们提交东西到暂存区(git add),而后:
Shell
1 |
$ git commit -m "Initial commit." |
这就建立了一次提交,-m “Initial commit.”表示对此次提交的描述,建议使用有意义的描述性信息。
到目前为止,咱们的操做都是在本地的,它存在于.git文件中。为了可以协同开发,咱们须要把代码发布到远端仓库上。
为了可以上传到远端仓库,咱们须要先创建起连接,这篇教程中,远端仓库的地址为:https://github.com/tutorialzine/awesome-project,但你应该本身在Github, BitBucket上搭建仓库,本身一步一步尝试。 添加测试用的远端仓库
Shell
1 |
$ git remote add origin https://github.com/tutorialzine/awesome-project.git |
一个项目能够同时拥有好几个远端仓库为了可以区分,一般会起不一样的名字。一般主远端仓库被称为origin。
每次咱们要提交代码到服务器上时,都会使用到git push。
git push命令会有两个参数,远端仓库的名字,以及分支的名字:
Shell
1 2 3 4 5 6 7 |
$ git push origin master
Counting objects: 3, done. Writing objects: 100% (3/3), 212 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/tutorialzine/awesome-project.git * [new branch] master -> master |
取决于你使用的服务器,push过程你可能须要验证身份。若是没有出差错,如今使用浏览器去你的远端分支上看,hello.txt已经在那里等着你了。
放在Github上的开源项目,人们能够看到你的代码。可使用 git clone进行下载到本地。
Shell
1 |
$ git clone https://github.com/tutorialzine/awesome-project.git |
本地也会建立一个新的仓库,并自动将github上的分支设为远端分支。
若是你更新了代码到仓库上,其余人能够经过git pull命令拉取你的变更:
Shell
1 2 3 4 |
$ git pull origin master From https://github.com/tutorialzine/awesome-project * branch master -> FETCH_HEAD Already up-to-date. |
由于暂时没有其余人提交,全部没有任何变更
当你在作一个新功能的时候,最好是在一个独立的区域上开发,一般称之为分支。分支之间相互独立,而且拥有本身的历史记录。这样作的缘由是:
每个仓库的默认分支都叫master, 建立新分支能够这样:
Shell
1 |
$ git branch amazing_new_feature |
建立了一个名为amazing_new_feature的新分支,它跟当前分支同一块儿点
单独使用git branch,能够查看分支状态:
Shell
1 2 3 |
$ git branch amazing_new_feature * master |
*
号表示当前活跃分支为master,使用git checkout切换分支。
Shell
1 |
$ git checkout amazing_new_feature |
咱们的 amazing_new_feature 分支的任务是增长一个featuer.txt。咱们来建立,添加到暂存区,提交。
Shell
1 2 |
$ git add feature.txt $ git commit -m "New feature complete." |
新分支任务完成了,回到master分支
Shell
1 |
$ git checkout master |
如今去查看文件,你会发现,以前建立的feature.txt文件不见了,由于master分支上并无feature.txt。使用git merge 把 amazing_new_feature 分支合并到master上。
Shell
1 |
$ git merge amazing_new_feature |
ok! 而后再把amazing_new_feature 分支删掉吧。
Shell
1 |
$ git branch -d amazing_new_feature |
这篇文章的最后一节,咱们来讲些比较高级而且使用的技巧。
每次提交都有一个惟一id,查看全部提交和他们的id,可使用 git log:
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$ git log
commit ba25c0ff30e1b2f0259157b42b9f8f5d174d80d7 Author: Tutorialzine Date: Mon May 30 17:15:28 2016 +0300
New feature complete
commit b10cc1238e355c02a044ef9f9860811ff605c9b4 Author: Tutorialzine Date: Mon May 30 16:30:04 2016 +0300
Added content to hello.txt
commit 09bd8cc171d7084e78e4d118a2346b7487dca059 Author: Tutorialzine Date: Sat May 28 17:52:14 2016 +0300
Initial commit |
id 很长,可是你并不须要复制整个字符串,前一小部分就够了。
查看某一次提交更新了什么,使用 git show:
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ git show b10cc123
commit b10cc1238e355c02a044ef9f9860811ff605c9b4 Author: Tutorialzine Date: Mon May 30 16:30:04 2016 +0300
Added content to hello.txt
diff --git a/hello.txt b/hello.txt index e69de29..b546a21 100644 --- a/hello.txt +++ b/hello.txt -0,0 +1 +Nice weather today, isn't it? |
查看两次提交的不一样,可使用git diff [commit-from]..[commit-to] 语法:
Shell
1 2 3 4 5 6 7 8 9 10 11 |
$ git diff 09bd8cc..ba25c0ff
diff --git a/feature.txt b/feature.txt new file mode 100644 index 0000000..e69de29 diff --git a/hello.txt b/hello.txt index e69de29..b546a21 100644 --- a/hello.txt +++ b/hello.txt -0,0 +1 +Nice weather today, isn't it? |
比较首次提交和最后一次提交,咱们能够看到全部的更改。固然使用git difftool命令更加方便。
git 容许咱们将某个特定的文件回滚到特定的提交,使用的也是 git checkout。
下面的例子,咱们将hello.txt回滚到最初的状态,须要指定回滚到哪一个提交,以及文件的全路径。
Shell
1 |
$ git checkout 09bd8cc1 hello.txt |
若是你发现最新的一次提交完了加某个文件,你能够经过 git commit —amend来修复,它会把最新的提交打回暂存区,并尝试从新提交。
若是是更复杂的状况,好比不是最新的提交了。那你可使用git revert。
最新的一次提交别名也叫HEAD。
Shell
1 |
$ git revert HEAD |
其余提交可使用id:
Shell
1 |
$ git revert b10cc123 |
混滚提交时,发生冲突是很是频繁的。当文件被后面的提交修改了之后,git不能正确回滚。
冲突常常出如今合并分支或者是拉去别人的代码。有些时候git能自动处理冲突,但大部分须要咱们手动处理。
好比John 和 Tim 分别在各自的分支上写了两部分代码。
John 喜欢 for:
// Use a for loop to console.log contents.
for(var i=0; i console.log(arr[i]);
}
Tim 喜欢 forEach:
// Use forEach to console.log contents.
arr.forEach(function(item) {
console.log(item);
});
假设John 如今去拉取 Tim的代码:
Shell
1 2 3 4 5 |
$ git merge tim_branch
Auto-merging print_array.js CONFLICT (content): Merge conflict in print_array.js Automatic merge failed; fix conflicts and then commit the result. |
这时候git并不知道如何解决冲突,由于他不知道John和Tim谁写得更好。
因而它就在代码中插入标记。
Shell
1 2 3 4 5 6 7 8 9 10 11 |
HEAD // Use a for loop to console.log contents. for(var i=0; iarr.length; i++) { console.log(arr[i]); } ======= // Use forEach to console.log contents. arr.forEach(function(item) { console.log(item); }); >>>>>>> Tim s commit. |
==== 号上方是当前最新一次提交,下方是冲突的代码。咱们须要解决这样的冲突,通过组委会成员讨论,一致认定,在座的各位都是垃圾!两个都不要。改为下面的代码。
Shell
1 2 3 |
// Not using for loop or forEach. // Use Array.toString() to console.log contents. console.log(arr.toString()); |
好了,再提交一下:
Shell
1 2 |
$ git add -A $ git commit -m "Array printing conflict resolved." |
若是在大型项目中,这个过程可能容易出问题。你可使用GUI 工具来帮助你。使用 git mergetool。
大部分项目中,会有写文件,文件夹是咱们不想提交的。为了防止一不当心提交,咱们须要gitignore文件:
一般会被ignore的文件有:
例如:
Shell
1 2 3 4 5 |
*.log build/ node_modules/ .idea/ my_notes.txt |
教程结束~(撒花)
git有点复杂,而且有一大堆特性和技巧等着你去挖掘,这篇文章只是提供冰山一角,但愿你不要由于太多繁琐的命令而停下前进的脚步! 怀挺!
文章转载:http://blog.jobbole.com/102957/