整理常用的命令
技巧
1.取消使用 https 拉取代碼時候每次都需要輸入密碼
git config --global credential.helper store
2.本地永久忽略,效果的gitignore一樣
#忽略跟蹤
git update-index --assume-unchanged /path/to/file
#恢復(fù)跟蹤
git update-index --no-assume-unchanged /path/to/file
# 當(dāng)在修改一個巨大的文件 先對其 git update-index --assume-unchanged filepath
# 這樣 Git 暫時不會理睬你對文件做的修改
# 當(dāng)工作告一段落決定可以提交的時候
# 重置改標(biāo)識 git update-index --no-assume-unchanged filepath
# 于是 Git 只需要做一次更新
3.生成一個可供發(fā)布的壓縮包
git archive
4..gitignore
無效
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
n
remote -> 遠(yuǎn)程倉庫
repository -> 本地倉庫
index / stage -> 暫存區(qū)
workspace -> 工作區(qū)
新建
1.在當(dāng)前目錄新建一個git倉庫
git init
2.新建一個目錄,并初始化為git庫
git init [project_path]
3.創(chuàng)建一個裸倉庫(遠(yuǎn)端)
git init --bare fengfeng.git
# Initialized empty Git repository in /home/gitrepository171103/fengfeng.git/
# 用 git init 初始化的版本庫用戶也可以在該目錄下執(zhí)行所有 git 操作 但別的用戶在將更新 push 上來的時候容易出現(xiàn)沖突
# 用 git init --bare 創(chuàng)建裸倉庫 之所以叫裸倉庫是因為這個倉庫只保存 git 歷史提交的版本信息 而不允許用戶在上面進行各種git操作 否則報錯 This operation must be run in a work tree
# 慣用的命名方式是在庫名后加上 .git
# 這個就是最好把遠(yuǎn)端倉庫初始化成bare倉庫的原因
4.克隆項目
git clone [project_url]
配置
1.當(dāng)前git配置
git config --list
2.編輯git配置文件
git config -e [--global]
3.設(shè)置提交代碼時候的用戶信息
git config [--global] user.username "[name]"
git config [--global] user.email "[email]"
增加刪除文件
1.添加文件到暫存區(qū),實際上就是把文件修改添加到暫存區(qū)
git add file_1 [file_2] ...
2.添加指定目錄到暫存區(qū),包括子目錄
git add [dir_name]
3.添加當(dāng)前目錄的所有文件到暫存區(qū)
git add.
4.刪除工作區(qū)文件,并將這次刪除放入暫存區(qū)
git rm file_1 [file_2] ...
5.停止追蹤指定文件,單該文件會保留在工作區(qū)
git rm --cached [file]
6.改文件名,并且將這個修改放入暫存區(qū)
git mv file_name file_new_name
提交代碼
1.提交暫存區(qū)文件到倉庫,實際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支。
git commit -m msg
2.提交暫存區(qū)的指定文件到倉庫區(qū)
git commit file_1 [file_2] ...-m msg
3.提交工作區(qū)自上次 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到遠(yuǎn)端還沒有人其他人下載或改動的,進入修改頁面修改注釋信息,修改后:wq保存退出
git commit --amend
git push --force-with-lease origin master
// 如果其他人已經(jīng)下載或改動:
git fetch origingit reset --hard origin/master
分支
1.列出所有的本地分支
git branch
2.列出所有的遠(yuǎn)程分支
git branch -r
# 將遠(yuǎn)程分支信息獲取到本地
# git fetch
# 將遠(yuǎn)程分支映射到本地命名為local_branchname 的分支
# git checkout -b local_branchname origin/remote_branchname
3.列出所有本地分支和遠(yuǎn)程分支
git branch -a
4.新建一個分支,并切換到該分支
git checkout -b branch_name
git push origin branch_name
# 相當(dāng)于
git branch branch_name
git checkout branch_name
5.新建一個分支,但依然停留在當(dāng)前分支
git branch branch_name
6.新建一個分支,指向指定的 commit
git branch [branch] [commit_id]
7.新建一個分支,與指定的遠(yuǎn)程分支建立追蹤關(guān)系
git branch --track [branch] [remote_branch]
8.切換到指定的分支,并更新工作區(qū)
git checkout branch_name
9.建立追蹤關(guān)系,在現(xiàn)有的分支和指定的遠(yuǎn)程分支之間
git branch --set-upstream [branch] [remote_branch]
10.合并指定分支到當(dāng)前分支
git merge branch
11.選擇一個commit,合并到當(dāng)前分支
git cherry-pick commit_id
// 合并 分支dev 指定 commit 到 當(dāng)前分支master
git checkout master
git cherry-pick d5dfd52ebce385912b8842451665aab781961e81
12.刪除分支
// 這些分支中還包含著尚未合并進來的工作成果,所以簡單地刪除該分支會提示錯誤,因為那樣做會丟失數(shù)據(jù)
git branch -d branch-name
// 如果確實想要刪除該分支上的改動,可以用大寫的刪除選項 -D 強制執(zhí)行
git branch -D branch-name
13.刪除遠(yuǎn)程分支
git push origin --delete branch_name
git branch -dr remote/branch
14.查看各個分支最后一個提交對象的信息
git branch -v
15.查看哪些分支已經(jīng)并入當(dāng)前分支或未并入當(dāng)前分支
git branch --merged
git branch --no-merged // 尚未合并的分支
- 設(shè)置 push pull 默認(rèn)的 提交 獲取 的分支
# 這樣就很方便的使用 git push 提交信息或 git pull
git branch --set-upstream-to=origin/dev
# 取消對master的跟蹤
git branch --unset-upstream master
- 如果已經(jīng)存在了同名的分支 使用 git checkout -b new_branch_name 會提示錯誤,加入-B可選參數(shù)后會強制創(chuàng)建新分支 并且會覆蓋原來存在的同名分支
git checkout -B new_branch_name
- 基于當(dāng)前所在分支新建一個赤裸裸的分支 沒有任何的提交歷史 但是當(dāng)前分支的內(nèi)容一一俱全
新建的分支 嚴(yán)格意義上說 還不是一個分支 因為HEAD指向的引用中沒有commit值 只有在進行一次提交后 它才算得上真正的分支
git checkout --orphan new_branch_ name
- 在切換分支的時候 將當(dāng)前分支修改的內(nèi)容一起打包帶走 同步到切換的分支下
- 如果當(dāng)前分支和切換分支間的內(nèi)容不同的話,容易造成沖突。
- 切換到新分支后,當(dāng)前分支修改過的內(nèi)容就丟失了。
git checkout --merge branch_name
- 這個命令可以用來打補丁 主要用來比較兩個分支間的差異內(nèi)容 并提供交互式的界面來選擇進一步的操作
這個命令不僅可以比較兩個分支間的差異 還可以比較單個文件的差異
git checkout -p branch_name
- 拉取遠(yuǎn)端分支到本地
# 在本地新建分支,并自動切換到該本地分支
# 本地分支會和遠(yuǎn)程分支建立映射關(guān)系
git checkout -b 本地分支名 origin/遠(yuǎn)程分支名
# 在本地新建分支,不會自動切換到該本地分支,需要手動checkout
# 本地分支不會和遠(yuǎn)程分支建立映射關(guān)系
git fetch origin 遠(yuǎn)程分支名:本地分支名
- 重命名本地分支
git branch -m localbranchname newbranchname
標(biāo)簽
1.列出所有tag
git tag
2.新建一個 tag 在當(dāng)前的 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
- 刪除tag
# 刪除本地 tag
git tag -d tag_name
# 刪除遠(yuǎn)程 tag
git push origin :refs/tags/tag_name
查看信息
1.顯示有變更的文件
git status
2.顯示當(dāng)前分支的版本歷史
git log
git log -p
git log -p -2
// 獲取從單詞層面的對比
// 這里并沒有平??吹降奶砑有谢蛘邉h除行的信息。這里的對比顯示在行間。新增加的單詞被 {+ +} 括起來,被刪除的單詞被 [- -] 括起來。在進行單詞層面的對比的時候,你可能希望上下文( context )行數(shù)從默認(rèn)的 3 行,減為 1 行,那么可以使用 -U1 選項
git log -p --word-diff
git log -U1 --word-diff
3.顯示 commit 歷史,以及每次 commit 發(fā)生變更的文件
// 僅顯示簡要的增改行數(shù)統(tǒng)計
git log --stat
4.顯示某個文件的版本歷史,包括文件改名
git log --follow file
git whatchanged file
5.顯示指定文件的相關(guān)的每一次 diff
git log -p file
6.顯示指定文件是什么人在什么時候修改的
git blame file
7.顯示暫存區(qū)和工作區(qū)的差異
git diff
8.顯示暫存區(qū)和上一個 commit 的差異
git diff --cached file
#Git 1.6.1 及更高版本還允許使用
git diff --staged file
9.顯示工作區(qū)和當(dāng)前分支最新 commit 的差異
git diff HEAD
10.顯示兩次提交之間的差異
git diff first_branch...seconde_branch
11.顯示某次提交的元數(shù)據(jù)和內(nèi)容變化
git show commit_id
12.顯示某次提交發(fā)生變化的文件
git show --name-only commit_id
13.顯示某次提交時,某個文件的內(nèi)容
git show commit:file_name
14.顯示當(dāng)前分支的最近幾次提交
git reflog
15.git log 命令支持的選項
-p 按補丁格式顯示每個更新之間的差異。
--word-diff 按 word diff 格式顯示差異。
--stat 顯示每次更新的文件修改統(tǒng)計信息。
--shortstat 只顯示 --stat 中最后的行數(shù)修改添加移除統(tǒng)計。
--name-only 僅在提交信息后顯示已修改的文件清單。
--name-status 顯示新增、修改、刪除的文件清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字符,而非所有的 40 個字符。
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)。
--graph 顯示 ASCII 圖形表示的分支合并歷史。
--pretty 使用其他格式顯示歷史提交信息??捎玫倪x項包括 oneline,short,full,fuller 和 format(后跟指定格式)。
--oneline --pretty=oneline --abbrev-commit 的簡化用法。
遠(yuǎn)程同步
1.下載遠(yuǎn)程倉庫的所有變更
git fetch remote
2.顯示所有遠(yuǎn)程倉庫
git remote -v
3.顯示某個遠(yuǎn)程倉庫的信息
git remote show [remote]
4.增加一個新的遠(yuǎn)程倉庫,并命名
git remote add name url
5.取回遠(yuǎn)程倉庫的變化,并與本地分支合并
git pull [remote] [branch]
6.上傳本地指定分支到遠(yuǎn)程倉庫
git push [remote] [branch]
7.強行推送當(dāng)前分支到遠(yuǎn)程倉庫,即使有沖突
git push [remote] --force
8.推送所有分支到遠(yuǎn)程倉庫
git push [remote] --all
- 更新遠(yuǎn)端的狀態(tài)
git remote update origin --prune
撤銷
1.恢復(fù)暫存區(qū)里的指定文件到工作區(qū)
git checkout file
2.恢復(fù)某個 commit 的指定文件到工作區(qū)
git checkout commit_id file
3.恢復(fù)上一個 commit 的所有文件到工作區(qū)
git checkout
4.重置暫存區(qū)的指定文件,與上一次 commit 保持一致,但工作區(qū)不變
git reset file
5.重置暫存區(qū)與工作區(qū),與上一次 commit 保持一致
git reset --hard
6.重置當(dāng)前分支的指針為指定 commit,同時重置暫存區(qū),但工作區(qū)不變
git reset commit_id
7.重置當(dāng)前分支的HEAD為指定commit,同時重置暫存區(qū)和工作區(qū),與指定 commit 一致
git reset --hard commit_id
8.重置當(dāng)前分支的HEAD為指定commit,但保持暫存區(qū)和工作區(qū)不變
git reset --keep commit_id
9.新建一個 commit,用來撤銷指定的 commit,后者的所有變化都將被前者抵消,并且應(yīng)用到當(dāng)前分支
git revert commit_id
10.未 commit 的修改 需要恢復(fù)到修改前的版本(在沒有 add 前 取消修改)
git checkout -- file
git checkout -- .
- add 后的文件 需要恢復(fù)
git reset HEAD file
git reset head .
# 文件已修改,未add到暫存區(qū):
# git checkout -- file可還原
# 文件已修改,并add到暫存區(qū)未commit:
# git reset HEAD file
# git checkout -- file可還原
- 已經(jīng)commit,想取消commit
git reset --soft HEAD^
或者 git reset [ --mixed | --soft | --hard] [<commit ID>]
注意,是想要撤銷的 commit 的前一條 commit id
1. 使用參數(shù)--mixed(默認(rèn)參數(shù)),撤銷git commit,撤銷git add,保留編輯器改動代碼
git reset --mixed <commit ID>或git reset <commit ID>
2. 使用參數(shù)--soft,撤銷git commit,不撤銷git add,保留編輯器改動代碼
git reset --soft<commit ID>
3. 使用參數(shù)--hard,撤銷git commit,撤銷git add,刪除編輯器改動代碼,此方式
git reset --hard <commit ID>
- 刪除暫存區(qū)或者分支上的文件,但是工作區(qū)中文件還將保留,一般用來取消文件的版本控制,但是依然可以在工作區(qū)編輯文件
// 此時file為脫落版本控制狀態(tài)(IDEA中文件顏色為紅色)
git rm -r --cached file
// 若文件已經(jīng)在版本分支里則delete file已經(jīng)加入到了暫存區(qū)所以還需要下面的命令
git commit -m 'delete cached file'
git push origin master
- 修改 commit 的備注信息
git commit --amend
- 刪除已經(jīng)push的commit
通過git reset –soft <版本號>
重置至指定版本的提交,達(dá)到撤銷提交的目的,必須添加參數(shù)force進行強制提交,否則會提交失敗,并報錯。
1. 參數(shù)soft指的是:保留當(dāng)前工作區(qū),以便重新提交,比如我們這次是修改后重新提交 還可以選擇參數(shù)hard,會撤銷相應(yīng)工作區(qū)的修改,一定要謹(jǐn)慎使用
報錯原因:本地項目版本號低于遠(yuǎn)端倉庫版本號
git reset --soft *****
2. 強制提交當(dāng)前版本號,以達(dá)到撤銷版本號的目的
git push origin branch_name --force
不定期更新 不合適的地方 還請指點~ 感激不盡