Git版本控制_&_分支管理

文章目錄

Git基本操作

1. git 兩大特點

  • 版本控制:可以解決多人同時開發的代碼問題,也可以解決找回歷史代碼的問題

  • 分佈式: Git是分佈式的版本控制系統,同一個Git倉庫,可以分不到不同的機器上。

    • 首先找一臺電腦充當服務器的角色,7*24小時開機,其他每個人都從這個“服務器”倉庫克隆一份到自己的電腦,
      並且把各自的提交 推送(commit/push) 到服務器倉庫裏,也可以從服務器倉庫中 拉取(pull) 別人的提交。

    • 可以自己搭建這臺“服務器”,也可以使用GitHub。
      在這裏插入圖片描述

2. 安裝與配置

  • Ubuntu 系統爲例:
sudo apt-get install git
  • 安裝成功之後 用git 查看相關指令說明,git version 查看版本號
[fuli@16:20:58]~/PycharmProjects/git_action$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

[fuli@16:19:34]~/PycharmProjects/git_action$ git version
git version 2.15.1 (Apple Git-101)

3. 創建一個版本庫

  • 新建一個目錄 或者 cd到需要用git管理的目錄下, 執行 git init,會生成一個.git 隱藏目錄,幫助管理該目錄(directory) 下的文件
[fuli@16:23:00]~/PycharmProjects/git_action$ git init
Initialized empty Git repository in /Users/fuli/PycharmProjects/git_action/.git/

4.版本創建與回退

4.1 使用

  1. git_action 目錄下創建一個文件 code.txt, 編輯內容如下:
[fuli@16:23:07]~/PycharmProjects/git_action$ vim code.txt
[fuli@16:31:54]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
  1. 使用下面兩條命令可以創建一個版本
git add code.txt

git commit -m "版本註釋, for instance: 版本1.123"

操作如下:

[fuli@16:47:33]~/PycharmProjects/git_action$ git log
fatal: your current branch 'master' does not have any commits yet
[fuli@16:47:39]~/PycharmProjects/git_action$ git add code.txt 
[fuli@16:47:46]~/PycharmProjects/git_action$ git commit -m "版本0.01"
[master (root-commit) ba26141] 版本0.01
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 code.txt
  1. 使用 git log 可以查看版本記錄
[fuli@16:48:22]~/PycharmProjects/git_action$ git log
commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@16:52:14]~/PycharmProjects/git_action$ 
  1. 繼續編輯 code.txt, 增加一行, 然後創建一個新版本,再查看日誌信息。
[fuli@16:52:14]~/PycharmProjects/git_action$ vim code.txt 
[fuli@16:57:15]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.
[fuli@16:57:18]~/PycharmProjects/git_action$ git add code.txt 
[fuli@16:58:17]~/PycharmProjects/git_action$ git commit -m "版本0.02"
[master bbf036c] 版本0.02
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
[fuli@16:58:35]~/PycharmProjects/git_action$ git log
commit bbf036c780245dffa56052047d6b0d1bbae939d0 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
  • git在管理不同的版本時,只記錄當前版本相較於上一個版本發生了哪些變化,不會拷貝之前版本沒有修改的內容。 上兩步的操作如圖所示:
    [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-o4TogVQD-1590760213645)(./img/WechatIMG94.png)]

  1. 版本回退:若想回到之前的某一個版本,可以使用git reset --hard HEAD^
    • 其中 HEAD 表示當前最新的版本, HEAD^表示當前版本的前一個版本, HEAD^^ 表示當前版本的前前一個版本(往前回退2次).

    • 也可以使用 HEAD~1 表示當前版本的前一個版本,HEAD~100 表示當前版本的前100個版本。

