git版本管理软件——git日常管理代码

日常管理代码为添加代码阶段,包括添加文件,修改文件的管理。在这章的命令是用git管理代码的过程中大量反复使用到的命令。
git开发最常用到的步骤:

git status——git diff——git add——git commit——git reset——git push——git log

git查看当前工作区改动文件 git status

命令:git status

该命令是查看当前工作区状态所改动的文件若没有改动,会提示当前工作区干净。该命令可以显示未提交的文件,通常以红色显示,也显示提交后在暂存区的文件,通常以绿色显示。

工作区内未有改动

ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
无文件要提交,干净的工作区
ghost@ghost-machine:~/workspace/test/Project_test$

工作区内有改动

ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     reset.txt

尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     readme.txt

ghost@ghost-machine:~/workspace/test/Project_test$

查看代码修改 git diff

该命令的功能是查看代码的变动。具体是现在正在修改的这份代码与最近一次提交的版本库的代码做比较,将不同之处罗列出来。

git diff后面可以写具体的文件名或文件夹,改动是当前工作状态下的改动与上一次commit的状态做对比,具体为
ghost@ghost-machine:~/workspace/test/Project_test$ git diff
diff --git a/readme.txt b/readme.txt
index 3550d6f..696e814 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,3 @@
-add 20160614.
+diff 20161106.
+
+test git diff
ghost@ghost-machine:~/workspace/test/Project_test$

红色-为原有代码的状态
绿色+为当前改动的状态

所表达的意思为:
对于文件readme.txt的改动,

删除了
-add 20160614.
添加了
+diff 20161106.
+
+test git diff

回车换行也算在内。

git提交代码 git add、git commit

git add < file | folder | -A | --all >

该命令的功能是将代码的改动提交到git的暂存区,暂时将修改好的代码保存起来,但修改的代码还没有加到本地库中。

用命令git add 提交文件到暂存区,具体使用方法

用法:git add [<options>] [--] <pathspec>...

    -n, --dry-run         	演习
    -v, --verbose         	冗长输出

    -i, --interactive     	交互式拣选
    -p, --patch           	交互式挑选数据块
    -e, --edit            	编辑当前差异并应用
    -f, --force           	允许添加忽略的文件
    -u, --update          	更新已跟踪的文件
    -N, --intent-to-add   	只记录,该路径稍后再添加
    -A, --all             	添加所有改变的已跟踪文件和未跟踪文件
    --ignore-removal      	忽略工作区中移除的路径(和 --no-all 相同)
    --refresh             不添加,只刷新索引
    --ignore-errors       	跳过因出错不能添加的文件
    --ignore-missing      	检查在演习模式下文件(即使不存在)是否被忽略

经常用到的git add 使用说明

git add file 		添加某一个文件到暂存区;
git add folder 	    添加某个文件夹里面的内容到暂存区;
git add -A | --all  添加git 目录下所有文件到暂存区;
git add . 		    添加当前目录下的所有文件到暂存区;

代码提交之后通过命令git status来查看提交的状态。

添加新文件状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   test.c

添加文件改动状态

git status
warning: LF will be replaced by CRLF in test/test.c.
The file will have its original line endings in your working directory.
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.c

git commit -m “commit message”

该命令的功能是将修改好的代码,即将暂存区的代码添加到本地库中。并加以标注。
用命令git commit 来给提交的内容做注释,具体使用方法有很多,但最常见的一种使用方法为
git commit -m “提交内容的注释说明”
$ git commit -m "add test.c."
[master c4aa087] add test.c.
warning: LF will be replaced by CRLF in test/test.c.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644test/test.c

git commit –amend

该命令为追加提交,在对应文件列出最后提交的commit注释,然后再在后面追加注释,这样就不用从新写前面的注释来解释本次提交。多用于对已上库的代码修复bug

对一个文件的追加,在使用git commit –amend命令时系统会弹出一个文本框要求修改和添加注释。

