Git基本使用

Git使用

安装Git

ubuntu安装git:sudo apt-get install git

win安装git:https://git-scm.com/downloads

安装完之后需要配置git

$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"

将文件设为Git仓库

首先创建一个repository,即随便创建一个版本库,目录在/c/Users/Administrator/Desktop/learngit

harry@PC-20210331TPAQ MINGW64 ~
$ cd Desktop

harry@PC-20210331TPAQ MINGW64 ~/Desktop
$ mkdir learngit

harry@PC-20210331TPAQ MINGW64 ~/Desktop
$ cd learngit

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit
$ pwd
/c/Users/Administrator/Desktop/learngit

第二步将learngit变成git可管理的仓库,进入到learngit文件夹中,运行git init,这时在文件夹下会出现一个.git目录,此目录用来追踪版本管理的,使用ls -a查看,然后向learngit中添加一个文件,这里我们添加一个hello.py的文件,使用vi hello.py进行编辑,里面输入一行print("hello git!")保存退出

我们可以用cat hello.py命令查看文件的内容,随后使用git add filename来将文件添加到仓库中,并使用git commit -m “modified content”命令将文件提交到仓库,这里也可以添加多个文件到仓库中,然后使用commit一起提交,如果要将当前目录下的所有文件添加到仓库中去,使用git add .命令

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit
$ git init
Initialized empty Git repository in C:/Users/Administrator/Desktop/learngit/.git/

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ ls -a
./  ../  .git/

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello git!")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ python hello.py
hello git!

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "add a python file"
[master (root-commit) 04a9599] add a python file
 1 file changed, 1 insertion(+)
 create mode 100644 hello.py

接下来我们可以继续修改hello.py文件,比如我们把第一行的hello git!git!删掉并继续添加一行print("hello world!"),使用git status可以查看当前仓库状态,这里显示hello.py文件被修改了,但是还没被添加。如果想知道修改了什么内容,使用git diff hello.py命令,这里显示第一行的内容和第二行的内容均有改动,然后我们执行git add hello.py命令,将其添加到仓库中,然后运行git commit -m "add hello world",将其提交到仓库

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git diff hello.py
diff --git a/hello.py b/hello.py
index a0525b4..666980e 100644
--- a/hello.py
+++ b/hello.py
@@ -1 +1,2 @@
-print("hello git!")
+print("hello")
+print("hello world!")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.py


harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "add hello world"
[master 9c84bef] add hello world
 1 file changed, 2 insertions(+), 1 deletion(-)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

此时我们已经会应用git的基本命令了,在hello.py中继续添加一行print("learning git")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "append learning git"
[master a9c63ec] append learning git
 1 file changed, 1 insertion(+)

此时我们已经提交了三个版本了

版本1 "add a python file"
print("hello git!")

版本2 "modified content"
print("hello")
print("hello world!")

版本3 "learning git"
print("hello")
print("hello world!")
print("learning git")

版本回退

git log可以查看最近提交的日志,其中commit是版本号

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git log
commit a9c63ec70d3a0b1cff15c774b13590127275ac19 (HEAD -> master)
Author: harrytea <1061771439@qq.com>
Date:   Thu Sep 16 09:48:33 2021 +0800

    append learning git

commit 9c84bef9447091deed788483669ebbae5ee94dd8
Author: harrytea <1061771439@qq.com>
Date:   Thu Sep 16 09:03:12 2021 +0800

    add hello world

commit 04a9599ba04bee172c8565a47acc07854830de50
Author: harrytea <1061771439@qq.com>
Date:   Thu Sep 16 08:50:23 2021 +0800

    add a python file

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git log --pretty=oneline
a9c63ec70d3a0b1cff15c774b13590127275ac19 (HEAD -> master) append learning git
9c84bef9447091deed788483669ebbae5ee94dd8 add hello world
04a9599ba04bee172c8565a47acc07854830de50 add a python file

