GIT常用命令
查看本地分支
git branch
或者
git branch -v
查看遠(yuǎn)程分支
git branch -r
創(chuàng)建分支
git branch分支名
例如創(chuàng)建develop分支:
git branch develop
刪除本地分支
git branch -d localbranch_name
刪除遠(yuǎn)程分支
git branch -r -d origin/branch-name
git push origin :banch-name
把分支推到遠(yuǎn)程分支(相當(dāng)于創(chuàng)建遠(yuǎn)程分支)
git push origin分支名
例如把本地develop分支推送到遠(yuǎn)程:
git push origin develop
拉取遠(yuǎn)程分支到本地并對(duì)應(yīng)本地分支
git checkout origin/remoteName -b localName
切換分支
git checkout分支名
例如切換到develop分支:git checkout develop
查看所有分支從屬關(guān)系
gitlog --graph --decorate --all
查看本地代碼修改
git status
忽略某個(gè)或者某類未跟蹤文件闺魏,eg忽略所有pyc文件
源碼目錄添加.gitignore文件史辙,在.gitignore中添加需要忽略文件
忽略某個(gè)或者某類已跟蹤文件,eg忽略所有pyc文件
git rm --cached *.pyc
本地文件提交步驟:
1误证、git pull先從服務(wù)器拉代碼,保證本地的代碼是服務(wù)器最新的弦牡,防止沖突
2兼都、git status查看本地代碼做了哪些修改
3、git add filename本地需要添加的代碼
4套媚、git add --all添加所有的文件缚态。該命令對(duì)當(dāng)前文件夾范圍有效,運(yùn)行該命令一定要注意當(dāng)前目錄堤瘤,最好不要在根目錄使用玫芦,cd到子文件夾確認(rèn)好再執(zhí)行
5、git diff filename和服務(wù)器代碼比較本辐,查看做了哪些改動(dòng)
6姨俩、git commit -m “l(fā)og” -a提交所有修改的文件到本地倉庫蘸拔,“l(fā)og”是注釋語句。最好每次提交都寫簡(jiǎn)單明了的注釋
7环葵、git log查看提交日志调窍。這一步主要是確認(rèn)是否提交成功,當(dāng)然從第6步git返回的信息也能看到是否提交成功
8张遭、git checkout --filename撤銷邓萨。相當(dāng)于一鍵還原,如果發(fā)現(xiàn)前面提交錯(cuò)誤就用該命令會(huì)退到上一次commit或者add的狀態(tài)
9菊卷、本地分支push到遠(yuǎn)程分支(本地分支與遠(yuǎn)程分支不同名時(shí)用以下命令缔恳,同名直接git push)
$ git push -u origin local_branchname:remote_branchname
分支間merge
1、切換到需要merge的分支洁闰,或者在需要merge的分支目錄下運(yùn)行g(shù)it bash
2歉甚、git merge develop把develop分支的全部merge到該分支
撤銷已修改
Git checkout filename
版本回退—返回上一個(gè)版本
1、徹底回到某個(gè)版本
git reset –-hard
2扑眉、回退到上一個(gè)版本
git resetHEAD^
3纸泄、回退到某一個(gè)版本
git reset head^版本哈希值
快照git stash
1、git stash 對(duì)當(dāng)前的暫存區(qū)和工作區(qū)狀態(tài)進(jìn)行保存
2腰素、git stash save "備注"? 使用save可以對(duì)進(jìn)度添加備注
3聘裁、git stash list 列出所有保存的進(jìn)度列表
4、git stash pop [--index] [<stash>]恢復(fù)工作進(jìn)度
5弓千、git stash clear刪除所有進(jìn)度
6衡便、git stash drop[<stash>] 刪除某一個(gè)進(jìn)度,默認(rèn)刪除最新進(jìn)度
7洋访、git stash show 顯示和他parent的差異
解決沖突:
1镣陕、git stash//保存修改到緩存
2、git pull//拉取git遠(yuǎn)程倉庫代碼
3姻政、git stash list//查看緩存信息
4呆抑、git stash pop stash@{0}//恢復(fù)第一條緩存
5、git commit -m “l(fā)og” –a//提交本地修改代碼
6扶歪、git push//推送本地分支代碼到遠(yuǎn)程對(duì)應(yīng)分支
查看commit
1理肺、git log 查看 當(dāng)前分支的 提交歷史 git log --graph 看起來跟好看
2、git reflog 顯示整個(gè)本地倉儲(chǔ)的commit,包括所有branch的commit, 甚至包括已經(jīng)撤銷的commit, 只要HEAD發(fā)生了變化, 就會(huì)在reflog里面看得到