1、創(chuàng)建一個(gè)git倉(cāng)庫(kù),生成.git文件 git init
2、克隆指定分支 git clone -b 分支名 倉(cāng)庫(kù)地址
3、查看當(dāng)前分支 git branch
4、查看所有分支 git branch -a
5、切換分支 git checkout 分支名
以某個(gè)分支為基礎(chǔ)創(chuàng)建新分支 git checkout -b 新分支名 基準(zhǔn)分支名
6、拉代碼
git pull 從遠(yuǎn)程倉(cāng)庫(kù)獲取最新提交,并將其合并到當(dāng)前分支(相當(dāng)于 git fetch + git merge)
git fetch 從遠(yuǎn)程倉(cāng)庫(kù)獲取最新提交,但不會(huì)自動(dòng)合并
7、添加至緩沖區(qū) git add
git add -A 提交所有變化
git add -u 提交被修改(modified)和被刪除(deleted)文件,不包括新文件(new)
git add . 提交新文件(new)和被修改(modified)文件,不包括被刪除(deleted)文件
8、提交代碼 git commit -m "注釋"
9、推送代碼 git push
推送本地新建的分支 git push --set-upstream origin 分支名
縮寫(xiě) git push -u origin 分支名
10、版本回退 git reset
git reset --hard HEAD^ 回退上一個(gè)版本 (HEAD代表當(dāng)前版本,有一個(gè)^代表上一個(gè)版本,以此類推)
git reset --hard d7b5:回退到指定版本(其中d7b5是想回退的指定版本號(hào)的前幾位)
11、刪除文件 git rm 文件名稱
12、忽略文件.gitignore
新加.gitignore只能忽略那些原來(lái)沒(méi)有被提交過(guò)的文件,如果某些文件已經(jīng)被納入了版本管理中,則修改.gitignore是無(wú)效的。
解決辦法:刪除本地緩存,改成未track狀態(tài)
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
13、查看日志 git log
14、取消合并未提交更改 git merge --abort
取消合并已提交更改 git reset --hard HEAD@{1}
15、比較文件修改前后差異 git diff 文件名稱
stash
# 保存當(dāng)前未commit的代碼
git stash
# 保存當(dāng)前未commit的代碼并添加備注
git stash save "備注的內(nèi)容"
# 列出stash的所有記錄
git stash list
#stash@{0}: WIP on ...
#stash@{1}: WIP on ...
#stash@{2}: On ...
# 刪除stash的所有記錄
git stash clear
# 應(yīng)用最近一次的stash
git stash apply
# 應(yīng)用第二條stash
git stash apply stash@{1}
# 應(yīng)用最近一次的stash,隨后刪除該記錄
git stash pop
# 刪除最近的一次stash
git stash drop
reset --soft
回退已提交的commit,并將commit的修改內(nèi)容放回暫存區(qū)。
#讓 commit 記錄強(qiáng)制回溯到某一個(gè)節(jié)點(diǎn)
git reset --hard
#回溯節(jié)點(diǎn)外,還會(huì)保留節(jié)點(diǎn)的修改內(nèi)容
git reset --soft
# 恢復(fù)最近一次 commit
git reset --soft HEAD^
#已 push 的commit再次 push 時(shí),由于遠(yuǎn)程分支和本地分支有差異,需要強(qiáng)制推送 git push -f 來(lái)覆蓋被 reset 的 commit
git push -f
tag
git tag <tagname> 創(chuàng)建標(biāo)簽
git tag -a <tagname> -m "message" 創(chuàng)建帶備注的標(biāo)簽
git push origin <tagname> 推送具體標(biāo)簽
git push origin -tags 推送本地所有標(biāo)簽
git tag -d <tagname> 刪除本地標(biāo)簽
git push origin -delete <tagname> 刪除遠(yuǎn)程標(biāo)簽