git 的简单操作,学习记录总结自廖雪峰老师的git教程

 好记性不如烂笔头,以下记录来源自廖雪峰老师的git教程,仅作记录没有过多的知识描述,墙裂推荐学习廖雪峰老师的原版git课程,详细技巧幽默,附上网址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

一、创建仓库

第一步:

toohoo@ubuntu:~$ mkdir django
toohoo@ubuntu:~$ cd django/
toohoo@ubuntu:~/django$ pwd
/home/toohoo/django
toohoo@ubuntu:~/django$

第二步:

toohoo@ubuntu:~/django$ git init
Initialized empty Git repository in /home/toohoo/django/.git/

第三步:

toohoo@ubuntu:~/django$ vim readme.txt
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a version control system.
Git is free software.

第四步:添加文件到仓库:

toohoo@ubuntu:~/django$ git add readme.txt

第五步:提交文件到仓库:

toohoo@ubuntu:~/django$ git commit -m "wrote a readme file"
[master (root-commit) e3dd442] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt

注意:commit可以一次提交很多文件,所以你可以多次add不同的文件

例如:

toohoo@ubuntu:~/django$ touch file1.txt file2.txt file3.txt
toohoo@ubuntu:~/django$ git add file1.txt 
toohoo@ubuntu:~/django$ git add file2.txt file3.txt 
toohoo@ubuntu:~/django$ git commit -m "add 3 files."
[master a3d9b84] add 3 files.
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
 create mode 100644 file3.txt

 二、文件版本向前向后控制

第一步:修改readme.txt

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software.
toohoo@ubuntu:~/django$ 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")

第二步:查看不同 git diff

toohoo@ubuntu:~/django$ git diff
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.

第三步:将更新的代码添加和提交并查看状态

toohoo@ubuntu:~/django$ git add readme.txt 
toohoo@ubuntu:~/django$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt

toohoo@ubuntu:~/django$ git commit -m "add distributed"
[master ef05297] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)
toohoo@ubuntu:~/django$ git status
On branch master
nothing to commit, working tree clean

第四步:再来一次,添加一个版本,学习版本回退

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.

toohoo@ubuntu:~/django$ git add readme.txt 
toohoo@ubuntu:~/django$ git commit -m "append GPL"
[master d17dd88] append GPL
 1 file changed, 2 insertions(+), 1 deletion(-)

第五步:git log 查看版本变化情况:

toohoo@ubuntu:~/django$ git log
commit d17dd88a942fddb4059f1a0c47efb4dd73e7cb3e (HEAD -> master)
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:37:11 2019 +0800

    append GPL

commit ef052970599241555a6620621c55f9048d58f4a5
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:32:06 2019 +0800

    add distributed

commit a3d9b84bbb4aa3c922de87638a4cf8f795dfb404
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:21:16 2019 +0800

    add 3 files.

commit e3dd442003b6d0b3a2e49a0a7869372b419d7a09
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:17:49 2019 +0800

    wrote a readme file
toohoo@ubuntu:~/django$ git log --pretty=oneline
d17dd88a942fddb4059f1a0c47efb4dd73e7cb3e (HEAD -> master) append GPL
ef052970599241555a6620621c55f9048d58f4a5 add distributed
a3d9b84bbb4aa3c922de87638a4cf8f795dfb404 add 3 files.
e3dd442003b6d0b3a2e49a0a7869372b419d7a09 wrote a readme file

第六步:版本的回退和向前找回以及查看操作

toohoo@ubuntu:~/django$ git reset --hard HEAD^
HEAD is now at ef05297 add distributed
toohoo@ubuntu:~/django$ cat readme.txt
Git is a distributed version control system.
Git is free software.
toohoo@ubuntu:~/django$ git log
commit ef052970599241555a6620621c55f9048d58f4a5 (HEAD -> master)
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:32:06 2019 +0800

    add distributed

commit a3d9b84bbb4aa3c922de87638a4cf8f795dfb404
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:21:16 2019 +0800

    add 3 files.

commit e3dd442003b6d0b3a2e49a0a7869372b419d7a09
Author: too-hoo <13414851554@163.com>
Date:   Thu Mar 7 09:17:49 2019 +0800

    wrote a readme file