# 回退到上一個版本
[fuli@16:58:45]~/PycharmProjects/git_action$ git reset --hard HEAD^
HEAD is now at ba26141 版本0.01
[fuli@17:35:02]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
[fuli@17:35:09]~/PycharmProjects/git_action$ git log
commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
  • 回退之後,git並沒有刪除之前的信息,而是將頭指針移動到了對應的位置。上面操作之後的示意圖如下:
    [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-c7T4aaRS-1590760213647)(./img/WechatIMG97.png)]
  1. 如果再想返回到最新版本,需要用到版本號信息。
    • 即 git也可以用版本號直接指定HEAD指向的版本信息,格式爲 git reset --hard 版本號

    • 這裏所說的版本號是 在每次 commit 之後,系統會返回版本號信息,如下

      • commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
    • 可以用 git reflog 查看之前的操作記錄,該操作記錄會顯示每次 commit 的版本號信息。

  • 例如:重新返回到 版本0.02
# 執行 git reflog 之後,返回的每一行最前面的值 即爲版本號的 前7位,可以只輸入這7爲 返回到其對應的版本。
[fuli@17:45:30]~/PycharmProjects/git_action$ git reflog
ba26141 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
bbf036c HEAD@{1}: commit: 版本0.02
ba26141 (HEAD -> master) HEAD@{2}: commit (initial): 版本0.01
[fuli@17:47:25]~/PycharmProjects/git_action$ git reset --hard bbf036c
HEAD is now at bbf036c 版本0.02
[fuli@17:47:46]~/PycharmProjects/git_action$ git log
commit bbf036c780245dffa56052047d6b0d1bbae939d0 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@17:47:52]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

4.2 工作區和版本庫

工作區 ( Working Directory )

  • pc中的目錄,比如當前的目錄 git_action 就是一個工作區。

版本庫 ( Repository )

  • 工作區有一個隱藏目錄 .git,這個隱藏目錄就是 git 的版本庫。

    • git 版本庫裏存了很多東西,其中最重要的就是稱爲 stage 或者叫 (index) 的暫存區,還有git自動創建的第一個分支 master ,以及指向 master 的一個指針 HEAD.

    • 在創建 git 版本庫時, git 自動創建了唯一一個 master 分區。 故, git commit 默認是往 master 分支上提交修改。

    • 可以理解爲:需要提交的文件修改通通放到暫存區( git add 文件名 , 可以多次 add ),然後一次性提交暫存區的所有修改(一次 git commit -m "註釋")。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-4LaoJ8RM-1590760213649)(./img/WechatIMG93.png)]

  • 前面提到:把文件往 git 版本庫中添加分兩步執行。
      1. git add 文件名 把文件添加進去,實際上是把文件修改添加到了暫存區
      1. git commit -m "註釋" 提交更改,實際上是把暫存區的所有內容提交到當前分支。

    1. 下面 再創建一個 code2.txt 並編輯內容爲 添加一行,
    1. 然後再次編輯 code.txt 內容,在其中添加一行
[fuli@20:11:41]~/PycharmProjects/git_action$ vim code2.txt
[fuli@20:13:38]~/PycharmProjects/git_action$ cat code2.txt 
This is code2's first line...
[fuli@20:13:43]~/PycharmProjects/git_action$ vim code.txt 
[fuli@20:22:28]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

    1. 使用 git status 查看當前工作樹的狀態。
[fuli@20:22:34]~/PycharmProjects/git_action$ 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:   code.txt

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

	code2.txt

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

其中,Changes not staged for commit 表示 尚未暫存椅背提交的變更,即 code.txt 修改的內容還未 git add

Untracked files 表示 未跟蹤的文件,也即新建的 code2.txt 還未 git add

no changes added to commit 表示修改還未加入提交。


接着執行下面操作, 將修改添加到暫存區

[fuli@20:29:42]~/PycharmProjects/git_action$ git add code.txt code2.txt 
[fuli@20:29:54]~/PycharmProjects/git_action$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   code.txt
	new file:   code2.txt

    1. 最後執行 git commit -m "註釋"一次性把所有的修改提交到分支,創建一個版本