ghost@ghost-machine:~/workspace/git_test$ vim readme.txt 
ghost@ghost-machine:~/workspace/git_test$ git add .
ghost@ghost-machine:~/workspace/git_test$ git commit --amend
[master 84fd0df] test git push
 Author: zx <zx@git.com>
 Date: Sat Dec 3 11:48:12 2016 +0800
 2 files changed, 5 insertions(+)
ghost@ghost-machine:~/workspace/git_test$ git log
commit 84fd0df24d200c655541f90b9502e9e69ae2d1ff
Author: zx <zx@git.com>
Date:   Sat Dec 3 11:48:12 2016 +0800

    test git push
    
    test git commit --amend

追加多个文件

ghost@ghost-machine:~/workspace/git_test$ vim readme.txt 
ghost@ghost-machine:~/workspace/git_test$ ls
patch.txt  readme.txt  reset.txt  v1.1_test.txt
ghost@ghost-machine:~/workspace/git_test$ vim reset.txt 
ghost@ghost-machine:~/workspace/git_test$ git add .
ghost@ghost-machine:~/workspace/git_test$ git commit --amend
[master 7929f3c] test git push
 Author: zx <zx@git.com>
 Date: Sat Dec 3 11:48:12 2016 +0800
 2 files changed, 7 insertions(+)
	
	
commit 7929f3ccd9431db0c7782310e7760000be836615
Author: zx <zx@git.com>
Date:   Sat Dec 3 11:48:12 2016 +0800

    test git push
    
    test git commit --amend
    
    test git commit --amend for two file

git撤销git reset git checkout

git reset

1、撤销git add
该命令git reset相对于git add 而言,是git add 的相反动作,在输入了命令git add filename 后,发现提交还需要修改,或者不需要提交这个文件,就选择git reset撤销这个提交,git reset filename,具体使用方法为:

ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	reset.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
ghost@ghost-machine:~/workspace/test/Project_test$ git add .
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   reset.txt

ghost@ghost-machine:~/workspace/test/Project_test$ git reset reset.txt
ghost@ghost-machine:~/workspace/test/Project_test$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	reset.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
ghost@ghost-machine:~/workspace/test/Project_test$

2、撤销git commit

该命令git reset –hard 相对于git commit而言,将命令回退到该commit提交后的动作,包括上一次commit的改动和添加的文件全都回到指定的commit位置,指定的commit由哈希值来区分
commit 546d73a5842ecd4df8dc35994bc520b22c12fcb3
Author: zx <zx@git.com>
Date:   Sun Nov 6 09:26:29 2016 +0800

    add reset.txt

commit 70456a99ee3899c40d4846b7101646855b7a1219
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:55:17 2016 +0800

    add 20160614.

commit 824005a2c174d195c5823b2a412ce2c3081c39f3
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:53:50 2016 +0800

    add 20160614.

具体用法为:
加 --hard为具体引用

ghost@ghost-machine:~/workspace/test/Project_test$ git log
commit 546d73a5842ecd4df8dc35994bc520b22c12fcb3
Author: zx <zx@git.com>
Date:   Sun Nov 6 09:26:29 2016 +0800

    add reset.txt

commit 70456a99ee3899c40d4846b7101646855b7a1219
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:55:17 2016 +0800

    add 20160614.

commit 824005a2c174d195c5823b2a412ce2c3081c39f3
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:53:50 2016 +0800

    add 20160614.
ghost@ghost-machine:~/workspace/test/Project_test$ git reset --hard 70456a99ee3899c40d4846b7101646855b7a1219
HEAD 现在位于 70456a9 add 20160614.
ghost@ghost-machine:~/workspace/test/Project_test$ git log
commit 70456a99ee3899c40d4846b7101646855b7a1219
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:55:17 2016 +0800

    add 20160614.