在Git中,HEAD表示当前版本,这里的版本号为a9c63ec70,上一个版本就是HEAD^,上上一个版本是HEAD^^,如果往上100个版本,可以写成HEAD~100,这里我们将append learning git回退到上一个版本add hello world,使用命令git reset --hard HEAD^,此时使用cat命令查看hello.py的内容,发现已经回到上个版本,再用git log查看版本信息,发现上一个版本消失了,此时有两种解决办法。

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git log --pretty=oneline
a9c63ec70d3a0b1cff15c774b13590127275ac19 (HEAD -> master) append learning git
9c84bef9447091deed788483669ebbae5ee94dd8 add hello world
04a9599ba04bee172c8565a47acc07854830de50 add a python file

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git reset --hard HEAD^
HEAD is now at 9c84bef add hello world

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello")
print("hello world!")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git log --pretty=oneline
9c84bef9447091deed788483669ebbae5ee94dd8 (HEAD -> master) add hello world
04a9599ba04bee172c8565a47acc07854830de50 add a python file
  • 如果没有关闭命令行,可以看到append learning git的版本id为a9c63ec70(不需要将所有版本号都打出来),然后运行git reset --hard a9c63ec70即可
  • 如果关闭了命令行,使用git reflog命令,可以记录每一次命令,可以看到前面显示了版本的id,这里我们已经运行过git rest --hard a9c63ec了,所以版本已经回退到append learning git
# 未关闭命令行
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git reset --hard a9c63ec
HEAD is now at a9c63ec append learning git

# 关闭命令行
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git reflog
a9c63ec (HEAD -> master) HEAD@{0}: reset: moving to a9c63ec
9c84bef HEAD@{1}: reset: moving to HEAD^
a9c63ec (HEAD -> master) HEAD@{2}: commit: append learning git
9c84bef HEAD@{3}: commit: add hello world
04a9599 HEAD@{4}: commit (initial): add a python file

文件操作

git有工作区和缓存区的概念,可以将工作区简单理解为我们当前工作目录,缓存区就是一个将修改后的文件add之后的地方,可以当做一个虚拟的空间,当执行commit命令之后,这个虚拟空间的内容就被合并到master分支当中了。

[工作区] ----add-----> [缓存区] ----commit----> [master]

这里我们将hello.py进行修改,在加入一行print("learn work"),随后重建一个文件hello2.py,写一行print("i'm the second file"),运行git status查看当前状态,发现hello.py被修改了,而hello2.py刚被创建,所以显示Untracked files

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello2.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello2.py
print("i'm the second file")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

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

no changes added to commit (use "git add" and/or "git commit -a")

使用git add hello.py hello2.py将其添加到缓存区,然后git status查看一下状态,此时两个文件被放到了暂存区(stage),当运行git commit -m “know work”时,文件被合并到分支

运行add

work: hello.py hello2.py ---> stage: hello.py hello2.py ---> branch: none

运行commit

work: hello.py hello2.py ---> stage: none ---> branch: hello.py hello2.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add hello.py hello2.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.py
        new file:   hello2.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "know work"
[master 87098dd] know work
 2 files changed, 2 insertions(+)
 create mode 100644 hello2.py
 
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

撤销修改

在工作区将hello.py最后一行加入aaaaa,用git status查看当前状态,根据提示,可以用git restore [filename]丢弃在工作区的修改

git restore [filename]将文件在工作取得修改全部撤销,有两种情况

  • file修改后没有放到暂存区,撤销修改就回到和版本库一模一样的状态
  • file修改后放到暂存区,又做了修改,撤销修改就回到添加到暂存区后的状态

总之,就是让文件回到最近一次被git commit或git add的状态

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")
aaaaa

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git resotre hello.py
git: 'resotre' is not a git command. See 'git --help'.

The most similar command is
        restore

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git restore hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")

如果修改的文件已经被提交到了缓存区(stage),这里我们依然用上面的例子,需要用提示的git store --staged [filename]命令,此时修改已经被退回到了工作区,在使用git restore [filename]就可以回退到最初版本了

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.py


harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git restore --staged hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git restore hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")

删除文件

将修改后的文件添加到缓存区(stage)使用add,如果是删除使用git rm [filename],如果误删了使用git restore [filename]

如果删除的文件本身就不在版本库中,删除了是无法恢复的

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ vi text.txt

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git add text.txt

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "add text.txt"
[master 6f8dba2] add text.txt
 1 file changed, 1 insertion(+)
 create mode 100644 text.txt

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ rm text.txt

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    text.txt

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git rm text.txt
rm 'text.txt'

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "remove text.txt"
[master 2d0ad2c] remove text.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 text.txt

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