[fuli@20:29:59]~/PycharmProjects/git_action$ git commit -m "版本3"
[master aa4e0e8] 版本3
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 2 files changed, 3 insertions(+)
 create mode 100644 code2.txt
[fuli@20:32:17]~/PycharmProjects/git_action$ git status
On branch master
nothing to commit, working tree clean
[fuli@20:32:23]~/PycharmProjects/git_action$ git log
commit aa4e0e8732d92d5b46c6a0db37e05442ed0d84da (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 20:32:17 2020 +0800

    版本3

commit bbf036c780245dffa56052047d6b0d1bbae939d0
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-JCMOns8x-1590760213650)(./img/WechatIMG95.png)]


4.3 管理修改

  • git 管理文件:只會提交暫存區的修改來創建一個新的版本

  • 一個例子:

    • code.txt 中新增一行

    • 執行 git add <file>,將修改添加到暫存區

    • 再在code.txt 中新增一行. (這次的修改並沒有 git add)

    • 執行 git commit -m "版本4", 創建一個新版本。

    • 使用 git status 查看工作樹(the working tree)的狀態,發現第二次修改由於沒有被添加到暫存區,導致新創建的版本中沒有該修改記錄。

[fuli@22:53:51]~/PycharmProjects/git_action$ ls
code.txt  code2.txt
[fuli@22:53:53]~/PycharmProjects/git_action$ vim code.txt 
[fuli@22:54:26]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...
[fuli@22:54:32]~/PycharmProjects/git_action$ git add code.txt 
[fuli@22:54:42]~/PycharmProjects/git_action$ vim code.txt 
[fuli@22:55:20]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...

the new line...
[fuli@22:55:24]~/PycharmProjects/git_action$ git commit -m "版本4"
[master 7e9087b] 版本4
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
[fuli@22:55:41]~/PycharmProjects/git_action$ 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:   code.txt

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

4.4 撤銷修改

  • 繼續 4.3 節的操作

  • 如果要撤銷的修改 還未添加到暫存區

    • 可以使用 git checkout -- <file_name> 來丟棄工作區的改動。

    • 執行該操作會撤銷 該文件未添加到暫存區的修改

  • 如果要撤銷的修改 已經添加到暫存區

    • 限制性 git reset HEAD <file_name> 取消暫存

    • 再使用 git checkout -- <file_name> 來丟棄工作區的改動。

    • 執行該操作會撤銷 該文件未添加到暫存區的修改

  • 如果要撤銷的修改 已經到創建了版本記錄, 即commit到了對應的分支

    • 使用之前提到的 git reset --hard <版本號>, 回退到之前版本
[fuli@23:09:10]~/PycharmProjects/git_action$ 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:   code.txt
no changes added to commit (use "git add" and/or "git commit -a")
[fuli@23:09:14]~/PycharmProjects/git_action$ git add code.txt 
[fuli@23:09:24]~/PycharmProjects/git_action$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	modified:   code.txt
[fuli@23:09:27]~/PycharmProjects/git_action$ git reset HEAD code.txt 
Unstaged changes after reset:
M	code.txt
[fuli@23:09:41]~/PycharmProjects/git_action$ 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:   code.txt
no changes added to commit (use "git add" and/or "git commit -a")
[fuli@23:09:43]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
the new line...
[fuli@23:10:11]~/PycharmProjects/git_action$ git checkout -- code.txt 
[fuli@23:10:23]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
[fuli@23:10:27]~/PycharmProjects/git_action$ git status
On branch master
nothing to commit, working tree clean

4.5 對比文件的不同

  • 用法 git diff [<options>] [<commit> [<commit>]] [--] [<path>...]

  • 對比工作區 和 版本中文件的不同

    • git diff HEAD -- <file_name>

    • 例: 繼續編輯code.txt ,添加一行,

    • 對比工作區中 code.txtHEAD版本中 code.txt 的不同,執行 git diff HEAD -- code.txt

  • 對比 兩個版本中 文件的不同

    • git diff HEAD HEAD^ -- <file_name>

    • 執行 git diff HEAD HEAD^ -- code.txt

