Git經常使用命令清單

增長/刪除文件

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

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

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

# 更名文件,而且將這個更名放入暫存區
$ git mv [file-original] [file-renamed]

代碼提交

# 提交暫存區到倉庫區
$ 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 [branch-name]

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

# 新建一個分支,指向指定commit
$ git branch [branch] [commit]

# 創建追蹤關係,在現有分支與指定的遠程分支之間
$ git branch --set-upstream [branch] [remote-branch]

# 合併指定分支到當前分支
$ git merge [branch]

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

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

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

撤銷

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

# 恢復某個commit的指定文件到暫存區和工做區
$ git checkout [commit] [file]

# 恢復暫存區的全部文件到工做區
$ git checkout .

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

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

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

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

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

# 新建一個commit,用來撤銷指定commit
# 後者的全部變化都將被前者抵消,而且應用到當前分支
$ git revert [commit]

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

更改本地和遠程分支的名稱

git branch -m old_branch new_branch # Rename branch locally 
git push origin :old_branch # Delete the old branch 
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote

放棄修改,強制覆蓋本地代碼

git fetch --all
git reset --hard origin/master 
git pull
//合併寫法
git fetch --all && git reset --hard origin/master && git pull

撤銷commit

git reset --soft HEAD^so

解決衝突,合併分支

//合併指定分支到當前分支
git merge master

使用一次新的commit,替代上一次提交

git commit --amend -m [message]
相關文章
相關標籤/搜索