一篇極好的Git 總結

常用命令

簡單的,常用的命令也就幾個。但是想非常熟練使用,怕是要記住幾十個。

img

  • Workspace:工作區(clone或者原始內容)
  • Index/Stage:暫存區(有增刪改查後add到臨時區)
  • Repository:本地倉庫(保存了本地的增刪改查記錄)
  • Remote:遠程倉庫(git.code.oa.com,本地的記錄提交到遠端,供團隊所有人查看使用)

有意思的事

代碼更新之Fetch vs Pull

二者都是從遠程拉取代碼到本地

  • fetch:只是拉取到本地
  • pull:不僅拉取到本地,還merge到本地分支中

代碼合流之Merge vs Rebase

  • rebase:用於把一個分支的修改合併到當前分支

img

假設遠程分支上有2個提交,然後基於遠程develop,再創建一個分支feature。

然後分別在兩個分支上做兩次提交。

img

git merge

這時候,你可以用pull命令把develop分支上的修改拉下來並且和你的修改合併;結果看起來就像一個新的和並提交

img

git rebase

$ git checkout feature
$ git rebase develop
  1. 這些命令會把你的feature分支裏的每個提交(commit)取消掉,並且把它們臨時保存爲補丁(patch)(這些補丁放到".git/rebase"目錄中)
  2. 然後把feature分支更新爲最新的develop分支
  3. 最後把保存的這些補丁應用到feature分支上
  4. 當feature分支更新後,會指向最新的commit,臨時存放的就會被刪除掉

img

代碼回滾之Reset、Revert、 Checkout

img

  • Reset

將一個分支的末端指向另一個提交,可以用來移除當前分支的一些提交。

文件層面上,將緩存區的文件同步到指定的那個提交。

$ git checkout develop
$ git reset HEAD~2

# 將當前的README.md從緩存區中移除出去
$ git reset HEAD README.md

img

develop分支末端的兩個提交就變成了懸掛提交

如果提交還沒有push,git reset 是撤銷commit的簡單方法

除了在當前分支上操作,還可以通過其他參數來修改stage或者workspace

  1. --soft:stage和workspace都不會被改變
  2. --mixed(默認):stage和你指定的提交同步,但workspace不受影響
  3. --hard:stage和workspace都同步到你指定的提交
# 將當前的改動從stage中移除,但這些改動還保留在workspace中
$ git reset --mixed HEAD

# 完全捨棄沒有提交的改動
$ git reset --hard HEAD
  • Checkout

提交層面上的checkout,可以切換分支,同一分支,可以切換當前HEAD。

文件層面上,不會移動HEAD指針,也不會切換到其他分支上,只是更改workspace,而不是stage。

# 將HEAD移到新的分支,然後更新工作目錄
$ git checkout develop

# 將HEAD移動到當前commit的前兩個commit上,同時更新workspace
$ git checkout HEAD~2

# 將workspace中的README.md同步到最新的提交
$ git checkout HEAD README.md
  • Revert

撤銷一個提交的同時會創建一個新的提交。

# 產生一個新的commit用於撤銷倒數第二個commit
$ git checkout develop
$ git revert HEAD~2
  1. 不會改變提交歷史
  2. revert可以用在公共分支,reset應該用在私有分支上
  3. 如果提交已經push,想到達到撤銷的目的,應該使用revert
命令 作用域 常用情景
git reset 提交層面 在私有分支上舍棄一些沒有提交的更改
git reset 文件層面 將文件從緩存區中移除
git checkout 提交層面 切換分支或查看舊版本
git checkout 文件層面 捨棄工作目錄中的更改
git revert 提交層面 在公共分支上回滾更改
git revert 文件層面 (然而並沒有)

代碼暫存之Stash

git stash會把所有未提交的修改(包括暫存和未暫存的)都保存起來,用於日後恢復當前工作目錄

  • 保存一個不必要但日後又想查看的提交
  • 切換分支前先暫存,處理分支的其他事情
$ git status
On branch develop
Changes to be committed:

new file:   README.md

Changes not staged for commit:

modified:   index.html

$ git stash
Saved working directory and index state WIP on master: 5002d47 ...

$ git status
On branch master
nothing to commit, working tree clean

stage是本地的,不會上傳到git server

實際應用中,推薦給每個stash加一個message,使用git stash save 取代 git stash