上传GitHub

首先进入到自己电脑里的.ssh目录,查看目录下有没有id_rsa(不能泄露)和id_rsa.pub文件,如果有了就不需要运行下面的命令,如果没有运行下面的命令生成SSH Key,其中mail是你的邮箱

ssh-keygen -t rsa -C "yourmail@example.com"

文件中有了密钥之后,打卡GitHub网站,在自己账户的SSH Keys选项下,将自己的公钥id_rsa.pub复制上去即可,此时GitHub已经添加了对本机的信任,然后在GitHub上创建一个learngit的仓库,创建完空仓库以后,会提示以下命令

...or create a new repository on the command line
echo "# learngit" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:harrytea/learngit.git
git push -u origin main


...or push an existing repository from the command line
git remote add origin git@github.com:harrytea/learngit.git
git branch -M main
git push -u origin main

由于我们本地已经有一个git仓库了,所以我们这里将本地的仓库上传到GitHub上去,运行命令

git remote add origin git@github.com:harrytea/learngit.git
git branch -M main # 重命名分支为main
git push -u origin main # 将本地仓库push到远程中,只有在第一次使用时加入-u

注意:使用git push -u origin main时报错了,这里的意思是远程库与本地库不一致造成的,只要把远程库同步到本地库就可以了,使用git pull --rebase origin master其中--rebase的意思是取消掉本地库中的commit,并把他们接到更新后的版本库之中

具体解释

发生问题时的状态

  • 远程库:1-->2-->3 此时远程库更新了3,而本地库没有同步
  • 本地库:1-->2-->4 本地库更新到了3,想push到远程库,失败了

使用git pull -rebase origin master之后,首先取消了commit记录,将他们临时保存为补丁放到了.git/rebase中,然后同步远程库到本地,最后合并补丁到本地库之中

  • 远程库:1-->2-->3
  • 本地库:1-->2-->3(使用了git pull)-->4

把本地push到远程库中

  • 远程库:1-->2-->3-->4
  • 本地库:1-->2-->3-->4
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git remote add origin git@github.com:harrytea/learngit.git

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (master)
$ git branch -M main

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git push -u origin main
To github.com:harrytea/learngit.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:harrytea/learngit.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git pull --rebase origin main
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 591 bytes | 45.00 KiB/s, done.
From github.com:harrytea/learngit
 * branch            main       -> FETCH_HEAD
 * [new branch]      main       -> origin/main
Successfully rebased and updated refs/heads/main.

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git push -u origin main
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 4 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (17/17), 1.45 KiB | 744.00 KiB/s, done.
Total 17 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To github.com:harrytea/learngit.git
   f1732bd..25e1683  main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

如果添加远程库时地址写错了,想删除,使用git remote rm [name]命令,在使用之前先使用git remote -v查看远程库信息

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git remote -v
origin  git@github.com:harrytea/learngit.git (fetch)
origin  git@github.com:harrytea/learngit.git (push)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git remote rm origin

# 此时在执行push命令会提示没有关联仓库
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git push origin main
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git remote add origin git@github.com:harrytea/learngit.git

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git push -u origin main
Everything up-to-date
Branch 'main' set up to track remote branch 'main' from 'origin'.

分支

分支的基本操作

创建与合并分支,首先明白分支的基本运行原理

一开始的时候只有mater分支每次提交master都会向前移动一步,在这个过程中HEAD始终指向master

  • 1-->2-->3(master)

当我们创建新的分支(dev)时,指向master,再把HEAD指向dev,表示当前分支在dev上

  • 1-->2-->3(master, dev)-->4
  • 1-->2-->3(master)-->4(dev)

当在dev的工作完成时,把dev合并到master上,直接将master指向dev即可,合并完后就可以删除dev分支了,只剩下master

  • 1-->2-->3-->4(master, dev)
  • 1-->2-->3-->4(master)

这里我们将上述的过程运行一遍,命令主要用到git checkout -b dev创建并切换分支,git branch查看当前分支,git checkout main切换分支,git merge合并分支,git branch -d dev删除分支

