Checkout
換branch、換到某commit的狀態
$ git checkout branch-name
$ git checkout xxxxx # <- commit hash
換到remote的branch
$ git checkout --track origin/daves_branch
# Same as "git checkout -b [branch] [remotename]/[branch]"
開新branch
$ git checkout -b some-branch
$ git push --set-upstream origin some-branch
gitignore
加入.gitignore
vim .gitignore
git add .gitignore
git commit -m "Add .gitignore"
更新tracking files
# Remove tracking in git
$ git rm -r --cached .
$ git status
# 應該一堆綠色(deleted)和一堆紅色(untracked)...
$ git add .
$ git status
# 需要從git ignore走的檔案會顯示為"deleted"
$ git commit -m "Remove tracking some files"
Log: 查看git history
$ git log
Reset: 取消local commit
$ git reset HEAD~1 # 取消一個local commit
$ git reset HEAD~2 # 兩個
Revert: 回復到幾個commit前的狀態
重要概念1:不要想著能回到以前,git是version control工具,任何動作和history都需要被紀錄,包括「回復舊version」這動作。
重要概念2:每revert一個commit都是一次改動,代表需要一次commit,但可以找方法squash做同一次commit。
$ git revert HEAD~1 # revert最後一個commit
$ git revert HEAD~2 # 兩個
# ...
$ git revert master~3..master # A <- B <- C <- D <- BCD' <- HEAD
# 途中不用自動commit,只留下最後一個change自己commit
# 不建議使用
$ git revert --no-commit master~3..master # A <- B <- C <- D <- BCD' <- HEAD
Rebase:
Combine previous commits
可以和上面的revert
組合使用,例如revert master~4..master
後,出現四個用來revert的commits,然後用rebase來變做同一個commit。
$ git rebase -i HEAD~4
# rebase HEAD~4: rebase至第四新的commit
# -i: 用interactive的方式決定如何處理之前的commits
1 pick af9ccec Upgrade to expo v25.0.0 dependencies # 第一個不能是squash
2 squash c6f387a Update version # squash到上一個commit當中
3 squash e9564d4 Update ios android version
4 squash c588e11 Update yarn.lock to expo v25.0.0
5
6 # Rebase 32f1edc..c588e11 onto 32f1edc (4 commands)
7 #
8 # Commands:
9 # p, pick = use commit
10 # r, reword = use commit, but edit the commit message
11 # e, edit = use commit, but stop for amending
12 # s, squash = use commit, but meld into previous commit
13 # f, fixup = like "squash", but discard this commit's log message
14 # x, exec = run command (the rest of the line) using shell
15 # d, drop = remove commit
16 #
17 # These lines can be re-ordered; they are executed from top to bottom.
18 #
19 # If you remove a line here THAT COMMIT WILL BE LOST.
20 #
21 # However, if you remove everything, the rebase will be aborted.
22 #
23 # Note that empty commits are commented out
Amend: 修改commit
修改上一條local commit的message,並加入現在的staged changes
$ git commit --amend
# 或直接
$ git commit --amend -m "xxxx"