toohoo@ubuntu:~/django$ git reset --hard d17dd88a942fddb4059f1a0c47efb4dd73e7cb3e
HEAD is now at d17dd88 append GPL
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
toohoo@ubuntu:~/django$ git reflog # 记录每一次命令,可以使用来进行版本控制
d17dd88 (HEAD -> master) HEAD@{0}: reset: moving to d17dd88a942fddb4059f1a0c47efb4dd73e7cb3e
ef05297 HEAD@{1}: reset: moving to HEAD^
d17dd88 (HEAD -> master) HEAD@{2}: commit: append GPL
ef05297 HEAD@{3}: commit: add distributed
a3d9b84 HEAD@{4}: commit: add 3 files.
e3dd442 HEAD@{5}: commit (initial): wrote a readme file

第七步:再进行一次修改添加提交操作,理解工作区和暂存区

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.

toohoo@ubuntu:~/django$ echo "hahahahahaaaaaa" >> LICENSE
toohoo@ubuntu:~/django$ ls
file1.txt  file2.txt  file3.txt  LICENSE  readme.txt
toohoo@ubuntu:~/django$ 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	LICENSE

no changes added to commit (use "git add" and/or "git commit -a")
toohoo@ubuntu:~/django$ git add readme.txt 
toohoo@ubuntu:~/django$ git add LICENSE
toohoo@ubuntu:~/django$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   LICENSE
	modified:   readme.txt

toohoo@ubuntu:~/django$ git commit -m "understand how stage works"
[master ccd4c13] understand how stage works
 2 files changed, 2 insertions(+)
 create mode 100644 LICENSE
toohoo@ubuntu:~/django$ git status
On branch master
nothing to commit, working tree clean

 

第八步:管理修改

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
toohoo@ubuntu:~/django$ git add readme.txt
toohoo@ubuntu:~/django$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

toohoo@ubuntu:~/django$ git commit -m "git tracks changes"
[master 7d71f96] git tracks changes
 1 file changed, 1 insertion(+)
toohoo@ubuntu:~/django$ 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")
toohoo@ubuntu:~/django$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 8b0eb8c..d021562 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,5 +1,5 @@
 Git is a distributed version control system.
 Git is free software distributed under the GPL.
 Git has a mutable index called stage.
-Git tracks changes.
+Git tracks changes of files.
toohoo@ubuntu:~/django$ git add readme.txt 
toohoo@ubuntu:~/django$ git commit -m "第二次修改提交"
[master 67b7e8e] 第二次修改提交
 1 file changed, 1 insertion(+), 1 deletion(-)

第九步:撤销修改

toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.

toohoo@ubuntu:~/django$ 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")
toohoo@ubuntu:~/django$ git checkout -- readme.txt
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
toohoo@ubuntu:~/django$ vim readme.txt 
toohoo@ubuntu:~/django$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
toohoo@ubuntu:~/django$ 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")
toohoo@ubuntu:~/django$ git add readme.txt 
toohoo@ubuntu:~/django$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt
toohoo@ubuntu:~/django$ git reset HEAD readme.txt
Unstaged changes after reset:
M	readme.txt
toohoo@ubuntu:~/django$ 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")
toohoo@ubuntu:~/django$ git checkout -- readme.txt
toohoo@ubuntu:~/django$ git status
On branch master
nothing to commit, working tree clean


第十步:删除文件

toohoo@ubuntu:~/django$ touch test.txt
toohoo@ubuntu:~/django$ git add test.txt 
toohoo@ubuntu:~/django$ git commit -m "add test.txt"
[master 9e505fe] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
toohoo@ubuntu:~/django$ rm test.txt
toohoo@ubuntu:~/django$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    test.txt

no changes added to commit (use "git add" and/or "git commit -a")
# git checkout -- test.txt 可以在没有从版本库中将文件还原到工作区
toohoo@ubuntu:~/django$ git rm test.txt  # 确定从版本库中删除test.txt 
rm 'test.txt'
toohoo@ubuntu:~/django$ git commit -m "remove test.txt"
[master 11305cd] remove test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值