[fuli@23:18:25]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...
[fuli@23:18:29]~/PycharmProjects/git_action$ vim code.txt 
[fuli@23:19:08]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...

neeeeeew line...................
[fuli@23:19:17]~/PycharmProjects/git_action$ git diff HEAD -- code.txt 
diff --git a/code.txt b/code.txt
index 2e58d97..17c0853 100644
--- a/code.txt
+++ b/code.txt
@@ -5,3 +5,5 @@ This is the second line.
 This is the third line....
 
 This is the fourth line...
+
+neeeeeew line...................

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-IptPtDnd-1590760213651)(./img/WechatIMG98.png)]

  • 對比工作區 和 版本中文件的不同

4.6 刪除文件

  • 無論是創建文件、編輯文件 或者是 刪除文件,都是對工作區的改動。

    • 比如刪除文件 code2.txt,如果想撤銷,可以用撤銷工作區的改動命令 git checkout -- <file_name>

    • 如果就是要 刪除文件 code2.txt, 執行 rm code2.txt 物理刪除,然後執行git add code2.txt 或者 git rm code2.txt將修改保存到暫存區,再git commit -m "版本註釋" 創建新的版本。

  • 命令 git rm <file_name> 用於刪除一個文件。 如果一個文件已經被提交到版本庫,那麼永遠不用擔心誤刪。但是 仍要小心,因爲只能恢復文件到最新版本,這樣會丟失刪除文件之後所有的版本修改。

  • 如果文件沒有被提交到版本庫,誤刪就找不回來了,除非用一些掃描磁盤的工具,但也不一定找得回。

Git 分支管理

1. 概念

  • 分支在實際中有什麼用呢?

    • 如果只有一個master 分支,假設我們準備開發一個新功能,但是需要兩週才能完成,第一週寫了50%的代碼,若立刻提交,由於代碼未寫完,不完整的代碼庫會導致別人無法幹活。

    • 如果等代碼全部寫完再一次提交,又存在丟失每天工作進度的巨大風險。

    • 現在有了分支,就不用怕了。 可以創建一個屬於自己的分支,別人看不到,還繼續再原來的分支上正常工作,而我們再自己的分支上幹活,想提交到自己的分支就提交,直到開發完畢後,再一次性合併到原來的分支上,這樣既安全,又不影響別人工作。

2. 創建與合併分支

  • 默認只有一個 master 分支,HEAD 指向 分支,分支指向分支上最新的一次版本。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-gUC4CE1Q-1590760377838)(./img/WechatIMG100.png)]


  • 案例

      1. git branch 查看當前所有的分支,以及(HEAD)當前在哪個分支下工作。
      1. 執行 git checkout -b dev,創建一個dev 分支,並切換到其上進行工作。 這兩步完成後的 工作樹如下

    [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-etwyw3LH-1590760377839)(./img/WechatIMG102.jpeg)]

      1. 在code.txt 新增一行,然後 在 dev 分支下 git addgit commit 創建版本. 該步完成後 工作樹如下
    • [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-pVXFR63z-1590760377842)(./img/WechatIMG103.png)]

      1. 執行 git checkout master, 切換回 master 分支
      1. master分支下 執行 git merge dev 合併 dev 分支。此時是Fast-forward (快速合併) 方式的合併。
      1. 刪除dev 分支,執行 git branch -d dev
[fuli@23:41:35]~/PycharmProjects/git_action$ git branch
* master
[fuli@18:12:12]~/PycharmProjects/git_action$ git checkout -b dev
Switched to a new branch 'dev'
[fuli@18:12:21]~/PycharmProjects/git_action$ git branch
* dev
  master