commit 824005a2c174d195c5823b2a412ce2c3081c39f3
Author: zx <zx@git.com>
Date:   Tue Jun 14 22:53:50 2016 +0800

    add 20160614.
ghost@ghost-machine:~/workspace/test/Project_test$

该命令可以简化成git reset --hard HEAD^
HEAD^为最近的上一次提交的commit。

git reset撤销相对于git而言,不是真正的撤销,而是将当前进度指针移到指定的commit位置而已。

##git checkout
将git checkout单单归于撤销并不合适,因为git checkout的真正意义为“检出”。
1、git checkout filename

git checkout filename的意义为将该文件在版本库里面检出,功能类似于还原,那就不是简单的撤销问题了,对该文件filename的修改不满意,想恢复文件,就用git checkout filename,即放弃修改,丢弃文件,将该文件上一次保存在版本库中的状态检出,也就是恢复。
ghost@ghost-machine:~/workspace/test/Project_test$ git diff
diff --git a/reset.txt b/reset.txt
index 7389186..3013816 100644
--- a/reset.txt
+++ b/reset.txt
@@ -1 +1,3 @@
 add reset
+
+test git checkout
ghost@ghost-machine:~/workspace/test/Project_test$ git checkout reset.txt
ghost@ghost-machine:~/workspace/test/Project_test$ cat reset.txt 
add reset
ghost@ghost-machine:~/workspace/test/Project_test$

checkout在分支中的作用也有切换分支的功能,在主分支master和其他分支中切换。

git checkout commit

与git reset相比,git checkout不是撤销commit的改动而是跳出master分支,新建一个新的分支并且在该分支的改动不会影响主分支。
ghost@ghost-machine:~/workspace/test/Project_test$ git checkout 70456a99ee3899c40d4846b7101646855b7a1219
Note: checking out '70456a99ee3899c40d4846b7101646855b7a1219'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD 目前位于 70456a9... add 20160614.
ghost@ghost-machine:~/workspace/test/Project_test$
ghost@ghost-machine:~/workspace/test/Project_test$ git branch
* (HEAD detached from 70456a9)
  master
ghost@ghost-machine:~/workspace/test/Project_test$

对于git reset来说,git checkout更安全,因为不影响主分支master,而是新建一个bug修复分支,在该分支修改好后,还可以将该分支的修改合并到主分支上面。该分支名字可以修改,具体的分支操作在后面git分支管理会详细介绍。在图形界面中可以看到分支情况,该分支不稳定,切换回master之后会自动消失,必须要为这个分支命名,命令为:git checkout -b new_branch_name。
这里写图片描述

git推送代码git push,与同步代码git pull

git push

git push将git目录下提交注释好的代码推送到本地仓库或远程仓库里面。

不带参数的git push默认推送到master主分支。

推送到分支命令为

git push origin branch1

推送到项目1仓库的分支1的命令为

git push project1 branch1

不带参数的git push使用方法

ghost@ghost-machine:~/workspace/test/Project_test$ git push
对象计数中: 4, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (4/4), 431 bytes | 0 bytes/s, 完成.
Total 4 (delta 0), reused 0 (delta 0)
To /home/ghost/warehouse/Project_test.git/
   84cb30d..5d65d1e  master -> master
ghost@ghost-machine:~/workspace/test/Project_test$

git pull

git pull 是将本地仓库或远程仓库的代码同步到git目录下。
不带参数的git pull默认将同步原始仓库的master主分支

同步分支命令为

git pull origin branch1

不带参数的git pull使用方法

ghost@ghost-machine:~/workspace/test/ghost_project/Project_test$ git pull
remote: 对象计数中: 14, 完成.
remote: 压缩对象中: 100% (12/12), 完成.
remote: Total 14 (delta 5), reused 0 (delta 0)
展开对象中: 100% (14/14), 完成.
来自 /home/ghost/warehouse/Project_test
   28f059e..5d65d1e  master     -> origin/master
   28f059e..25ceef1  v1.1       -> origin/v1.1
