Git使用的基本方法

31 Dec 2016 » git

Git的使用方法,忘记了就多看看

1. 安装完成后,还需要最后一步设置,输入

$ git config --global user.name "qlb6x"
$ git config --global user.email "[email protected]"

2. 找到一个合适的位置,创建空目录

$ mkdir test
$ cd test
$ pwd
/Users/michael/test

3. 把这个目录变为git可以管理的仓库

$ git init

4. 添加文件

$ git add readme.txt

5. 把文件提交到仓库

$ git commit -m "write a readme file"

6. 查看提交结果

$ git status

7. 有没有被修改过

$ git diff

8. 查看版本控制历史记录

$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <[email protected]>
Date:   Tue Aug 20 15:11:49 2013 +0800
    append GPL
		
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <[email protected]>
Date:   Tue Aug 20 14:53:12 2013 +0800
		
	 add distributed
		
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <[email protected]>
Date:   Mon Aug 19 17:51:55 2013 +0800
		
	wrote a readme file
			
$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file

9. 退回到上一个版本

$ git reset --hard HEAD^
$ git reset --hard 3628164

在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^,也可以写成HEAD^100

10. 丢弃工作区的修改让文件回到最近一次git commitgit add时的状态

$ git checkout -- readme.txt

11. 修改了文件,并且git add到暂存区了,但是在git commit之前,用以下指令把暂存区的修改撤销,重新放回工作区

$ git reset HEAD readme.txt 

小结

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。

12. 从版本库中删除该文件,用git rm删除,并且git commit

$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"

13. 另一种情况是删错了

$ git checkout -- test.txt

git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以”一键还原”。

命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

14. 克隆一个本地库

$ git clone [email protected]:qlb6x/Qlb6x.github.io
Cloning into 'qlb6x.github.io'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), 7.12 KiB | 0 bytes/s, done.
Checking connectivity... done.

原文:大专栏  Git使用的基本方法


猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11615116.html