git 命令

整理常用的命令

技巧

1.取消使用 https 拉取代碼時候每次都需要輸入密碼

git config --global credential.helper store

2.本地永久忽略,效果的gitignore一樣

#忽略跟蹤
git update-index --assume-unchanged /path/to/file
#恢復跟蹤
git update-index --no-assume-unchanged /path/to/file

# 當在修改一個巨大的文件 先對其 git update-index --assume-unchanged filepath
# 這樣 Git 暫時不會理睬你對文件做的修改
# 當工作告一段落決定可以提交的時候
# 重置改標識  git update-index --no-assume-unchanged filepath
# 于是 Git 只需要做一次更新

3.生成一個可供發布的壓縮包

git archive

4..gitignore無效

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

n


remote -> 遠程倉庫
repository -> 本地倉庫
index / stage -> 暫存區
workspace -> 工作區

新建

1.在當前目錄新建一個git倉庫

git init

2.新建一個目錄,并初始化為git庫

git init [project_path]

3.創建一個裸倉庫(遠端)

git init --bare fengfeng.git

# Initialized empty Git repository in /home/gitrepository171103/fengfeng.git/

# 用 git init 初始化的版本庫用戶也可以在該目錄下執行所有 git 操作 但別的用戶在將更新 push 上來的時候容易出現沖突
# 用 git init --bare 創建裸倉庫 之所以叫裸倉庫是因為這個倉庫只保存 git 歷史提交的版本信息 而不允許用戶在上面進行各種git操作 否則報錯 This operation must be run in a work tree
# 慣用的命名方式是在庫名后加上 .git
# 這個就是最好把遠端倉庫初始化成bare倉庫的原因

4.克隆項目

git clone [project_url]

配置

1.當前git配置

git config --list

2.編輯git配置文件

git config -e [--global]

3.設置提交代碼時候的用戶信息

git config [--global] user.username "[name]"
git config [--global] user.email "[email]"

增加刪除文件

1.添加文件到暫存區,實際上就是把文件修改添加到暫存區

git add file_1 [file_2] ...

2.添加指定目錄到暫存區,包括子目錄

git add [dir_name]

3.添加當前目錄的所有文件到暫存區

git add.

4.刪除工作區文件,并將這次刪除放入暫存區

git rm file_1 [file_2] ...

5.停止追蹤指定文件,單該文件會保留在工作區

git rm --cached [file]

6.改文件名,并且將這個修改放入暫存區

git mv file_name file_new_name

提交代碼

1.提交暫存區文件到倉庫,實際上就是把暫存區的所有內容提交到當前分支。

git commit -m msg

2.提交暫存區的指定文件到倉庫區

git commit file_1 [file_2] ...-m msg

3.提交工作區自上次 commit 之后的變化,直接到倉庫

git commit -a

4.提交時顯示所有的 diff 信息

git commit -v

5.使用一次新的 commit 來替代上一次提交,如果代碼沒有任何新的變化,就會改寫上一次 commit 的提交信息

git commit --amend -m msg

6.重做上一次 commit,并包括指定文件的新變化

git commit --amend file_1 [file_2] ...

7.修改 commit 提交注釋

// 修改還未push的注釋,修改后保存退出
git commit --amend

// push到遠端還沒有人其他人下載或改動的,進入修改頁面修改注釋信息,修改后:wq保存退出
git commit --amend
git push --force-with-lease origin master
// 如果其他人已經下載或改動:
git fetch origingit reset --hard origin/master

分支

1.列出所有的本地分支

git branch

2.列出所有的遠程分支

git branch -r

# 將遠程分支信息獲取到本地
# git fetch 
# 將遠程分支映射到本地命名為local_branchname 的分支
# git checkout -b local_branchname origin/remote_branchname

3.列出所有本地分支和遠程分支

git branch -a

4.新建一個分支,并切換到該分支

git checkout -b branch_name
git push origin branch_name
# 相當于
git branch branch_name
git checkout branch_name

5.新建一個分支,但依然停留在當前分支

git branch branch_name

6.新建一個分支,指向指定的 commit

git branch [branch] [commit_id]

7.新建一個分支,與指定的遠程分支建立追蹤關系

git branch --track [branch] [remote_branch]

8.切換到指定的分支,并更新工作區

git checkout branch_name

9.建立追蹤關系,在現有的分支和指定的遠程分支之間

git branch --set-upstream [branch] [remote_branch]

10.合并指定分支到當前分支

git merge branch

11.選擇一個commit,合并到當前分支

git cherry-pick commit_id

// 合并 分支dev 指定 commit 到 當前分支master
git checkout master
git cherry-pick d5dfd52ebce385912b8842451665aab781961e81

12.刪除分支

