Git常用命令 (持續更新)

NicolasHe發表於2017-08-11

生成SSH Key:

ssh-keygen -t rsa -C "<email>"

設定:

git config --global user.email "<email>"
git config --global user.name "<name>"

新增remote:

git remote add <name> <path>
git pull <name> <branch> --rebase

使用pull <name> <branch> –rebase衝突後, 修改好衝突後 merge:

git add .
git pull --continue

檢視改動:

# 檢視還沒有暫存起來的變化內容
git diff
# 檢視已經暫存起來的檔案(staged)和上次提交時的的差異
git diff --cached
git diff --staged
# 顯示工作版本(working tree)和HAD的不同
git diff HEAD
# 當前目錄與另一個分支的差別
git diff <branch>
# 比較上次提交和上上次提交
git diff HEAD^ HEAD

取消改動:

# 放棄當前未暫存的改動
git checkout .
# 放棄git add .暫存後改動
git reset HEAD
# 放棄commit後但未push的改動,需要重新修改再push
git reset <commit_id>
# 放棄push到git後的改動
git reset --hard <commit_id>
git push origin HEAD --force

覆蓋上一條已經提交的commit:

git add .
git commit --amend
git push origin <branch> -f

刪除遠端ignored的檔案:

git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"

拉取遠端分支並提交:

git checkout -b <branch> <remote>/<branch>
git push --set-upstream origin <branch>

檢視分支跟蹤情況/設定新的跟蹤

git branch -vv
git branch --set-upstream <branch> <remote>/<branch>

相關文章