[fuli@18:12:30]~/PycharmProjects/git_action$ git log --pretty=oneline
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (HEAD -> dev, master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:44:48]~/PycharmProjects/git_action$ ls
code.txt  code2.txt
[fuli@18:47:08]~/PycharmProjects/git_action$ vim code.txt 
[fuli@18:48:00]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...

[fuli@18:48:04]~/PycharmProjects/git_action$ git add code.txt 
[fuli@18:48:13]~/PycharmProjects/git_action$ git commit -m "dev分支提交"
[dev 2e2afc6] dev分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+), 3 deletions(-)
[fuli@18:48:29]~/PycharmProjects/git_action$ git log
commit 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> dev)
Author: Fuli <[email protected]>
Date:   Sun Apr 19 18:48:29 2020 +0800

    dev分支提交

commit 7e9087bcb1a8c8a0acdb4e1a418239160db0584b (master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 22:55:41 2020 +0800

    版本4

commit aa4e0e8732d92d5b46c6a0db37e05442ed0d84da
Author: Fuli <[email protected]>
Date:   Fri Apr 17 20:32:17 2020 +0800

    版本3

commit bbf036c780245dffa56052047d6b0d1bbae939d0
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@18:48:37]~/PycharmProjects/git_action$ git log --pretty=oneline
2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> dev) dev分支提交
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:50:19]~/PycharmProjects/git_action$ git checkout master
Switched to branch 'master'
[fuli@18:53:46]~/PycharmProjects/git_action$ git log --pretty=oneline
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (HEAD -> master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:53:56]~/PycharmProjects/git_action$ git branch
  dev
* master
[fuli@18:54:08]~/PycharmProjects/git_action$ git merge dev
Updating 7e9087b..2e2afc6
Fast-forward
 code.txt | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
[fuli@18:56:30]~/PycharmProjects/git_action$ git log --pretty=oneline
2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> master, dev) dev分支提交
7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01

[fuli@18:58:49]~/PycharmProjects/git_action$ git branch -d dev
Deleted branch dev (was 2e2afc6).
[fuli@18:59:32]~/PycharmProjects/git_action$ git branch
* master
小結
查看分支 git branch
創建分支 git branch <name>
切換分支 git checkout <name>
創建+切換分支 git checkout -b <name>
合併某分支到當前分支 git merge <name>
刪除分支 git branch -d <name>

3. 解決衝突

  • masterdev2(或者任意兩個欲合併的分支)起衝突,需要手動修改 衝突對應的文件, 並進行一次新的 git addgit commit 創建新的版本,即可使兩個分支合併,並解決衝突。

  • git status 也可以提示 衝突對應的文件

  • 解決 Conflict之後就可以刪除 dev2分支了

[fuli@19:20:03]~/PycharmProjects/git_action$ git branch
* master
[fuli@19:20:15]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...

[fuli@19:20:34]~/PycharmProjects/git_action$ git branch dev2
[fuli@19:20:50]~/PycharmProjects/git_action$ git checkout dev2
Switched to branch 'dev2'
[fuli@19:20:59]~/PycharmProjects/git_action$ git branch
* dev2
  master
[fuli@19:21:07]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:22:17]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
this is dev2 branch add: dev2.....
[fuli@19:22:58]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:23:05]~/PycharmProjects/git_action$ git commit -m "dev2分支提交"
[dev2 3bc40fe] dev2分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[fuli@19:23:19]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
* 3bc40fe5ffd2f542bab9f6988d58570bee271454 (HEAD -> dev2) dev2分支提交
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (master) dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:23:34]~/PycharmProjects/git_action$ git checkout master
Switched to branch 'master'
[fuli@19:23:50]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:25:21]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
master branch add: master......
[fuli@19:25:25]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:25:33]~/PycharmProjects/git_action$ git commit -m "master分支提交"
[master a5027ad] master分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[fuli@19:25:47]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
* a5027ad7feb05c133c237f0098d1645a8d2a27a9 (HEAD -> master) master分支提交
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:25:53]~/PycharmProjects/git_action$ git merge dev2
Auto-merging code.txt
CONFLICT (content): Merge conflict in code.txt
Automatic merge failed; fix conflicts and then commit the result.
[fuli@19:26:05]~/PycharmProjects/git_action$ git status
On branch master
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:   code.txt