除此之外,还有新的创建分支命令git switch -c dev git switch main

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git checkout -b dev
Switched to a new branch 'dev'

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git branch
* dev
  main

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ ls
README.md  hello.py  hello2.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ vim hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git commit -m "branch"
[dev 92b027c] branch
 1 file changed, 1 insertion(+)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git merge dev
Updating 25e1683..92b027c
Fast-forward
 hello.py | 1 +
 1 file changed, 1 insertion(+)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git branch -d dev
Deleted branch dev (was 92b027c).

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git branch
* main

解决冲突

有时候我们创建了新的分支feature1在上面进行修改并commit后,我们又回到了main中进行修改commit,这样在合并两个分支的时候就会出现冲突,这时就需要我们手动进行删除

# 创建分支feature1
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git switch -c feature1
Switched to a new branch 'feature1'

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (feature1)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (feature1)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (feature1)
$ git commit -m "conflict YES"
[feature1 822b4e1] conflict YES
 1 file changed, 1 insertion(+)

# 回到main分支中进行修改
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (feature1)
$ git switch main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git commit -m "conflict NO"
[main 6994ed6] conflict NO
 1 file changed, 1 insertion(+)

# 合并时出现了冲突
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git merge feature1
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py
Automatic merge failed; fix conflicts and then commit the result.

# 查看当前状态并手动修改冲突
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main|MERGING)
$ git status
On branch main
Your branch is ahead of 'origin/main' 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:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main|MERGING)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")
you name
<<<<<<< HEAD
conflict NO
=======
conflict YES
>>>>>>> feature1

# 添加的内容修改成自己想要的
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main|MERGING)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main|MERGING)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main|MERGING)
$ git commit -m "conflict yes"
[main 745748c] conflict yes

# 查看合并的过程
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git log --graph --pretty=oneline --abbrev-commit
*   745748c (HEAD -> main) conflict yes
|\
| * 822b4e1 (feature1) conflict YES
* | 6994ed6 conflict NO
|/
* 92b027c branch
* 25e1683 (origin/main) remove text.txt
* 4bdd4eb add text.txt
* 0ab3842 know work
* 5e3279d append learning git
* 3f61a4f add hello world
* 32d68dc add a python file
* f1732bd Initial commit

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git branch
  feature1
* main

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git branch -d feature1
Deleted branch feature1 (was 822b4e1).

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ cat hello.py
print("hello")
print("hello world!")
print("learning git")
print("learn work")
you name
conflict yes

多人合作的分支管理

在上面的合并中,git使用了Fast forward模式,再删除分之后,会丢掉分支信息,如果强制禁用Fast forward模式,在merge时会生成一个新的commit,这样在分支历史上就能看出分支信息,禁用Fast forward模式使用--no-ff命令

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git switch -c dev
Switched to a new branch 'dev'

# 在最后一行加一个branch
harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ vi hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git status
On branch dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git add hello.py

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git commit -m "add branch"
[dev df953a4] add branch
 1 file changed, 1 insertion(+)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (dev)
$ git switch main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
 hello.py | 1 +
 1 file changed, 1 insertion(+)

harry@PC-20210331TPAQ MINGW64 ~/Desktop/learngit (main)
$ git log --graph --pretty=oneline --abbrev-commit
*   0a2b770 (HEAD -> main) merge with no-ff
|\
| * df953a4 (dev) add branch
|/
*   745748c conflict yes
|\
| * 822b4e1 conflict YES
* | 6994ed6 conflict NO
|/
* 92b027c branch
* 25e1683 (origin/main) remove text.txt
* 4bdd4eb add text.txt
* 0ab3842 know work
* 5e3279d append learning git
* 3f61a4f add hello world
* 32d68dc add a python file
* f1732bd Initial commit

临时修改bug,如果当前的任务还没完成,不能commit,但是有一个bug必须修复而且要提交应该怎么做呢?我们只需要创建一个分支issue-101即可,首先在dev分支上进行修改

Git命令

git diff [filename]                # 查看文件改动
git diff HEAD -- [filename]        # 查看文件在工作区和版本库的区别
posted @ 2021-09-20 16:58  harrytea  阅读(74)  评论(0编辑  收藏  举报