更新 28f059e..5d65d1e
Fast-forward
 readme.txt | 6 ++++++
 reset.txt  	| 1 +
 2 files changed, 7 insertions(+)
ghost@ghost-machine:~/workspace/test/ghost_project/Project_test$

git查看历史记录 git log

git可以随时查看提交的历史记录,历史记录包括具体某次提交的commit哈希值,提交作者和提交的邮箱,提交的时间以及提交时候作者所做这个提交的备注和理由。

使用该命令可以查看到git commit提交的记录
commit d8b74f02ffdf6b2843fdce848490d40f6e562c95
Author: zx <zx@git.com>
Date:   Sun Nov 13 09:42:49 2016 +0800

    rm patch
commit 2ea33641ee8ec5c1e191764fa6d0c9c884632c10
Author: zx <zx@git.com>
Date:   Sun Nov 13 09:33:08 2016 +0800

    patch is 2

该命令可以有几个详细的查看方法:

命令:git log -p

查看详细的修改记录,如同git diff的格式一样将每个文件的修改一一列出来。

commit 2ea33641ee8ec5c1e191764fa6d0c9c884632c10
Author: zx <zx@git.com>
Date:   Sun Nov 13 09:33:08 2016 +0800

    patch is 2

diff --git a/patch.txt b/patch.txt
index 3886f8b..62b0eaf 100644
--- a/patch.txt
+++ b/patch.txt
@@ -1 +1,3 @@
 patch diff test
+
+patch is 2

命令:git log --stat

查看文件添加的记录

commit d8b74f02ffdf6b2843fdce848490d40f6e562c95
Author: zx <zx@git.com>
Date:   Sun Nov 13 09:42:49 2016 +0800

    rm patch

 0001-add-test-git-patch.patch | 21 ---------------------
 1 file changed, 21 deletions(-)

commit 2ea33641ee8ec5c1e191764fa6d0c9c884632c10
Author: zx <zx@git.com>
Date:   Sun Nov 13 09:33:08 2016 +0800

    patch is 2

 patch.txt | 2 ++
 1 file changed, 2 insertions(+)

命令:git log -p [filename | commit]

查看具体某个文件或某个commit提交的具体改动。

查看文件具体的修改点 git blame

通过该命令可以查看到文件的修改点具体是何人何时修改的,用于追溯代码的修改点。

ghost@ghost-machine:~/workspace/git_test$ git blame readme.txt
^0f58788 (zx 2016-06-13 21:56:46 +0800 1) add 2016.06.13
84cb30d3 (zx 2016-11-29 22:28:08 +0800 2) 
84cb30d3 (zx 2016-11-29 22:28:08 +0800 3) test for git stasha
84cb30d3 (zx 2016-11-29 22:28:08 +0800 4) 
84cb30d3 (zx 2016-11-29 22:28:08 +0800 5) add by 2016.11.29
5d65d1e8 (zx 2016-12-03 11:48:12 +0800 6) 
5d65d1e8 (zx 2016-12-03 11:48:12 +0800 7) test git status

查看分支操作记录 git reflog

可以查看所有分支的操作记录,及时恢复被git reset掉的commit提交。

ghost@ghost-machine:~/workspace/git_test$ git reflog
7929f3c HEAD@{0}: commit (amend): test git push
84fd0df HEAD@{1}: reset: moving to 84fd0df24
84cb30d HEAD@{2}: reset: moving to HEAD^
e5cc770 HEAD@{3}: commit (amend): add
84fd0df HEAD@{4}: commit (amend): test git push
5d65d1e HEAD@{5}: checkout: moving from v1.2 to master
7ad85d0 HEAD@{6}: commit (cherry-pick): git cherry-pick
0943962 HEAD@{7}: checkout: moving from v1.1 to v1.2
d1a4202 HEAD@{8}: checkout: moving from master to v1.1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值