no changes added to commit (use "git add" and/or "git commit -a")
[fuli@19:26:12]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:26:47]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
master branch add: master......
this is dev2 branch add: dev2.....
[fuli@19:26:54]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:27:03]~/PycharmProjects/git_action$ git commit -m "解決衝突Conflict"
[master 455c320] 解決衝突Conflict
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

[fuli@19:27:30]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
*   455c32056f8f3b7aa87ff087036adb4578aa1f10 (HEAD -> master) 解決衝突Conflict
|\  
| * 3bc40fe5ffd2f542bab9f6988d58570bee271454 (dev2) dev2分支提交
* | a5027ad7feb05c133c237f0098d1645a8d2a27a9 master分支提交
|/  
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:27:40]~/PycharmProjects/git_action$ git branch -d dev2
Deleted branch dev2 (was 3bc40fe).

4. 分支管理策略

通常合併分支時,如果可能,git 會採用 Fast-forward 模式。
但是有時候 雖然沒有衝突,但也不可以使用 快速合併,這時git(系統)會在合併之後做一次新的版本提交。
在這種模式下,刪除分支會丟掉分支信息。

重點

強制禁用 Fast-forward 模式,gitmerge時會生成一個新的 commit,這樣,從分支歷史上就可以看出分支信息。
git merge --no-ff -m "禁用Fast-forward合併" dev . 因爲本次合併要創建一個新的commit,所以加上-m 參數,把commit 描述寫進去。

git stash 保存工作現場

git stash list 列出工作現場

git stash pop 恢復工作現場

Github 的使用


GitHub使用

1. 創建倉庫 Repository

  • .gitignore 標記git不會跟蹤的文件類型

2. 添加 ssh 賬戶

  • 若要使某臺機器與GitHub上的Repository 交互,需要將某個pc端的 ssh公鑰 添加到對應的Github賬戶上

第一步:創建SSH

打開終端(terminal)檢測是否存在ssh:

$cd ~/.ssh

注:若提 -bash: cd: ~/.ssh: No such file or directory 那就說明.ssh文件夾不存在。

  1. 如果已存在,先將已有的ssh備份,或者將新建的ssh生成到另外的目錄下。
  2. 如果不存在,通過默認的參數直接生成ssh。輸入以下命令來創建ssh:
$ ssh-keygen -t rsa -C [email protected]

注:[email protected] 爲你註冊GitHub時的郵箱賬號。
命令執行成功:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fuli/.ssh/id_rsa):    // .ssh默認路徑,不輸入則不修改 
Enter passphrase (empty for no passphrase):       // 密碼長度至少爲4  
Enter same passphrase again
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mS+RjTY9yt5uRz/spUP/lBIE0Go9Xv/o+ODEKbS31W4 xxxxx@xx.com
The key's randomart image is:
+---[RSA 2048]----+
|         .o.     |
|           ..    |
|          o  .   |
|         X o..   |
|        S * o..  |
|       o * =..oo.|
|        + +.*=.+=|
|       . o.=.=B=E|
|        .oo.++=++|
+----[SHA256]-----+

SSH創建成功。

第二步:登陸GitHub 配置 SSH keys

登陸GitHub,選擇Settings-->SSH Keys

Title:[email protected]

Key:打開你生成的id_rsa.pub文件,將其中內容拷貝至此。

注:

  1. 查找id_rsa.pub文件,打開Finder,shift+command+g 輸入設置的.ssh的路徑(默認路徑:~/.ssh)。

  2. id_rsa.pub文件可用文本編輯打開。