$ git stash save "test stash"
Saved working directory and index state On autoswitch: test stash
HEAD 現在位於 296e8d4 remove unnecessary postion reset in onResume function
$ git stash list
stash@{0}: On autoswitch: test stash
  1. 可以使用git stash list命令,查看stash列表
$ git stash list
stash@{0}: WIP on master: 049d078 stash_0
stash@{1}: WIP on master: c264051 stash_1
stash@{2}: WIP on master: 21d80a5 stash_2

\2. 使用git stash apply命令可以通過名字指定那個stash,默認指定最近的(stash@{0})

\3. 使用git stash pop將stash中第一個stash刪除,並將對應修改應用到當前的工作目錄中

\4. 使用git stash drop,後面加上stash名,可以移除相應的stash;或者使用git stash clear清空所有stash

默認情況下,git stash會緩存:

  • 添加到暫存區的修改(staged changes )
  • Git跟蹤但並未添加到暫存區的修改(unstaged changes)

但不會緩存:

  • 在工作目錄中新的文件(untracked files)
  • 被忽略的文件(ignored files)

此時,使用-u或者--include-untracked可以stash untracked 文件;使用-a或者--all可以stash當前目錄下的所有修改(慎用

基礎命令

初始化

# 在當前目錄新建一個Git代碼庫
$ git init

# 新建一個目錄,將其初始化爲Git代碼庫
$ git init git_test

# 下載一個項目和它的整個代碼歷史
$ git clone http://git.code.oa.com/jaelintu/git_test

增加/刪除文件

# 添加指定文件到暫存區
$ git add file1 file2...

# 添加指定目錄到暫存區,包括子目錄
$ git add dir

# 添加當前目錄的所有文件到暫存區
$ git add .

# 添加每個變化前,都會要求確認
# 對於同一個文件的多處變化,可以實現分次提交
$ git add -p

# 刪除工作區文件,並且將這次刪除放入暫存區
$ git rm file1 file2 ...

代碼提交

# 提交暫存區到倉庫區
$ git commit -m "message"

# 提交暫存區的指定文件到倉庫區
$ git commit file1 file2 ... -m "message"

# 提交工作區自上次commit之後的變化,直接到倉庫區
$ git commit -a

# 提交時顯示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代碼沒有任何新變化,則用來改寫上一次commit的提交信息
$ git commit --amend -m "message"

# 重做上一次commit,幷包括指定文件的新變化
$ git commit --amend file1 file2 ...

分支

# 列出所有本地分支
$ git branch

# 列出所有遠程分支
$ git branch -r

# 列出所有本地分支和遠程分支
$ git branch -a

# 新建一個分支,但依然停留在當前分支
$ git branch name

# 新建一個分支,並切換到該分支
$ git checkout -b branch

# 新建一個分支,指向指定commit
$ git branch name commit_SHA

# 新建一個分支,與指定的遠程分支建立追蹤關係
$ git branch --track name orgin/name

# 切換到指定分支,並更新工作區
$ git checkout name

# 切換到上一個分支
$ git checkout -

# 建立追蹤關係,在現有分支與指定的遠程分支之間
$ git branch --set-upstream name origin/name

# 合併指定分支到當前分支
$ git merge branch-name

# 選擇一個commit,合併進當前分支
$ git cherry-pick commit_SHA

# 刪除分支
$ git branch -d branch-name

# 刪除遠程分支
$ git push origin --delete branch-name
$ git branch -dr remote/branch

tags

# 列出所有tag
$ git tag

# 新建一個tag在當前commit
$ git tag tag-name

# 新建一個tag在指定commit
$ git tag tag-name commit-SHA

# 刪除本地tag
$ git tag -d tag-name

# 刪除遠程tag
$ git push origin :refs/tags/tag-Name

# 查看tag信息
$ git show tag-name

# 提交指定tag
$ git push origin tag-name

# 提交所有tag
$ git push origin --tags

# 新建一個分支,指向某個tag
$ git checkout -b branch-name tag-name

查看信息

# 顯示有變更的文件
$ git status

# 顯示當前分支的版本歷史
$ git log

# 顯示commit歷史,以及每次commit發生變更的文件
$ git log --stat

# 搜索提交歷史,根據關鍵詞
$ git log -S [keyword]

# 顯示某個commit之後的所有變動
$ git log (tag-name||commit-SHA) HEAD

# 顯示某個文件的版本歷史,包括文件改名
$ git log --follow file
$ git whatchanged file

# 顯示指定文件相關的每一次diff
$ git log -p file

# 顯示過去5次提交
$ git log -5 --pretty --oneline

# 顯示所有提交過的用戶,按提交次數排序
$ git shortlog -sn

# 顯示指定文件是什麼人在什麼時間修改過
$ git blame file

# 顯示暫存區和工作區的代碼差異
$ git diff

# 顯示暫存區和上一個commit的差異
$ git diff --cached file

# 顯示工作區與當前分支最新commit之間的差異
$ git diff HEAD

# 顯示兩次提交之間的差異
$ git diff [first-branch]...[second-branch]

# 顯示今天你寫了多少行代碼
$ git diff --shortstat "@{0 day ago}"

# 顯示某次提交的元數據和內容變化
$ git show commit-SHA

# 顯示某次提交發生變化的文件
$ git show --name-only commit-SHA

# 顯示某次提交時,某個文件的內容
$ git show commit-SHA:filename

# 顯示當前分支的最近幾次提交
$ git reflog

# 從本地master拉取代碼更新當前分支:branch 一般爲master
$ git rebase branch-name

遠程同步

# 下載遠程倉庫的所有變動
$ git fetch origin

# 顯示所有遠程倉庫
$ git remote -v

# 顯示某個遠程倉庫的信息
$ git remote show origin

# 增加一個新的遠程倉庫,並命名
$ git remote add shortname url

# 取回遠程倉庫的變化,並與本地分支合併
$ git pull origin branch-name

# 上傳本地指定分支到遠程倉庫
$ git push origin branch-name

# 強行推送當前分支到遠程倉庫,即使有衝突
$ git push origin --force

# 推送所有分支到遠程倉庫
$ git push origin --all

撤銷

# 恢復暫存區的指定文件到工作區
$ git checkout file

# 恢復某個commit的指定文件到暫存區和工作區
$ git checkout commit-SHA file

# 恢復暫存區的所有文件到工作區
$ git checkout .

# 重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
$ git reset file

# 重置暫存區與工作區,與上一次commit保持一致
$ git reset --hard

# 重置當前分支的指針爲指定commit,同時重置暫存區,但工作區不變
$ git reset commit-SHA

# 重置當前分支的HEAD爲指定commit,同時重置暫存區和工作區,與指定commit一致
$ git reset --hard commit-SHA

# 重置當前HEAD爲指定commit,但保持暫存區和工作區不變
$ git reset --keep commit-SHA

# 新建一個commit,用來撤銷指定commit
# 後者的所有變化都將被前者抵消,並且應用到當前分支
$ git revert commit-SHA

# 暫時將未提交的變化移除,稍後再移入
$ git stash
$ git stash pop

衝突解決

rebase過程中,也許會出現衝突(conflict)

  • git會停止rebase,需要解決衝突
  • 解決完,使用git add添加衝突的文件,更新暫存區
  • git rebase --continue繼續剩下的rebase
  • git rebase --abort終止rebase行爲,並且feature會回到rebase開始之前的狀態
$ git rebase develop
CONFLICT (content): Rebase conflict in readme.txt
Automatic rebase failed; fix conflicts and then commit the result.

$ git status
On branch feature

You have unmerged paths.
  (fix conflicts and run "git rebase --continue")
  (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.md 內容

Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature

選擇保留HEAD或者feature的版本

Git tracks changes of files.
Creating a new branch is quick AND simple.

在提交:

$ git add readme.md
$ git rebase --contine

推薦的Git GUI工具

\1. Source Tree(號稱最好用):特色支持git flow,一鍵創建工作流

  • 免費功能
  • 強大:無論你是新手還是重度用戶,SourceTree 都會讓你覺得很順手。對於非常重度用戶,Source Tree還支持自定義腳本的執行
  • 同時支持 Windows 和 Mac 操作系統
  • 同時支持 Git 和 Mercurial 兩種 VCS
  • 內置GitHub, BitBucket 和 Stash 的支持:直接綁定帳號即可操作遠程repo

    \2. Tortoise git:文件的右鍵菜單很容易上手

  • 免費
  • 只支持Windows:與文件管理器良好集成
  • 中文界面
  • 與Tortoise SVN相同的體驗

此文已由作者授權騰訊雲+社區發佈,更多原文請點擊

搜索關注公衆號「雲加社區」,第一時間獲取技術乾貨,關注後回覆1024 送你一份技術課程大禮包!

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