寫在前面:
整理:CS_GUIDER,作者:阮一峰,原文鏈接:https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
文章一般會(huì)優(yōu)先在個(gè)人博客中更新,歡迎少爺們圍觀我的個(gè)人博客:
https://wl2o2o.github.io/(點(diǎn)擊即可訪問)
寶藏資源推薦???:
推薦一個(gè)快速練習(xí) Git 的寶藏網(wǎng)站——?jiǎng)訄D演示 Git 的命令邏輯。
戳我查看??
常用 Git 命令清單
我每天使用 Git ,但是很多命令記不住。
一般來說,日常使用只要記住下圖6個(gè)命令,就可以了。但是熟練使用,恐怕要記住60~100個(gè)命令。
下面是我整理的常用 Git 命令清單。幾個(gè)專用名詞的譯名如下。
- Workspace:工作區(qū)
- Index / Stage:暫存區(qū)
- Repository:倉庫區(qū)(或本地倉庫)
- Remote:遠(yuǎn)程倉庫
一、新建代碼庫
# 在當(dāng)前目錄新建一個(gè)Git代碼庫 $ git init # 新建一個(gè)目錄,將其初始化為Git代碼庫 $ git init [project-name] # 下載一個(gè)項(xiàng)目和它的整個(gè)代碼歷史 $ git clone [url]
二、配置
Git的設(shè)置文件為.gitconfig
,它可以在用戶主目錄下(全局配置),也可以在項(xiàng)目目錄下(項(xiàng)目配置)。
# 顯示當(dāng)前的Git配置 $ git config --list # 編輯Git配置文件 $ git config -e [--global] # 設(shè)置提交代碼時(shí)的用戶信息 $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]" # usage: git config [<options>] Config file location --global use global config file --system use system config file --local use repository config file --worktree use per-worktree config file -f, --file <file> use given config file --blob <blob-id> read config from given blob object Action --get get value: name [value-pattern] --get-all get all values: key [value-pattern] --get-regexp get values for regexp: name-regex [value-pattern] --get-urlmatch get value specific for the URL: section[.var] URL --replace-all replace all matching variables: name value [value-pattern] --add add a new variable: name value --unset remove a variable: name [value-pattern] --unset-all remove all matches: name [value-pattern] --rename-section rename section: old-name new-name --remove-section remove a section: name -l, --list list all --fixed-value use string equality when comparing values to 'value-pattern' -e, --edit open an editor --get-color find the color configured: slot [default] --get-colorbool find the color setting: slot [stdout-is-tty] Type -t, --type <> value is given this type --bool value is "true" or "false" --int value is decimal number --bool-or-int value is --bool or --int --bool-or-str value is --bool or string --path value is a path (file or directory name) --expiry-date value is an expiry date Other -z, --null terminate values with NUL byte --name-only show variable names only --includes respect include directives on lookup --show-origin show origin of config (file, standard input, blob, command line) --show-scope show scope of config (worktree, local, global, system, command) --default <value> with --get, use default value when missing entry
三、增加/刪除文件
# 添加指定文件到暫存區(qū) $ git add [file1] [file2] ... # 添加指定目錄到暫存區(qū),包括子目錄 $ git add [dir] # 添加當(dāng)前目錄的所有文件到暫存區(qū) $ git add . # 添加每個(gè)變化前,都會(huì)要求確認(rèn) # 對(duì)于同一個(gè)文件的多處變化,可以實(shí)現(xiàn)分次提交 $ git add -p # 刪除工作區(qū)文件,并且將這次刪除放入暫存區(qū) $ git rm [file1] [file2] ... # 停止追蹤指定文件,但該文件會(huì)保留在工作區(qū) $ git rm --cached [file] # 改名文件,并且將這個(gè)改名放入暫存區(qū) $ git mv [file-original] [file-renamed]
四、代碼提交
# 提交暫存區(qū)到倉庫區(qū) $ git commit -m [message] # 提交暫存區(qū)的指定文件到倉庫區(qū) $ git commit [file1] [file2] ... -m [message] # 提交工作區(qū)自上次commit之后的變化,直接到倉庫區(qū) $ git commit -a # 提交時(shí)顯示所有diff信息 $ git commit -v # 使用一次新的commit,替代上一次提交 # 如果代碼沒有任何新變化,則用來改寫上一次commit的提交信息 $ git commit --amend -m [message] # 重做上一次commit,并包括指定文件的新變化 $ git commit --amend [file1] [file2] ...
五、分支
# 列出所有本地分支 $ git branch # 列出所有遠(yuǎn)程分支 $ git branch -r # 列出所有本地分支和遠(yuǎn)程分支 $ git branch -a # 新建一個(gè)分支,但依然停留在當(dāng)前分支 $ git branch [branch-name] # 新建一個(gè)分支,并切換到該分支 $ git checkout -b [branch] # 新建一個(gè)分支,指向指定commit $ git branch [branch] [commit] # 新建一個(gè)分支,與指定的遠(yuǎn)程分支建立追蹤關(guān)系 $ git branch --track [branch] [remote-branch] # 切換到指定分支,并更新工作區(qū) $ git checkout [branch-name] # 切換到上一個(gè)分支 $ git checkout - # 建立追蹤關(guān)系,在現(xiàn)有分支與指定的遠(yuǎn)程分支之間 $ git branch --set-upstream [branch] [remote-branch] $ 詳細(xì)說明: `--set-upstream`是`git push`命令的一個(gè)選項(xiàng),它用于在將本地分支推送到遠(yuǎn)程倉庫時(shí),同時(shí)設(shè)置本地分支與遠(yuǎn)程分支的關(guān)聯(lián)關(guān)系。 當(dāng)您使用`git push`命令將本地分支推送到遠(yuǎn)程倉庫時(shí),Git會(huì)嘗試將本地分支的更新推送到與之關(guān)聯(lián)的遠(yuǎn)程分支。通常情況下,當(dāng)您首次推送本地分支時(shí),遠(yuǎn)程倉庫可能還沒有與之對(duì)應(yīng)的遠(yuǎn)程分支。在這種情況下,如果您使用`--set-upstream`選項(xiàng),Git會(huì)自動(dòng)在遠(yuǎn)程倉庫上創(chuàng)建一個(gè)與本地分支同名的分支,并將其與本地分支關(guān)聯(lián)起來。 所以,當(dāng)您執(zhí)行以下命令時(shí): ``` git push --set-upstream origin hexo ``` 它的含義是將當(dāng)前分支(假設(shè)是`hexo`)的更新推送到名為`origin`的遠(yuǎn)程倉庫,并在遠(yuǎn)程倉庫上創(chuàng)建一個(gè)名為`hexo`的分支,并將其與本地分支進(jìn)行關(guān)聯(lián)。 通過使用`--set-upstream`選項(xiàng),您可以方便地建立本地分支與遠(yuǎn)程分支之間的連接,使得后續(xù)的`git push`命令可以自動(dòng)將本地分支的更新推送到正確的遠(yuǎn)程分支。 # 合并指定分支到當(dāng)前分支 $ git merge [branch] # 選擇一個(gè)commit,合并進(jìn)當(dāng)前分支 $ git cherry-pick [commit] # 刪除分支 $ git branch -d [branch-name] # 刪除遠(yuǎn)程分支 $ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
六、標(biāo)簽
# 列出所有tag $ git tag # 新建一個(gè)tag在當(dāng)前commit $ git tag [tag] # 新建一個(gè)tag在指定commit $ git tag [tag] [commit] # 刪除本地tag $ git tag -d [tag] # 刪除遠(yuǎn)程tag $ git push origin :refs/tags/[tagName] # 查看tag信息 $ git show [tag] # 提交指定tag $ git push [remote] [tag] # 提交所有tag $ git push [remote] --tags # 新建一個(gè)分支,指向某個(gè)tag $ git checkout -b [branch] [tag]
七、查看信息
# 顯示有變更的文件 $ git status # 顯示當(dāng)前分支的版本歷史 $ git log # 顯示commit歷史,以及每次commit發(fā)生變更的文件 $ git log --stat # 搜索提交歷史,根據(jù)關(guān)鍵詞 $ git log -S [keyword] # 顯示某個(gè)commit之后的所有變動(dòng),每個(gè)commit占據(jù)一行 $ git log [tag] HEAD --pretty=format:%s # 顯示某個(gè)commit之后的所有變動(dòng),其"提交說明"必須符合搜索條件 $ git log [tag] HEAD --grep feature # 顯示某個(gè)文件的版本歷史,包括文件改名 $ git log --follow [file] $ git whatchanged [file] # 顯示指定文件相關(guān)的每一次diff $ git log -p [file] # 顯示過去5次提交 $ git log -5 --pretty --oneline # 顯示所有提交過的用戶,按提交次數(shù)排序 $ git shortlog -sn # 顯示指定文件是什么人在什么時(shí)間修改過 $ git blame [file] # 顯示暫存區(qū)和工作區(qū)的差異 $ git diff # 顯示暫存區(qū)和上一個(gè)commit的差異 $ git diff --cached [file] # 顯示工作區(qū)與當(dāng)前分支最新commit之間的差異 $ git diff HEAD # 顯示兩次提交之間的差異 $ git diff [first-branch]...[second-branch] # 顯示今天你寫了多少行代碼 $ git diff --shortstat "@{0 day ago}" # 顯示某次提交的元數(shù)據(jù)和內(nèi)容變化 $ git show [commit] # 顯示某次提交發(fā)生變化的文件 $ git show --name-only [commit] # 顯示某次提交時(shí),某個(gè)文件的內(nèi)容 $ git show [commit]:[filename] # 顯示當(dāng)前分支的最近幾次提交 $ git reflog
八、遠(yuǎn)程同步
# 下載遠(yuǎn)程倉庫的所有變動(dòng) $ git fetch [remote] # 顯示所有遠(yuǎn)程倉庫 $ git remote -v # 顯示某個(gè)遠(yuǎn)程倉庫的信息 $ git remote show [remote] # 增加一個(gè)新的遠(yuǎn)程倉庫,并命名 $ git remote add [shortname] [url] # 取回遠(yuǎn)程倉庫的變化,并與本地分支合并 $ git pull [remote] [branch] # 上傳本地指定分支到遠(yuǎn)程倉庫 $ git push [remote] [branch] # 強(qiáng)行推送當(dāng)前分支到遠(yuǎn)程倉庫,即使有沖突 $ git push [remote] --force # 推送所有分支到遠(yuǎn)程倉庫 $ git push [remote] --all
九、撤銷
# 恢復(fù)暫存區(qū)的指定文件到工作區(qū) 誤刪文件,用于恢復(fù)add時(shí)的狀態(tài) $ git checkout [file] # 恢復(fù)某個(gè)commit的指定文件到暫存區(qū)和工作區(qū) $ git checkout [commit] [file] # 恢復(fù)暫存區(qū)的所有文件到工作區(qū) $ git checkout . # 重置暫存區(qū)的指定文件,與上一次commit保持一致,但工作區(qū)不變 已暫存的內(nèi)容不想提交,用于取消暫存de $ git reset [file] # 重置暫存區(qū)與工作區(qū),與上一次commit保持一致 $ git reset --hard # 如果想回滾的話,比如說回滾到上一個(gè)版本,可以執(zhí)行以下兩種命令: $ git reset --hard HEAD^ 上上個(gè)版本就是 git reset --hard HEAD^^,以此類推。 $ git reset --hard HEAD~100 如果回滾到前 100 個(gè)版本,用這個(gè)命令比上一個(gè)命令更方便。 # 假如回滾錯(cuò)了,想恢復(fù),不記得版本號(hào)了,可以先執(zhí)行此命令查看版本號(hào): $ git reflog # 重置當(dāng)前分支的指針為指定commit,同時(shí)重置暫存區(qū),但工作區(qū)不變 $ git reset [commit] # 重置當(dāng)前分支的HEAD為指定commit,同時(shí)重置暫存區(qū)和工作區(qū),與指定commit一致 $ git reset --hard [commit] # 重置當(dāng)前HEAD為指定commit,但保持暫存區(qū)和工作區(qū)不變 $ git reset --keep [commit] # 新建一個(gè)commit,用來撤銷指定commit # 后者的所有變化都將被前者抵消,并且應(yīng)用到當(dāng)前分支 $ git revert [commit] # 暫時(shí)將未提交的變化移除,稍后再移入 $ git stash $ git stash pop
十、其他
# 生成一個(gè)可供發(fā)布的壓縮包 $ git archive
說說一個(gè)比較常見的問題:如何合并沖突?
當(dāng)我們?cè)谶h(yuǎn)倉直接更新內(nèi)容時(shí),下次如果是在本地準(zhǔn)備提交上傳,如果忘記拉取最新更新,
那么在push時(shí)會(huì)拒絕我們推送,所以我們需要在提交前進(jìn)行拉取遠(yuǎn)倉的最新變更,
然后手動(dòng)處理沖突,最后再提交
拉取遠(yuǎn)程倉庫變更:在本地倉庫中運(yùn)行 git pull 命令,將遠(yuǎn)程倉庫中的最新變更拉取到本地。
處理沖突(如果存在):如果在遠(yuǎn)程倉庫和本地倉庫同時(shí)修改了相同的文件,可能會(huì)發(fā)生沖突。Git 會(huì)將沖突標(biāo)記在文件中,你需要手動(dòng)解決這些沖突。打開有沖突的文件,查找并解決沖突的部分。完成后保存文件。
添加和提交變更:在本地倉庫中使用 git add 命令將修改后的文件標(biāo)記為已暫存,然后使用 git commit 命令提交這些變更。確保你提供了有意義的提交消息,以便于他人理解你所做的更改。
推送到遠(yuǎn)程倉庫:使用 git push 命令將本地倉庫的變更推送到遠(yuǎn)程倉庫。
公司多分支倉庫是怎么協(xié)作開發(fā)的?
在Git中,每個(gè)人創(chuàng)建一個(gè)分支進(jìn)行協(xié)作是一種常見的模式。在這種情況下,主分支通常被稱為"main",其他分支則用于個(gè)人或團(tuán)隊(duì)的工作。
如果你想將主分支(main)的最新代碼拉取到你的分支(github-wl)上,可以按照以下步驟進(jìn)行操作:
- 首先,確保你已經(jīng)在你的本地倉庫中創(chuàng)建了一個(gè)名為"github-wl"的分支。你可以使用以下命令創(chuàng)建并切換到該分支:
git checkout -b github-wl
- 接下來,你需要將主分支(main)的最新代碼拉取到你的本地倉庫??梢允褂靡韵旅顝倪h(yuǎn)程倉庫中拉取最新代碼:
git fetch origin main
這將會(huì)獲取主分支的最新更改,但不會(huì)自動(dòng)合并到你的分支。
- 如果你想將這些更改合并到你的分支上,可以使用以下命令進(jìn)行合并:
git merge origin/main
這將把主分支的最新代碼合并到你的當(dāng)前分支(github-wl)上。
- 如果在合并過程中出現(xiàn)沖突,你需要手動(dòng)解決沖突。Git會(huì)在沖突的文件中標(biāo)記出沖突的部分,你需要打開這些文件并手動(dòng)編輯以解決沖突。
- 一旦沖突解決完成,你可以使用以下命令將你的分支推送到遠(yuǎn)程倉庫:
git push origin github-wl
這將把你的分支(github-wl)的更改推送到遠(yuǎn)程倉庫。文章來源:http://www.zghlxwxcb.cn/news/detail-773641.html
通過以上步驟,你可以將主分支(main)的最新代碼拉取到你的分支(github-wl)上,并在解決沖突后將更改推送到遠(yuǎn)程倉庫。文章來源地址http://www.zghlxwxcb.cn/news/detail-773641.html
到了這里,關(guān)于Git 怎么使用?管理項(xiàng)目?沖突?命令匯總(git rebase、git reverse、git reset、git tag)——保姆及教程(持續(xù)更新)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!