0x001 概述
这一章直接说git的命令行的简单使用姿势,对应的其实是Pro Git Book的第1-3章,这里只是做个记录,详细的还是撸官网比较实在。
0x002 环境搭建
$ mkdir git_test
$ cd git_test
0x003 初始化项目
$ git init
#输出
Initialized empty Git repository in /Users/FollowWinter/Desktop/git_test/.git/
这个指令会在当前目录下生成一个.git
隐藏目录,该目录保存了该项目的所有信息。
0x004 查看项目文件状态
$ git status
#输出
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
在git
中,文件只有两种状态,已跟踪和未跟踪,这时候我们刚刚初始化项目,并未有任何的操作,显示nothing to commit
。
添加一个文件并查看状态
$ echo "hello git" > README.md
$ git status
#输出
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
此时便提示有Untracked files
。
0x005 添加文件跟踪
该指令会将文件夹下的所有文件(除了被忽略的)加入文件跟踪,也就是只要被add
的文件,任何修改都会被git
跟踪,反之,没有被add
的任何修改都不会被跟踪。
$ git add README.me
$ git status
#输出
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
此时提示new file
修改这个文件
$ echo 'hello git again! ' > README.md $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: README.md
此时提示
modified
再次
add
$ git add README.md $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md
让显示信息简介一点
$ git status -s #输出 A README.md
A:新添加的文件
M:修改过的文件
??:未跟踪的文件
0x006 提交修改
$ git commit -m "添加README文件"
# 输出
[master (root-commit) 9519e09] 添加README文件
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git status
# 输出
On branch master
nothing to commit, working directory clean
```
这个指令用来提交本次修改。
所以,在git中,一个文件的状态变化大概是untracked->tracked->modified->staged
如果在提交之后,发现有一些文件忘记提交,可以使用--amend
,重新提交一次,此时记录中只会有一次提交
$ git add index.html
$ git commit --amend
所以,在git中,一个文件的状态变化大概是`untracked->tracked->modified->staged`
如果在提交之后,发现有一些文件忘记提交,可以使用`--amend`,重新提交一次,此时记录中只会有一次提交
```
$ git add index.html
$ git commit --amend
```
0x007 查看提交历史
$ echo "<p>hello git</p>" > index.html
$ git add index.html
$ git commit -m "修改首页"
#输出
[master 71855c1] 修改首页
1 file changed, 1 insertion(+)
$ git log
#输出
commit 71855c112b7420d9c6a39e91b03adb12d4924491
Author: FollowWinter <lyxxxx@yeah.net>
Date: Sat Nov 18 22:24:36 2017 +0800
修改首页
commit e1dcd6a8e364e2bef226bad1388d54f92206b7d9
Author: FollowWinter <lyxxxx@yeah.net>
Date: Sat Nov 18 22:15:16 2017 +0800
添加README文件
0x007 使用远程仓库
之前的所有操作都是在本地操作,现在使用远程仓库来玩玩,这里使用github
,还有许多仓库可以用,比如国内的码云等。
我们现在github
上新建项目:https://github.com/followWint…
接着使用git remote add <shortname> <url>
来添加一个远程仓库
$ git remote add origin https://github.com/followWinter/git_test
$ git remote
# 输出
origin
$ git remote -v
origin https://github.com/followWinter/git_test (fetch)
origin https://github.com/followWinter/git_test (push)
0x008 克隆远程项目
当然我们也可以先在github
上新建一个git_test
项目,然后使用git clone
来拉一个项目,这样就不需要git init
和git remote add
了
$ git https://github.com/followWinter/git_test
#输出
Cloning into 'git_test'...
remote: Counting objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
Unpacking objects: 100% (15/15), done.
Checking connectivity... done.
0x009 推送到远程分支
可以使用git push [remote-name] [branch-name]
,将当前项目代码推送到远程仓库,这样我们在其他地方就可以直接使用git clone
来继续开发了。
$ git push origin master
Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 580 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/followWinter/git_test
+ 132d89c...71855c1 master -> master (forced update)
0x010 打标签
打标签并查看当前项目的标签
$ git tag -a v1.0.1 -m "首页发布" $ git tag -a v1.1.1 -m "修改首页" $ git tag # 输出 v1.0.1 v1.1.1
查看某个标签具体修改
$ git show v1.1.1 # 输出 tag v1.1.1 Tagger: FollowWinter <lyxxxx@yeah.net> Date: Sat Nov 18 22:39:15 2017 +0800 修改首页 commit 71855c112b7420d9c6a39e91b03adb12d4924491 Author: FollowWinter <lyxxxx@yeah.net> Date: Sat Nov 18 22:24:36 2017 +0800 修改首页 diff --git a/index.html b/index.html index e69de29..18b0a77 100644 --- a/index.html +++ b/index.html @@ -0,0 +1 @@ +<p>hello git</p>
推送标签到仓库
$ git push origin v1.0.1 # 输出 Counting objects: 1, done. Writing objects: 100% (1/1), 178 bytes | 0 bytes/s, done. Total 1 (delta 0), reused 0 (delta 0) To https://github.com/followWinter/git_test
推送所有标签到仓库
$ git push origin --tags
# 输出
Counting objects: 1, done.
Writing objects: 100% (1/1), 177 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/followWinter/git_test
* [new tag] v1.1.1 -> v1.1.1
```
0x012 分支
创建一个分支
$ git branch dev $ git branch # 输出 dev * master
切换分支
$ git checkout dev
#输出
Switched to branch 'dev'
```
创建并切换到一个新的分支
```
$ git checkout -b dev-v2
#输出
Switched to a new branch 'dev-v2'
$ git branch
#输出
dev
* dev-v2
master
```
0x013 分支合并
$ echo "update a new feature " > index.html
$ git add index.html
$ git commit -a -m "添加一个新功能"
#输出
[dev-v2 4430bbd] 添加一个新功能
1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
#输出
Switched to branch 'master'
$ git merge dev-v2
#输出
Updating 71855c1..4430bbd
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git push origin master
#输出
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/followWinter/git_test
71855c1..4430bbd master -> master
0x014 拉取远程分支
当我们在两台电脑工作的时候,就会出现,本地和远程的代码不符合,这时候就必须从远程仓库拉取新的代码
```
$ git fetch # 或者使用`git pull`
#输出
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 0), reused 8 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), done.
From https://github.com/followWinter/git_test
* [new branch] feature1 -> origin/feature1
```