git的简单玩法

本篇笔记参考廖雪峰的git教程,为方便查看将命令部分提取并记录下来。

无意对原作的版权侵犯,如需要学习请到廖雪峰网站学习git

 

创建git仓库

# mkdir learngit && cd learngit
# git init

添加一个文件 readme.txt 内容如下:

Git is a version control system.
Git is free software.

将修改的文件加入git缓存

# git add readme.txt

然后将缓存提交到本地仓库

git commit -m "wrote a readme file"
[master (root-commit) cb926e7] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt

 

修改readme.txt文件

Git is a distributed version control system.
Git is free software distributed under the GPL.

然后尝试提交:

$ git add readme.txt
$ git commit -m "append GPL"
[master 3628164] append GPL
 1 file changed, 1 insertion(+), 1 deletion(-)

 修改readme.txt文件,改成如下内容:

Git is a distributed version control system.
Git is free software.

运行 git status 查看git状态

# git status
# On branch master
# 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.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

可以看到readme.txt被修改,查看修改的具体内容

# git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

 

说明:

git diff        #是工作区(work dict)和暂存区(Stage)的比较

git diff --cached             #是暂存区(Stage)和分支(Master)的比较

 

提交修改到缓存中

# git add readme.txt

我们再运行git status看看当前仓库的状态

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

依然是readme.txt被修改,此时可以放心提交到仓库

# git commit -m "add distributed"
[master ea34578] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)

提交后,我们再用git status命令看看仓库的当前状态:

# git status
# On branch master
nothing to commit (working directory clean)

 

现在有三个版本:

版本1:wrote a readme file

Git is a version control system.
Git is free software.

版本2:add distributed

Git is a distributed version control system.
Git is free software.

版本3:append GPL

Git is a distributed version control system.
Git is free software distributed under the GPL.

 

使用 git log 查看历史版本

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 15:11:49 2013 +0800

    append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

加上--pretty=oneline参数可以只输出摘要信息

$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file

像 3628164fb26d48395383f8f31179f24e0882e1e0 称为 commit ID ,用于代表某个版本,通过他可以回退版本

 

假如现在想要回退到  add distributed  时的状态:

$ git reset --hard ea34578d5496d7dd233c827ed32a8cd576c5ee85

如果你保证commit ID前5位唯一,也可以简写:

$ git reset --hard ea3457

或者使用 HEAD^ 代表HEAD指针指向上一个版本:

git reset --hard HEAD^

 

 

说明:

版本回退后,即HEAD指针指向上个版本HEAD^

 

如果想指向上上个版本用:HEAD^^      ,以此类推

如果想指向上100个版本用: HEAD~100  

 

现在已经成功回退到 add distributed 状态了,此时查看log

$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

最新的那个版本append GPL已经看不到了!,而现在又想恢复到那个最新的版本方法如下:

 

方法一:如果终端没关,向上翻记录找到最新版本的coommit ID号

$ git reset --hard 3628164
HEAD is now at 3628164 append GPL

方法二:如果终端关了,查看git历史操作寻找新版本的commit ID号

$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file

方法三:如果历史操作也被其他操作覆盖记录了,查看历史所有版本号

$ git log --graph --pretty=format:'%h -%d %s (%cr)' --abbrev-commit --reflog

 

现在既可以向后回退,也可以向前恢复,只要有COMMIT ID号即可,查看当前状态号

$ git rev-parse HEAD

 

假如当前提交了多个文件a b c,现在只想恢复文件c的上次状态,其他文件a 和 b保持当前状态

$ git checkout HEAD^ -- c

或者

$ git checkout HEAD^ c

 

解决fatal:remote error:You can't push to git://github.com/username/*.git问题的办法

今天Git push的时候,发现没有push权限

fatal:remote error:
You can't push to git://github.com/username/*.git
Use git@github.com:username/*.git

解决方法:

git remote rm origin
git remote add origin git@github.com:username/resname.git

 

 https://git-scm.com/blog

本篇笔记参考廖雪峰的git教程,为方便查看将命令部分提取并记录下来。

无意对原作的版权侵犯,如需要学习请到廖雪峰网站学习git

posted @ 2017-10-12 11:21  Khazix  阅读(370)  评论(0编辑  收藏  举报