最後:打開終端

$ ssh -T [email protected]

測試一下你的帳號跟github連上沒有, 如果出現如下提示,表示你連已經連上了.

Hi your GitHub's username! You've successfully authenticated, but GitHub does not provide shell access.

接下來就可以上傳你的代碼了。

3. 克隆項目

  • git clone <one github repo address>

4. 推送分支

  • 克隆到本地的項目一般會建一個分支進行開發,若開發完成,需要將分支推送到GitHub,執行git push origin fuli_dev,其中 origin 代表遠程的分支(這裏即GitHub),fuli_dev 是本地分支。

  • 若是第一次推送,執行上述命令之後,git會在 GitHub對應的Repository 中新建一個 fuli_dev 分支。

5. 跟蹤遠程

  • 本地的分支跟蹤遠程的分支, git branch --set-upstream-to=origin/遠程分支名稱 本地分支名稱

    • 例如:git branch --set-upstream-to=origin/fuli_dev fuli_dev

    • 如果本地的提交版本 和 遠程的不一致的話,git status會有相應提示。

6. 拉取代碼

  • git pull origin <遠程分支名dev_mac> 將遠程的分支 dev_mac 上的代碼下載並 合併 到本地所在的分支。

    • 注意 這裏是 下載 並 合併

小結

項目一般包括兩個主要分支:MasterDev

Master : 保存發佈的項目版本代碼。 V1.0, V2.0 …

Dev : 保存開發過程中的代碼,每一個組員開發完自己的代碼之後,都需要將代碼發佈到遠程的Dev分支上。


關於.gitignore更新後不生效的方法

由於是後期對.gitignore 添加的內容的緣故

昨天能用pycharm寫代碼,發現venv文件夾更新了,
但是不想commit這些環境配置文件,就在.gitignore裏面加了venv/但是發現並不起作用,
於是就google了一下,發現得在terminal中輸入已下幾行代碼

例如,後期添加了 venv/,若要使添加內容生效,需要先 清除venv/git緩存。
git rm -r --cached venv
git add .
git commit -m 'update .gitignore'

搞定,再commit的時候.gitignore文件已經生效。

Reference
Reference2

Mac系統GitHub上傳項目

  • 說到GitHub相信大家都不陌生,這裏就不再贅述了。作爲開源代碼庫以及版本控制系統,使用好了會非常受益。
  • 經常維護自己的技術博客和GitHub,在你找工作時也是加分項喲。

一、準備工作:

  1. 註冊GitHub賬號。https://github.com

  2. 安裝Git客戶端。https://git-scm.com/downloads

二、準備工作做好以後,打開終端(terminal)可以查看Git版本

$ git version
git version 2.14.1

第一步:創建SSH

打開終端(terminal)檢測是否存在ssh:

$cd ~/.ssh

注:若提 -bash: cd: ~/.ssh: No such file or directory 那就說明.ssh文件夾不存在。

  1. 如果已存在,先將已有的ssh備份,或者將新建的ssh生成到另外的目錄下。
  2. 如果不存在,通過默認的參數直接生成ssh。輸入以下命令來創建ssh:
$ ssh-keygen -t rsa -C [email protected]

注:[email protected] 爲你註冊GitHub時的郵箱賬號。
命令執行成功:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/clyde/.ssh/id_rsa):    // .ssh默認路徑,不輸入則不修改 
Enter passphrase (empty for no passphrase):       // 密碼長度至少爲4  
Enter same passphrase again
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mS+RjTY9yt5uRz/spUP/lBIE0Go9Xv/o+ODEKbS31W4 xxxxx@xx.com
The key's randomart image is:
+---[RSA 2048]----+
|         .o.     |
|           ..    |
|          o  .   |
|         X o..   |
|        S * o..  |
|       o * =..oo.|
|        + +.*=.+=|
|       . o.=.=B=E|
|        .oo.++=++|
+----[SHA256]-----+

