工作中用到git時(shí),很多命令經(jīng)常需要網(wǎng)上查找,浪費(fèi)了大量時(shí)間。因此,決定寫本文來收錄常用的git命令,方便工作速查,并會(huì)不但更新完善。
配置系列
-
git config工具專門用來配置git相應(yīng)的工作環(huán)境變量,這些環(huán)境變量決定了git在各個(gè)環(huán)節(jié)的工作方式和行為。-
git config --list可以查看已有配置信息 - 配置用戶名和郵箱:
git config --global user.name "someone" git config --global user.email "someone@company.com" -
git config --system修改配置文件[/etc/gitconfig] -
git config --global修改配置文件[~/.gitconfig] - 配置優(yōu)先級: [工作目錄中的
.git/config] > [~/.gitconfig] > [/etc/gitconfig]
-
- 配置
ssh:git可以通過https和ssh兩種協(xié)議與遠(yuǎn)程倉庫進(jìn)行通訊。配置ssh的基本思路是:本地生產(chǎn)ssh密鑰對--->在倉庫端添加本地生產(chǎn)的public key。- 本地生產(chǎn)
ssh密鑰對:ssh-keygen -t rsa -
~/.ssh/id_rsa.pub即為public key
- 本地生產(chǎn)
基本操作
-
git add將工作區(qū)的文件添加到暫存區(qū) -
git restore撤銷git追蹤過的文件的修改-
git restore --worktree/-W some_file撤銷文件在工作區(qū)的修改 -
git restore --staged/-S some_file撤銷文件在暫存區(qū)的修改,將狀態(tài)恢復(fù)到add之前 -
git restore -s commit_id some_file將當(dāng)前工作區(qū)切換到某一次commit
-
-
git clean從工作目錄中刪除所有git沒有追蹤過的文件-
git clean -n演習(xí),列出那些文件將被刪除 -
git clean -df刪除當(dāng)前目前下未被``git```追蹤過的文件和文件夾
-
-
git reset [--soft | --mixed | --hard] commit_id回退版本,--mixed為默認(rèn)選項(xiàng)-
git reset --hard commit_id將倉庫、暫存區(qū)和工作區(qū)均回退到某個(gè)版本
-
-
git checkout實(shí)現(xiàn)git restore和git switch的功能-
git checkout some_branch/git switch some_branch切換分支 -
git checkout -b new_branch/git switch -c new_branch創(chuàng)建并切換分支
-
-
git commit提交 -
git push推送分支-
git push推送到關(guān)聯(lián)的遠(yuǎn)程分支 -
git push origin some_local_branch:some_remote_branch推送到遠(yuǎn)程分支 -
git push origin :some_remote_branch刪除遠(yuǎn)程分支 -
git push origin --delete some_remote_branch刪除遠(yuǎn)程分支
-
-
git fetch從遠(yuǎn)程倉庫更新本地倉庫 -
git rebase變基-
git rebase some_branch以some_branch為基礎(chǔ)執(zhí)行變基 -
git add解決rebase沖突 -
git rebase --abort終止變基 -
git rebase --continue繼續(xù)變基
-
-
git merge分支合并-
git add+git commit解決merge沖突
-
-
git pull等同于git fetch+git merge -
git revert撤銷某次操作,并將撤銷操作作為新的提交 -
git cherry-pick挑選commit進(jìn)行合并-
git cherry-pick commit-id挑選commit_id進(jìn)行合并 -
git add解決cherry-pick沖突 -
git cherry-pick --continue繼續(xù)合并 -
git cherry-pick --abort放棄合并
-
-
git swich切換分支-
git switch some_branch切換分支 -
git switch -c new_branch創(chuàng)建并切換分支
-
-
git log --oneline單行顯示提交記錄 -
git status查看git工程當(dāng)前狀態(tài) -
git diff-
git diff commit_id查看commit_id的修改 -
git diff commit_id1 commit_id2查看兩次提交之間的差異 -
git diff --cached/--staged查看暫存區(qū)和倉庫之間的差異 -
git diff branch1 branch2查看兩分支之間的差異
-
-
git remote remove origin移除源origin -
git remote add origin https://*.git增加源origin
分支管理
- 通過遠(yuǎn)程分支創(chuàng)建本地分支:
git checkout -b some_local_branch origin/some_remote_branch - 通過當(dāng)前分支創(chuàng)建分支:
git checkout -b some_branch - 切換分支:
git checkout some_branch - 合并分支:
git merge some_branch - 變基分支:
git rebase some_branch - 刪除分支:
git branch -d some_branch - 查看分支:
git branch - 將本地分支與遠(yuǎn)程分支關(guān)聯(lián):
git branch --set-upstream-to=origin/some_remote_branch some_local_branch - 推送分支:
git push origin some_local_branch:some_remote_branch - 強(qiáng)推分支:
git push -f
沖突解決
-
git merge沖突解決:git add+git commit -
git rebase沖突解決:git add+git rebase --continue -
git cherry-pick沖突解決:git add+git cherry-pick --continue
修改commit歷史
- 修改當(dāng)前
commit的內(nèi)容和message:-
git commit --amend將暫存區(qū)的內(nèi)容合并到上一次提交,并可以修改message
-
- 修改歷史commit:
-
git rebase -i commit_id以commit_id為基準(zhǔn)執(zhí)行交互式變基 - 修改歷史commit里面文件
-
文件權(quán)限
- 查看文件權(quán)限:
git ls-files --stage some_file - 修改文件權(quán)限:
git update-index --chmod=+x some_file
子模塊
- 增加子模塊:
git submodule add submodule_url - 獲取子模塊:
- 方式一:
git clone mainmodule_url --recurse-submodules - 方式二:在主項(xiàng)目中執(zhí)行
git submodule init+git submodule update - 方式三:在主項(xiàng)目中執(zhí)行
git submodule update --init --recursive
- 方式一:
- 更新子模塊到
commit_id:- 進(jìn)入子模塊并更新:
git fetch & git rebase & git reset --hard commit_id - 推到主模塊并提交:
git push
- 進(jìn)入子模塊并更新:
- 查看子模塊:
git submodule