// 這些分支中還包含著尚未合并進來的工作成果,所以簡單地刪除該分支會提示錯誤,因為那樣做會丟失數據
git branch -d branch-name 
// 如果確實想要刪除該分支上的改動,可以用大寫的刪除選項 -D 強制執行
git branch -D branch-name

13.刪除遠程分支

git push origin --delete branch_name
git branch -dr remote/branch

14.查看各個分支最后一個提交對象的信息

git branch -v

15.查看哪些分支已經并入當前分支或未并入當前分支

git branch --merged
git branch --no-merged // 尚未合并的分支
  1. 設置 push pull 默認的 提交 獲取 的分支
# 這樣就很方便的使用 git push 提交信息或 git pull 
git branch --set-upstream-to=origin/dev
# 取消對master的跟蹤
git branch --unset-upstream master
  1. 如果已經存在了同名的分支 使用 git checkout -b new_branch_name 會提示錯誤,加入-B可選參數后會強制創建新分支 并且會覆蓋原來存在的同名分支
git checkout -B new_branch_name
  1. 基于當前所在分支新建一個赤裸裸的分支 沒有任何的提交歷史 但是當前分支的內容一一俱全
    新建的分支 嚴格意義上說 還不是一個分支 因為HEAD指向的引用中沒有commit值 只有在進行一次提交后 它才算得上真正的分支
git checkout --orphan new_branch_ name
  1. 在切換分支的時候 將當前分支修改的內容一起打包帶走 同步到切換的分支下
  • 如果當前分支和切換分支間的內容不同的話,容易造成沖突。
  • 切換到新分支后,當前分支修改過的內容就丟失了。
git checkout --merge branch_name
  1. 這個命令可以用來打補丁 主要用來比較兩個分支間的差異內容 并提供交互式的界面來選擇進一步的操作
    這個命令不僅可以比較兩個分支間的差異 還可以比較單個文件的差異
git checkout -p branch_name
  1. 拉取遠端分支到本地
# 在本地新建分支,并自動切換到該本地分支
# 本地分支會和遠程分支建立映射關系
git checkout -b 本地分支名 origin/遠程分支名

# 在本地新建分支,不會自動切換到該本地分支,需要手動checkout
# 本地分支不會和遠程分支建立映射關系
git fetch origin 遠程分支名:本地分支名
  1. 重命名本地分支
git branch -m localbranchname newbranchname

標簽

1.列出所有tag

git tag

2.新建一個 tag 在當前的 commit

git tag tag_name

3.新建一個 tag 在指定的 commit

git tag tag_name commit

4.查看tag信息

git show tag

5.提交所有 tag 信息

git push remote --tags

6.新建一個分支,指向某個tag

git checkout -b branch tag

7.提交指定 tag

git push remote_name tag_name
  1. 刪除tag
# 刪除本地 tag
git tag -d tag_name
# 刪除遠程 tag
git push origin :refs/tags/tag_name

查看信息

1.顯示有變更的文件

git status

2.顯示當前分支的版本歷史

git log
git log -p
git log -p -2

// 獲取從單詞層面的對比
// 這里并沒有平常看到的添加行或者刪除行的信息。這里的對比顯示在行間。新增加的單詞被 {+ +} 括起來,被刪除的單詞被 [- -] 括起來。在進行單詞層面的對比的時候,你可能希望上下文( context )行數從默認的 3 行,減為 1 行,那么可以使用 -U1 選項
git log -p --word-diff
git log -U1 --word-diff

3.顯示 commit 歷史,以及每次 commit 發生變更的文件

// 僅顯示簡要的增改行數統計
git log --stat

4.顯示某個文件的版本歷史,包括文件改名

git log --follow file
git whatchanged file

5.顯示指定文件的相關的每一次 diff

git log -p file

6.顯示指定文件是什么人在什么時候修改的

git blame file

7.顯示暫存區和工作區的差異

git diff

8.顯示暫存區和上一個 commit 的差異

git diff --cached file

#Git 1.6.1 及更高版本還允許使用
git diff --staged file

9.顯示工作區和當前分支最新 commit 的差異

git diff HEAD

10.顯示兩次提交之間的差異

git diff first_branch...seconde_branch

11.顯示某次提交的元數據和內容變化

git show commit_id

12.顯示某次提交發生變化的文件

git show --name-only commit_id

13.顯示某次提交時,某個文件的內容

git show commit:file_name

14.顯示當前分支的最近幾次提交

git reflog

15.git log 命令支持的選項

-p              按補丁格式顯示每個更新之間的差異。
--word-diff     按 word diff 格式顯示差異。
--stat          顯示每次更新的文件修改統計信息。
--shortstat     只顯示 --stat 中最后的行數修改添加移除統計。
--name-only     僅在提交信息后顯示已修改的文件清單。
--name-status   顯示新增、修改、刪除的文件清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字符,而非所有的 40 個字符。
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)。
--graph         顯示 ASCII 圖形表示的分支合并歷史。
--pretty        使用其他格式顯示歷史提交信息。可用的選項包括 oneline,short,full,fuller 和 format(后跟指定格式)。
--oneline   --pretty=oneline --abbrev-commit 的簡化用法。