SSH創建成功。

第二步:登陸GitHub 配置 SSH keys

登陸GitHub,選擇Settings-->SSH Keys

Title:[email protected]

Key:打開你生成的id_rsa.pub文件,將其中內容拷貝至此。

注:

  1. 查找id_rsa.pub文件,打開Finder,shift+command+g 輸入設置的.ssh的路徑(默認路徑:~/.ssh)。

  2. id_rsa.pub文件可用文本編輯打開。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-hT6AYTBv-1590760879616)(attachment:image.png)]

最後:打開終端

$ ssh -T [email protected]

測試一下你的帳號跟github連上沒有, 如果出現如下提示,表示你連已經連上了.

Hi your GitHub's username! You've successfully authenticated, but GitHub does not provide shell access.

接下來就可以上傳你的代碼了。

第三步:在GitHub新建自己的Repository,並關聯到本地

一、登錄GitHub賬號,新建Repository

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-WcvuNxRl-1590760879619)(attachment:image.png)]

  • 點擊Create repository 進入下面界面
    在這裏插入圖片描述

二、關聯本地文件

打開終端(terminal),cd到所傳文件夾中,然後執行

git init   //初始化本地倉庫
git remote add origin [email protected]:xxxxxx/CCTestDemo.git  //連接遠程倉庫並建了一個名叫:origin的別名,當然可以爲其他名字,
git pull origin master  //先從遠程pull一次文件  以免提交報錯
git add -A   //文件  (git add -A 爲添加該文件夾所有文件)
git commit -m "你的註釋"  //提交到本地倉庫,並寫一些註釋
git push origin master   //將本地倉庫合併到別名爲origin地址的master分支

如果中間不出什麼問題就大功告成了!!! 刷新GitHub頁面就可以看到了。
注:

  1. 如果git remote add origin這一步出現origin exitss, 輸入git remote rm origin,再次執行上面那條語句。

  2. 如果出現 failed to push some refs to git錯誤,可以通過如下命令進行代碼合併 git pull --rebase origin master

  3. 切記上傳文件時,一定要先commit到本地倉庫,才能進行push提交,否則會顯示Everything up-to-date(意思就是目前的遠程倉庫的內容跟本地倉庫對比後,沒有做修改,是最新的。

  4. 要關聯一個遠程庫,使用命令git remote add origin git@server-name:path/repo-name.git 關聯後,使用命令git push -u origin master 第一次推送master分支的所有內容;

此後,每次本地提交後,只要有必要,就可以使用命令git push origin master推送最新修改。

Reference

Git 拉去Github某個分支

方法一:直接獲取

  • 首先新建個文件夾,右鍵打開Git Bash

在這裏插入圖片描述

  • Git Bash中直接輸入指令:git clone -b dev 代碼倉庫地址dev是分支名稱)
    在這裏插入圖片描述
  • 拉取完畢

方法二:先關聯遠程倉庫

  • 打開Git Bash
  • Git Bash 中輸入 git init進行初始化
  • 與遠程代碼倉庫建立連接:git remote add origin 代碼倉庫地址 –關聯遠程倉庫

建立連接

  • 將遠程分支拉到本地:git fetch origin devdev即分支名)
    拉取分支

  • 創建本地分支:git checkout -b LocalDev origin/dev (LocalDev 爲本地分支名,dev爲遠程分支名)

創建分支

  • 根據分支的變化,感覺這條指令可能是創建並切換到該分支
  • 最後一步將遠程分支拉取到本地:git pull origin devdev爲遠程分支名)
    拉取成功

方法三

  • 打開Git Bash

  • 輸入 git clone 代碼倉庫地址

  • 進入文件夾中 命令:cd XXX(文件夾名)

  • 繼續輸入指令 git submodule init

  • 最後 git submodule update

  • 至此結束

Reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章