- Linux – 打开控制台,然后通过包管理安装,在Ubuntu上命令是:
|
1
|
sudo apt-get install git-all
|
- Windows – 推荐使用git for windows,它包括了图形工具以及命令行模拟器。
- OS X – 最简单的方式是使用homebrew安装,命令行执行
|
1
|
brew install git
|
|
1
2
|
$ git config --global user.name "My Name"
$ git config --global user.email myEmail@example.com
|
|
1
2
|
$ cd Desktop/git_exercise/
$ git init
|
|
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
|
|
1
|
$ git add hello.txt
|
|
1
|
$ git add -A
|
|
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
|
|
1
|
$ git commit -m "Initial commit."
|
|
1
|
$ git remote add origin https://github.com/tutorialzine/awesome-project.git
|
|
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
|
|
1
|
$ git clone https://github.com/tutorialzine/awesome-project.git
|
|
1
2
3
4
|
$ git pull origin master
From https://github.com/tutorialzine/awesome-project
* branch master -> FETCH_HEAD
Already up-to-date.
|
- 稳定版本的代码不会被破坏
- 不同的功能可以由不同开发者同时开发。
- 开发者可以专注于自己的分支,不用担心被其他人破坏了环境
- 在不确定之前,同一个特性可以拥有几个版本,便于比较
|
1
|
$ git branch amazing_new_feature
|
|
1
2
3
|
$ git branch
amazing_new_feature
* master
|
|
1
|
$ git checkout amazing_new_feature
|
|
1
2
|
$ git add feature.txt
$ git commit -m "New feature complete."
|
|
1
|
$ git checkout master
|
|
1
|
$ git merge amazing_new_feature
|
|
1
|
$ git branch -d amazing_new_feature
|
|
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
|
|
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?
|
|
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?
|
|
1
|
$ git checkout 09bd8cc1 hello.txt
|
|
1
|
$ git revert HEAD
|
|
1
|
$ git revert b10cc123
|
|
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.
|
|
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.
|
|
1
2
3
|
// Not using for loop or forEach.
// Use Array.toString() to console.log contents.
console.log(arr.toString());
|
|
1
2
|
$ git add -A
$ git commit -m "Array printing conflict resolved."
|
- 在项目根目录创建.gitignore文件
- 在文件中列出不需要提交的文件名,文件夹名,每个一行
- .gitignore文件需要提交,就像普通文件一样
- log文件
- task runner builds
- node_modules等文件夹
- IDEs生成的文件
- 个人笔记
|
1
2
3
4
5
|
*.log
build/
node_modules/
.idea/
my_notes.txt
|