本文是廖雪峰官网 https://www.liaoxuefeng.com/wiki/896043488029600的学习记录
Git基本操作
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
$ git add readme.txt
$ git commit -m "wrote a readme file"
[master (root-commit) eaadf4e] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 013b5bc..c91805e 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
Git is a distributed version control system.
-Git is free software.
\ No newline at end of file
+Git is free software.
+123123
\ No newline at end of file
$ git log
commit 4406d221d0ddfbf8eeeebfa044f192b2253f86a7 (HEAD -> master)
Author: hppp <1499931489@qq.com>
Date: Wed Jan 19 19:11:14 2022 +0800
append GPL
commit 576de96e634b0bb2b05b1c4aa09d29b62667f8b2
Author: hppp <1499931489@qq.com>
Date: Wed Jan 19 19:09:20 2022 +0800
123123
commit 9bcf89537588a83c9c8083342f6a5c0fc2ec0e96
Author: hppp <1499931489@qq.com>
Date: Tue Jan 18 09:27:24 2022 +0800
worte a readme file
$ git log --pretty=oneline
4406d221d0ddfbf8eeeebfa044f192b2253f86a7 (HEAD -> master) append GPL
576de96e634b0bb2b05b1c4aa09d29b62667f8b2 123123
9bcf89537588a83c9c8083342f6a5c0fc2ec0e96 worte a readme file
$ git reset --hard HEAD^
HEAD is now at 576de96 123123
$ git reset --hard 4406
HEAD is now at 4406d22 append GPL
$ 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 commit或git add的状态
$ git checkout -- readme.txt
git checkout – file中的–十分重要,若无–,就变成了切换到另一个分支的命令
对于第二种清空,我们需要在使用上述命令前,使用git reset HEAD <file> ,可以把暂存区的修改撤销掉(unstage),重新放回工作区。
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
Git添加远程库
其中,origin是命名习惯
$ git init
$ git remote add origin git@github.com:ThePassedWind/learngit.git
$ git add README.txt
$ git commit -m "addREADME"
$ git push -u origin master
$ git remote -v
$ git remote rm origin
$ git add docName
$ git commit -m "meaning"
$ git push -u origin master
远程库克隆
- 使用如下指令,可在当前目录下创建gitskills目录,包含了该远程库的所有内容
$ git clone git@github.com:ThePassedWind/gitskills.git
Cloning into 'gitskills'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
注意:Github给出的地址有ssh鹤https等协议,但ssh协议速度最快
分支管理
一开始,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点:
每次提交,master分支都会向前移动一步,随着不断提交,master分支的线不断变长
当我们创建新的分支,例如dev 时,Git新建了一个指针叫dev ,指向master 相同的提交,再把HEAD 指向dev ,就表示当前分支在dev 上:
Git创建一个分支很快,因为除了增加一个dev 指针,改改HEAD 的指向,工作区的文件都没有任何变化
不过,从现在开始,对工作区的修改和提交就是针对dev 分支了,比如新提交一次后,dev 指针往前移动一步,而master 指针不变:
假如我们在dev 上的工作完成了,就可以把dev 合并到master 上。Git怎么合并呢?最简单的方法,就是直接把master 指向dev 的当前提交,就完成了合并:
合并完分支后,甚至可以删除dev 分支。删除dev 分支就是把dev 指针给删掉,删掉后,我们就剩下了一条master 分支:
实战练习:
-
创建dev分支,然后切换到dev分支 $ git checkout -b dev
Switched to a new branch 'dev'
$ git branch dev
$ git checkout dev
Switched to branch 'dev'
$ git switch -c dev
-
查看当前分支 $ git branch
* dev
main
-
添加提交操作 $ git add readme.txt
$ git commit -m "branch test"
[dev b17d20e] branch test
1 file changed, 1 insertion(+)
-
切换回main分支 $ git checkout main
Switched to branch 'main'
$ git switch main
-
将dev分支的工作成果合并到main分支上 $ git branch -d dev
Deleted branch dev (was b17d20e).
-
合并完成后,可以放心的删除dev分支 $ git branch
* main
解决冲突
当两个分支同时进行修改时,我们为了避免冲突,需要删除一共分支。
-
创建新的分支feature1,然后修改readme.txt的内容 $ git switch -c feature1
Switched to a new branch 'feature1'
修改readme.txt为Creating a new branch is quick AND simple.
-
在feature1分支上提交 $ git add readme.txt
$ git commit -m "AND simple"
[feature1 14096d0] AND simple
1 file changed, 1 insertion(+), 1 deletion(-)
-
切换到master分支 如下,可以看到Git自动提示我们当前master分支比远程的master分支超前1个提交 $ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
修改readme.txt为Creating a new branch is quick & simple.
-
添加并提交 $ git add readme.txt
$ git commit -m "& simple"
[master 5dc6824] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
-
此时master分支和feature1分支各自都分别有新的提交,Git无法执行快速合并,只能试图把各自的修改合并起来,但这种合并就可能会有冲突。 $ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
-
通过git status查看冲突的文件 $ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
查看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.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
-
查看分支合并情况(git log) $ git log --graph --pretty=oneline --abbrev-commit
* cf810e4 (HEAD -> master) conflict fixed
|\
| * 14096d0 (feature1) AND simple
* | 5dc6824 & simple
|/
* b17d20e branch test
* d46f35e (origin/master) remove test.txt
* b84166e add test.txt
* 519219b git tracks changes
* e43a48b understand how stage works
* 1094adb append GPL
* e475afc add distributed
* eaadf4e wrote a readme file
-
删除feature1分支 $ git branch -d feature1
Deleted branch feature1 (was 14096d0).
当Git无法自动合并分支时,必须解决冲突,即删除一个分支,之后才能合并完成。
|