遠程同步

1.下載遠程倉庫的所有變更

git fetch remote

2.顯示所有遠程倉庫

git remote -v

3.顯示某個遠程倉庫的信息

git remote show [remote]

4.增加一個新的遠程倉庫,并命名

git remote add name url

5.取回遠程倉庫的變化,并與本地分支合并

git pull [remote] [branch]

6.上傳本地指定分支到遠程倉庫

git push [remote] [branch]

7.強行推送當前分支到遠程倉庫,即使有沖突

git push [remote] --force

8.推送所有分支到遠程倉庫

git push [remote] --all
  1. 更新遠端的狀態
git remote update origin --prune

撤銷

1.恢復暫存區里的指定文件到工作區

git checkout file

2.恢復某個 commit 的指定文件到工作區

git checkout commit_id file

3.恢復上一個 commit 的所有文件到工作區

git checkout

4.重置暫存區的指定文件,與上一次 commit 保持一致,但工作區不變

git reset file

5.重置暫存區與工作區,與上一次 commit 保持一致

git reset --hard

6.重置當前分支的指針為指定 commit,同時重置暫存區,但工作區不變

git reset commit_id

7.重置當前分支的HEAD為指定commit,同時重置暫存區和工作區,與指定 commit 一致

git reset --hard commit_id

8.重置當前分支的HEAD為指定commit,但保持暫存區和工作區不變

git reset --keep commit_id

9.新建一個 commit,用來撤銷指定的 commit,后者的所有變化都將被前者抵消,并且應用到當前分支

git revert commit_id

10.未 commit 的修改 需要恢復到修改前的版本(在沒有 add 前 取消修改)

git checkout -- file
git checkout -- .
  1. add 后的文件 需要恢復
git reset HEAD file
git reset head .

# 文件已修改,未add到暫存區:
# git checkout -- file可還原

# 文件已修改,并add到暫存區未commit:
# git reset HEAD file
# git checkout -- file可還原
  1. 已經commit,想取消commit
git reset --soft HEAD^

或者 git reset [ --mixed | --soft | --hard] [<commit ID>]
注意,是想要撤銷的 commit 的前一條 commit id

1.  使用參數--mixed(默認參數),撤銷git commit,撤銷git add,保留編輯器改動代碼
  git reset --mixed <commit ID>或git reset <commit ID>
2.  使用參數--soft,撤銷git commit,不撤銷git add,保留編輯器改動代碼
  git reset --soft<commit ID>
3.  使用參數--hard,撤銷git commit,撤銷git add,刪除編輯器改動代碼,此方式
  git reset --hard <commit ID>
  1. 刪除暫存區或者分支上的文件,但是工作區中文件還將保留,一般用來取消文件的版本控制,但是依然可以在工作區編輯文件
// 此時file為脫落版本控制狀態(IDEA中文件顏色為紅色)
git rm -r --cached file 
// 若文件已經在版本分支里則delete file已經加入到了暫存區所以還需要下面的命令
git commit -m 'delete cached file' 
git push origin master
  1. 修改 commit 的備注信息
git commit --amend
  1. 刪除已經push的commit
    通過git reset –soft <版本號>重置至指定版本的提交,達到撤銷提交的目的,必須添加參數force進行強制提交,否則會提交失敗,并報錯。
1. 參數soft指的是:保留當前工作區,以便重新提交,比如我們這次是修改后重新提交 還可以選擇參數hard,會撤銷相應工作區的修改,一定要謹慎使用
報錯原因:本地項目版本號低于遠端倉庫版本號
git reset --soft *****
2. 強制提交當前版本號,以達到撤銷版本號的目的
git push origin branch_name --force

不定期更新 不合適的地方 還請指點~ 感激不盡

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 以下內容是我在學習和研究Git時,對Git操作的特性、重點和注意事項的提取、精練和總結,可以做為Git操作的字典,...
    科研者閱讀 4,249評論 4 50
  • 以下內容是我在學習和研究Git時,對Git操作的特性、重點和注意事項的提取、精練和總結,可以做為Git操作的字典,...
    科研者閱讀 3,575評論 2 19
  • 1. 安裝 Github 查看是否安裝git: $ git config --global user.name "...
    Albert_Sun閱讀 13,722評論 9 163
  • git常用命令 GIT常用命令備忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章閱讀 8,641評論 1 26
  • 青燈 如果你是條船, 漂泊就是你的命運,可別靠岸. ——北島 在我還在上大學的時候,我看大冰的書,《他們最幸?!?...
    貝塔妹妹閱讀 408評論 0 4