45 個 Git 經典操作場景,專治不會合代碼
git 對於大家應該都不太陌生,熟練使用 git 已經成爲程序員的一項基本技能,儘管在工作中有諸如
Sourcetree
這樣牛 X 的客戶端工具,使得合併代碼變的很方便。但找工作面試和一些需彰顯個人實力的場景,仍然需要我們掌握足夠多的 git 命令。
下邊我們整理了 45 個日常用 git 合代碼的經典操作場景,基本覆蓋了工作中的需求。
我剛纔提交了什麼?
如果你用
git commit -a
提交了一次變化 (changes),而你又不確定到底這次提交了哪些內容。你就可以用下面的命令顯示當前HEAD
上的最近一次的提交 (commit):
(main)$ git show
或者
$ git log -n1 -p
我的提交信息 (commit message) 寫錯了
如果你的提交信息 (commit message) 寫錯了且這次提交 (commit) 還沒有推(push), 你可以通過下面的方法來修改提交信息(commit message):
$ git commit --amend --only
這會打開你的默認編輯器, 在這裏你可以編輯信息. 另一方面, 你也可以用一條命令一次完成:
$ git commit --amend --only -m 'xxxxxxx'
如果你已經推 (push) 了這次提交 (commit), 你可以修改這次提交(commit) 然後強推(force push), 但是不推薦這麼做。
我提交 (commit) 裏的用戶名和郵箱不對
如果這只是單個提交 (commit),修改它:
$ git commit --amend --author "New Authorname <authoremail@mydomain.com>"
如果你需要修改所有歷史, 參考'git filter-branch'的指南頁.
我想從一個提交 (commit) 裏移除一個文件
通過下面的方法,從一個提交 (commit) 裏移除一個文件:
$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amend
這將非常有用,當你有一個開放的補丁 (open patch),你往上面提交了一個不必要的文件,你需要強推(force push) 去更新這個遠程補丁。
我想刪除我的的最後一次提交 (commit)
如果你需要刪除推了的提交 (pushed commits),你可以使用下面的方法。可是,這會不可逆的改變你的歷史,也會搞亂那些已經從該倉庫拉取(pulled) 了的人的歷史。簡而言之,如果你不是很確定,千萬不要這麼做。
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]
如果你還沒有推到遠程, 把 Git 重置 (reset) 到你最後一次提交前的狀態就可以了(同時保存暫存的變化):
(my-branch*)$ git reset --soft HEAD@{1}
這隻能在沒有推送之前有用. 如果你已經推了, 唯一安全能做的是
git revert SHAofBadCommit
, 那會創建一個新的提交 (commit) 用於撤消前一個提交的所有變化(changes);或者, 如果你推的這個分支是 rebase-safe 的 (例如:其它開發者不會從這個分支拉), 只需要使用git push -f
。
刪除任意提交 (commit)
同樣的警告:不到萬不得已的時候不要這麼做.
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]
或者做一個 交互式 rebase 刪除那些你想要刪除的提交 (commit) 裏所對應的行。
我嘗試推一個修正後的提交 (amended commit) 到遠程,但是報錯:
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
注意, rebasing(見下面)和修正 (amending) 會用一個新的提交 (commit) 代替舊的, 所以如果之前你已經往遠程倉庫上推過一次修正前的提交 (commit),那你現在就必須強推 (force push) (
-f
)。注意 – 總是 確保你指明一個分支!
(my-branch)$ git push origin mybranch -f
一般來說, 要避免強推. 最好是創建和推 (push) 一個新的提交(commit),而不是強推一個修正後的提交。後者會使那些與該分支或該分支的子分支工作的開發者,在源歷史中產生衝突。
我意外的做了一次硬重置 (hard reset),我想找回我的內容
如果你意外的做了
git reset --hard
, 你通常能找回你的提交 (commit), 因爲 Git 對每件事都會有日誌,且都會保存幾天。
(main)$ git reflog
你將會看到一個你過去提交 (commit) 的列表, 和一個重置的提交。選擇你想要回到的提交 (commit) 的 SHA,再重置一次:
(main)$ git reset --hard SHA1234
這樣就完成了。
暫存 (Staging)
我需要把暫存的內容添加到上一次的提交 (commit)
(my-branch*)$ git commit --amend
我想要暫存一個新文件的一部分,而不是這個文件的全部
一般來說, 如果你想暫存一個文件的一部分, 你可這樣做:
$ git add --patch filename.x
-p
簡寫。這會打開交互模式, 你將能夠用 s
選項來分隔提交 (commit);然而, 如果這個文件是新的, 會沒有這個選擇, 添加一個新文件時, 這樣做:
$ git add -N filename.x
然後, 你需要用 e
選項來手動選擇需要添加的行,執行 git diff --cached
將會顯示哪些行暫存了哪些行只是保存在本地了。
我想把在一個文件裏的變化 (changes) 加到兩個提交 (commit) 裏
git add
會把整個文件加入到一個提交. git add -p
允許交互式的選擇你想要提交的部分.
我想把暫存的內容變成未暫存,把未暫存的內容暫存起來
多數情況下,你應該將所有的內容變爲未暫存,然後再選擇你想要的內容進行 commit。但假定你就是想要這麼做,這裏你可以創建一個臨時的 commit 來保存你已暫存的內容,然後暫存你的未暫存的內容並進行 stash。然後 reset 最後一個 commit 將原本暫存的內容變爲未暫存,最後 stash pop 回來。
$ git commit -m "WIP"
$ git add .
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
注意 1: 這裏使用
pop
僅僅是因爲想盡可能保持冪等。注意 2: 假如你不加上--index
你會把暫存的文件標記爲爲存儲。
未暫存 (Unstaged) 的內容
我想把未暫存的內容移動到一個新分支
$ git checkout -b my-branch
我想把未暫存的內容移動到另一個已存在的分支
$ git stash
$ git checkout my-branch
$ git stash pop
我想丟棄本地未提交的變化 (uncommitted changes)
如果你只是想重置源 (origin) 和你本地 (local) 之間的一些提交(commit),你可以:
# one commit
(my-branch)$ git reset --hard HEAD^
# two commits
(my-branch)$ git reset --hard HEAD^^
# four commits
(my-branch)$ git reset --hard HEAD~4
# or
(main)$ git checkout -f
重置某個特殊的文件, 你可以用文件名做爲參數:
$ git reset filename
我想丟棄某些未暫存的內容
如果你想丟棄工作拷貝中的一部分內容,而不是全部。
簽出 (checkout) 不需要的內容,保留需要的。
$ git checkout -p
# Answer y to all of the snippets you want to drop
另外一個方法是使用 stash
, Stash 所有要保留下的內容, 重置工作拷貝, 重新應用保留的部分。
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
或者, stash 你不需要的部分, 然後 stash drop。
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
分支 (Branches)
我從錯誤的分支拉取了內容,或把內容拉取到了錯誤的分支
這是另外一種使用
git reflog
情況,找到在這次錯誤拉 (pull) 之前 HEAD 的指向。
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
重置分支到你所需的提交 (desired commit):
$ git reset --hard c5bc55a
完成。
我想扔掉本地的提交 (commit),以便我的分支與遠程的保持一致
先確認你沒有推 (push) 你的內容到遠程。
git status
會顯示你領先 (ahead) 源(origin)多少個提交:
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
一種方法是:
(main)$ git reset --hard origin/my-branch
我需要提交到一個新分支,但錯誤的提交到了 main
在 main 下創建一個新分支,不切換到新分支, 仍在 main 下:
(main)$ git branch my-branch
把 main 分支重置到前一個提交:
(main)$ git reset --hard HEAD^
HEAD^
是 HEAD^1
的簡寫,你可以通過指定要設置的HEAD
來進一步重置。
或者, 如果你不想使用
HEAD^
, 找到你想重置到的提交 (commit) 的 hash(git log
能夠完成), 然後重置到這個 hash。使用git push
同步內容到遠程。
例如, main 分支想重置到的提交的 hash 爲a13b85e
:
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
簽出 (checkout) 剛纔新建的分支繼續工作:
(main)$ git checkout my-branch
我想保留來自另外一個 ref-ish 的整個文件
假設你正在做一個原型方案 (原文爲 working spike (see note)), 有成百的內容,每個都工作得很好。現在, 你提交到了一個分支,保存工作內容:
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
當你想要把它放到一個分支裏 (可能是
feature
, 或者develop
), 你關心是保持整個文件的完整,你想要一個大的提交分隔成比較小。
假設你有:
-
分支
solution
, 擁有原型方案, 領先develop
分支。 -
分支
develop
, 在這裏你應用原型方案的一些內容。
我去可以通過把內容拿到你的分支裏,來解決這個問題:
(develop)$ git checkout solution -- file1.txt
這會把這個文件內容從分支
solution
拿到分支develop
裏來:
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
然後, 正常提交。
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem.
我把幾個提交 (commit) 提交到了同一個分支,而這些提交應該分佈在不同的分支裏
假設你有一個main
分支, 執行git log
, 你看到你做過兩次提交:
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
讓我們用提交 hash(commit hash) 標記 bug (
e3851e8
for #21,5ea5173
for #14).
首先, 我們把main
分支重置到正確的提交 (a13b85e
):
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
現在, 我們對 bug #21 創建一個新的分支:
(main)$ git checkout -b 21
(21)$
接着, 我們用 cherry-pick 把對 bug #21 的提交放入當前分支。這意味着我們將應用 (apply) 這個提交(commit),僅僅這一個提交(commit),直接在 HEAD 上面。
(21)$ git cherry-pick e3851e8
這時候, 這裏可能會產生衝突, 參見交互式 rebasing 章 衝突節 解決衝突.
再者, 我們爲 bug #14 創建一個新的分支, 也基於main
分支
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
最後, 爲 bug #14 執行 cherry-pick
:
(14)$ git cherry-pick 5ea5173
我想刪除上游 (upstream) 分支被刪除了的本地分支
一旦你在 github 上面合併 (merge) 了一個 pull request, 你就可以刪除你 fork 裏被合併的分支。如果你不準備繼續在這個分支裏工作, 刪除這個分支的本地拷貝會更乾淨,使你不會陷入工作分支和一堆陳舊分支的混亂之中(IDEA 中玩轉 Git)。
$ git fetch -p
我不小心刪除了我的分支
如果你定期推送到遠程, 多數情況下應該是安全的,但有些時候還是可能刪除了還沒有推到遠程的分支。讓我們先創建一個分支和一個新的文件:
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
添加文件並做一次提交
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
現在我們切回到主 (main) 分支,‘不小心的’刪除my-branch
分支
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
在這時候你應該想起了
reflog
, 一個升級版的日誌,它存儲了倉庫 (repo) 裏面所有動作的歷史。
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
正如你所見,我們有一個來自刪除分支的提交 hash(commit hash),接下來看看是否能恢復刪除了的分支。
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
看! 我們把刪除的文件找回來了。Git 的
reflog
在 rebasing 出錯的時候也是同樣有用的。
我想刪除一個分支
刪除一個遠程分支:
(main)$ git push origin --delete my-branch
你也可以:
(main)$ git push origin :my-branch
刪除一個本地分支:
(main)$ git branch -D my-branch
我想從別人正在工作的遠程分支簽出 (checkout) 一個分支
首先, 從遠程拉取 (fetch) 所有分支:
(main)$ git fetch --all
假設你想要從遠程的daves
分支簽出到本地的daves
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
(--track
是 git checkout -b [branch] [remotename]/[branch]
的簡寫)
這樣就得到了一個
daves
分支的本地拷貝, 任何推過 (pushed) 的更新,遠程都能看到.
Rebasing 和合並 (Merging)
我想撤銷 rebase/merge
你可以合併 (merge) 或 rebase 了一個錯誤的分支, 或者完成不了一個進行中的 rebase/merge。Git 在進行危險操作的時候會把原始的 HEAD 保存在一個叫 ORIG_HEAD 的變量裏, 所以要把分支恢復到 rebase/merge 前的狀態是很容易的。
(my-branch)$ git reset --hard ORIG_HEAD
我已經 rebase 過, 但是我不想強推 (force push)
不幸的是,如果你想把這些變化 (changes) 反應到遠程分支上,你就必須得強推 (force push)。是因你快進(Fast forward) 了提交,改變了 Git 歷史, 遠程分支不會接受變化 (changes),除非強推(force push)。這就是許多人使用 merge 工作流, 而不是 rebasing 工作流的主要原因之一, 開發者的強推(force push) 會使大的團隊陷入麻煩。使用時需要注意,一種安全使用 rebase 的方法是,不要把你的變化 (changes) 反映到遠程分支上, 而是按下面的做:
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
我需要組合 (combine) 幾個提交(commit)
假設你的工作分支將會做對於
main
的 pull-request。一般情況下你不關心提交 (commit) 的時間戳,只想組合 所有 提交 (commit) 到一個單獨的裏面, 然後重置(reset) 重提交 (recommit)。確保主(main) 分支是最新的和你的變化都已經提交了, 然後:
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
如果你想要更多的控制, 想要保留時間戳, 你需要做交互式 rebase (interactive rebase):
(my-branch)$ git rebase -i main
如果沒有相對的其它分支, 你將不得不相對自己的
HEAD
進行 rebase。例如:你想組合最近的兩次提交 (commit), 你將相對於HEAD~2
進行 rebase, 組合最近 3 次提交 (commit), 相對於HEAD~3
, 等等。
(main)$ git rebase -i HEAD~2
在你執行了交互式 rebase 的命令 (interactive rebase command) 後, 你將在你的編輯器裏看到類似下面的內容:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
所有以 #
開頭的行都是註釋, 不會影響 rebase.
然後,你可以用任何上面命令列表的命令替換
pick
, 你也可以通過刪除對應的行來刪除一個提交 (commit)。
例如, 如果你想 單獨保留最舊 (first) 的提交(commit), 組合所有剩下的到第二個裏面, 你就應該編輯第二個提交 (commit) 後面的每個提交(commit) 前的單詞爲
f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
如果你想組合這些提交 (commit) 並重命名這個提交 (commit), 你應該在第二個提交 (commit) 旁邊添加一個
r
,或者更簡單的用s
替代f
:
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
你可以在接下來彈出的文本提示框裏重命名提交 (commit)。
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
如果成功了, 你應該看到類似下面的內容:
(main)$ Successfully rebased and updated refs/heads/main.
安全合併 (merging) 策略
--no-commit
執行合併 (merge) 但不自動提交, 給用戶在做提交前檢查和修改的機會。no-ff
會爲特性分支 (feature branch) 的存在過留下證據, 保持項目歷史一致(更多 Git 資料,參見 IDEA 中如何完成 Git 版本回退?)。
(main)$ git merge --no-ff --no-commit my-branch
我需要將一個分支合併成一個提交 (commit)
(main)$ git merge --squash my-branch
我只想組合 (combine) 未推的提交(unpushed commit)
有時候,在將數據推向上游之前,你有幾個正在進行的工作提交 (commit)。這時候不希望把已經推(push) 過的組合進來,因爲其他人可能已經有提交 (commit) 引用它們了。
(main)$ git rebase -i @{u}
這會產生一次交互式的 rebase(interactive rebase), 只會列出沒有推 (push) 的提交(commit), 在這個列表時進行 reorder/fix/squash 都是安全的。
檢查是否分支上的所有提交 (commit) 都合併 (merge) 過了
檢查一個分支上的所有提交 (commit) 是否都已經合併 (merge) 到了其它分支, 你應該在這些分支的 head(或任何 commits)之間做一次 diff:
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
這會告訴你在一個分支裏有而另一個分支沒有的所有提交 (commit), 和分支之間不共享的提交(commit) 的列表。另一個做法可以是:
(main)$ git log main ^feature/120-on-scroll --no-merges
交互式 rebase(interactive rebase) 可能出現的問題
這個 rebase 編輯屏幕出現'noop'
如果你看到的是這樣:
noop
這意味着你 rebase 的分支和當前分支在同一個提交 (commit) 上, 或者 領先 (ahead) 當前分支。你可以嘗試:
-
檢查確保主 (main) 分支沒有問題
-
rebase
HEAD~2
或者更早
有衝突的情況
如果你不能成功的完成 rebase, 你可能必須要解決衝突。
首先執行 git status
找出哪些文件有衝突:
(my-branch)$ git status
On branch my-branch
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.md
在這個例子裏面, README.md
有衝突。打開這個文件找到類似下面的內容:
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
你需要解決新提交的代碼 (示例裏, 從中間
==
線到new-commit
的地方) 與HEAD
之間不一樣的地方.
有時候這些合併非常複雜,你應該使用可視化的差異編輯器 (visual diff editor):
(main*)$ git mergetool -t opendiff
在你解決完所有衝突和測試過後, git add
變化了的 (changed) 文件, 然後用git rebase --continue
繼續 rebase。
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
如果在解決完所有的衝突過後,得到了與提交前一樣的結果, 可以執行
git rebase --skip
。
任何時候你想結束整個 rebase 過程,回來 rebase 前的分支狀態, 你可以做:
(my-branch)$ git rebase --abort
Stash
暫存所有改動
暫存你工作目錄下的所有改動
$ git stash
你可以使用
-u
來排除一些文件
$ git stash -u
暫存指定文件
假設你只想暫存某一個文件
$ git stash push working-directory-path/filename.ext
假設你想暫存多個文件
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
暫存時記錄消息
這樣你可以在
list
時看到它
$ git stash save <message>
或
$ git stash push -m <message>
使用某個指定暫存
首先你可以查看你的stash
記錄
$ git stash list
然後你可以apply
某個stash
$ git stash apply "stash@{n}"
此處, 'n'是stash
在棧中的位置,最上層的stash
會是 0
除此之外,也可以使用時間標記 (假如你能記得的話)。
$ git stash apply "stash@{2.hours.ago}"
暫存時保留未暫存的內容
你需要手動 create 一個
stash commit
, 然後使用git stash store
。
$ git stash create
$ git stash store -m "commit-message" CREATED_SHA1
雜項 (Miscellaneous Objects)
克隆所有子模塊
$ git clone --recursive git://github.com/foo/bar.git
如果已經克隆了:
$ git submodule update --init --recursive
刪除標籤 (tag)
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
恢復已刪除標籤 (tag)
如果你想恢復一個已刪除標籤 (tag), 可以按照下面的步驟: 首先, 需要找到無法訪問的標籤 (unreachable tag):
$ git fsck --unreachable | grep tag
記下這個標籤 (tag) 的 hash,然後用 Git 的 update-ref
$ git update-ref refs/tags/<tag_name> <hash>
這時你的標籤 (tag) 應該已經恢復了。
已刪除補丁 (patch)
如果某人在 GitHub 上給你發了一個 pull request, 但是然後他刪除了他自己的原始 fork, 你將沒法克隆他們的提交 (commit) 或使用
git am
。在這種情況下, 最好手動的查看他們的提交 (commit),並把它們拷貝到一個本地新分支,然後做提交。
做完提交後, 再修改作者,參見變更作者。然後, 應用變化, 再發起一個新的 pull request。
跟蹤文件 (Tracking Files)
我只想改變一個文件名字的大小寫,而不修改內容
(main)$ git mv --force myfile MyFile
我想從 Git 刪除一個文件,但保留該文件
(main)$ git rm --cached log.txt
配置 (Configuration)
我想給一些 Git 命令添加別名 (alias)
在 OS X 和 Linux 下, 你的 Git 的配置文件儲存在 ~/.gitconfig
。我在[alias]
部分添加了一些快捷別名 (和一些我容易拼寫錯誤的),如下:
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
我想緩存一個倉庫 (repository) 的用戶名和密碼
你可能有一個倉庫需要授權,這時你可以緩存用戶名和密碼,而不用每次推 / 拉 (push/pull) 的時候都輸入,Credential helper 能幫你。
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
我不知道我做錯了些什麼
你把事情搞砸了:你
重置(reset)
了一些東西, 或者你合併了錯誤的分支, 亦或你強推了後找不到你自己的提交 (commit) 了。有些時候, 你一直都做得很好, 但你想回到以前的某個狀態。
這就是
git reflog
的目的,reflog
記錄對分支頂端 (the tip of a branch) 的任何改變, 即使那個頂端沒有被任何分支或標籤引用。基本上, 每次 HEAD 的改變, 一條新的記錄就會增加到reflog
。遺憾的是,這隻對本地分支起作用,且它只跟蹤動作 (例如,不會跟蹤一個沒有被記錄的文件的任何改變)。
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
上面的 reflog 展示了從 main 分支簽出 (checkout) 到 2.2 分支,然後再籤回。那裏,還有一個硬重置 (hard reset) 到一個較舊的提交。最新的動作出現在最上面以 HEAD@{0}
標識.
如果事實證明你不小心回移 (move back) 了提交(commit), reflog 會包含你不小心回移前 main 上指向的提交(0254ea7)。
$ git reset --hard 0254ea7
然後使用 git reset 就可以把 main 改回到之前的 commit,這提供了一個在歷史被意外更改情況下的安全網。
來自:入門小站
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/McjqjUZcl8ya4JqnQKCoJA