歡迎你們前往騰訊雲+社區,獲取更多騰訊海量技術實踐乾貨哦~javascript
本文由 騰訊工蜂發表於 雲+社區專欄
簡單的,經常使用的命令也就幾個。可是想很是熟練使用,怕是要記住幾十個。html
兩者都是從遠程拉取代碼到本地java
假設遠程分支上有2個提交,而後基於遠程develop,再建立一個分支feature。git
而後分別在兩個分支上作兩次提交。緩存
git merge
這時候,你能夠用pull
命令把develop
分支上的修改拉下來而且和你的修改合併;結果看起來就像一個新的和並提交
app
git rebase
$ git checkout feature $ git rebase develop
將一個分支的末端指向另外一個提交,能夠用來移除當前分支的一些提交。機器學習
文件層面上,將緩存區的文件同步到指定的那個提交。工具
$ git checkout develop $ git reset HEAD~2 # 將當前的README.md從緩存區中移除出去 $ git reset HEAD README.md
develop分支末端的兩個提交就變成了懸掛提交post
若是提交尚未push,git reset 是撤銷commit的簡單方法
除了在當前分支上操做,還能夠經過其餘參數來修改stage或者workspace學習
# 將當前的改動從stage中移除,但這些改動還保留在workspace中 $ git reset --mixed HEAD # 徹底捨棄沒有提交的改動 $ git reset --hard HEAD
提交層面上的checkout,能夠切換分支,同一分支,能夠切換當前HEAD。
文件層面上,不會移動HEAD指針,也不會切換到其餘分支上,只是更改workspace,而不是stage。
# 將HEAD移到新的分支,而後更新工做目錄 $ git checkout develop # 將HEAD移動到當前commit的前兩個commit上,同時更新workspace $ git checkout HEAD~2 # 將workspace中的README.md同步到最新的提交 $ git checkout HEAD README.md
撤銷一個提交的同時會建立一個新的提交。
# 產生一個新的commit用於撤銷倒數第二個commit $ git checkout develop $ git revert HEAD~2
命令 | 做用域 | 經常使用情景 |
---|---|---|
git reset | 提交層面 | 在私有分支上舍棄一些沒有提交的更改 |
git reset | 文件層面 | 將文件從緩存區中移除 |
git checkout | 提交層面 | 切換分支或查看舊版本 |
git checkout | 文件層面 | 捨棄工做目錄中的更改 |
git revert | 提交層面 | 在公共分支上回滾更改 |
git revert | 文件層面 | (然而並無) |
git stash
會把全部未提交的修改(包括暫存和未暫存的)都保存起來,用於往後恢復當前工做目錄
$ git status On branch develop Changes to be committed: new file: README.md Changes not staged for commit: modified: index.html $ git stash Saved working directory and index state WIP on master: 5002d47 ... $ git status On branch master nothing to commit, working tree clean
stage是本地的,不會上傳到git server
實際應用中,推薦給每一個stash加一個message,使用git stash save
取代 git stash
$ git stash save "test stash" Saved working directory and index state On autoswitch: test stash HEAD 如今位於 296e8d4 remove unnecessary postion reset in onResume function $ git stash list stash@{0}: On autoswitch: test stash
git stash list
命令,查看stash列表$ git stash list stash@{0}: WIP on master: 049d078 stash_0 stash@{1}: WIP on master: c264051 stash_1 stash@{2}: WIP on master: 21d80a5 stash_2
2. 使用git stash apply
命令能夠經過名字指定那個stash,默認指定最近的(stash@{0})
3. 使用git stash pop
將stash中第一個stash刪除,並將對應修改應用到當前的工做目錄中
4. 使用git stash drop
,後面加上stash名,能夠移除相應的stash;或者使用git stash clear
清空全部stash
默認狀況下,git stash
會緩存:
但不會緩存:
此時,使用-u
或者--include-untracked
能夠stash untracked 文件;使用-a
或者--all
能夠stash當前目錄下的全部修改(慎用)
# 在當前目錄新建一個Git代碼庫 $ git init # 新建一個目錄,將其初始化爲Git代碼庫 $ git init git_test # 下載一個項目和它的整個代碼歷史 $ git clone http://git.code.oa.com/jaelintu/git_test
# 添加指定文件到暫存區 $ git add file1 file2... # 添加指定目錄到暫存區,包括子目錄 $ git add dir # 添加當前目錄的全部文件到暫存區 $ git add . # 添加每一個變化前,都會要求確認 # 對於同一個文件的多處變化,能夠實現分次提交 $ git add -p # 刪除工做區文件,而且將此次刪除放入暫存區 $ git rm file1 file2 ...
# 提交暫存區到倉庫區 $ git commit -m "message" # 提交暫存區的指定文件到倉庫區 $ git commit file1 file2 ... -m "message" # 提交工做區自上次commit以後的變化,直接到倉庫區 $ git commit -a # 提交時顯示全部diff信息 $ git commit -v # 使用一次新的commit,替代上一次提交 # 若是代碼沒有任何新變化,則用來改寫上一次commit的提交信息 $ git commit --amend -m "message" # 重作上一次commit,幷包括指定文件的新變化 $ git commit --amend file1 file2 ...
# 列出全部本地分支 $ git branch # 列出全部遠程分支 $ git branch -r # 列出全部本地分支和遠程分支 $ git branch -a # 新建一個分支,但依然停留在當前分支 $ git branch name # 新建一個分支,並切換到該分支 $ git checkout -b branch # 新建一個分支,指向指定commit $ git branch name commit_SHA # 新建一個分支,與指定的遠程分支創建追蹤關係 $ git branch --track name orgin/name # 切換到指定分支,並更新工做區 $ git checkout name # 切換到上一個分支 $ git checkout - # 創建追蹤關係,在現有分支與指定的遠程分支之間 $ git branch --set-upstream name origin/name # 合併指定分支到當前分支 $ git merge branch-name # 選擇一個commit,合併進當前分支 $ git cherry-pick commit_SHA # 刪除分支 $ git branch -d branch-name # 刪除遠程分支 $ git push origin --delete branch-name $ git branch -dr remote/branch
# 列出全部tag $ git tag # 新建一個tag在當前commit $ git tag tag-name # 新建一個tag在指定commit $ git tag tag-name commit-SHA # 刪除本地tag $ git tag -d tag-name # 刪除遠程tag $ git push origin :refs/tags/tag-Name # 查看tag信息 $ git show tag-name # 提交指定tag $ git push origin tag-name # 提交全部tag $ git push origin --tags # 新建一個分支,指向某個tag $ git checkout -b branch-name tag-name
# 顯示有變動的文件 $ git status # 顯示當前分支的版本歷史 $ git log # 顯示commit歷史,以及每次commit發生變動的文件 $ git log --stat # 搜索提交歷史,根據關鍵詞 $ git log -S [keyword] # 顯示某個commit以後的全部變更 $ git log (tag-name||commit-SHA) HEAD # 顯示某個文件的版本歷史,包括文件更名 $ git log --follow file $ git whatchanged file # 顯示指定文件相關的每一次diff $ git log -p file # 顯示過去5次提交 $ git log -5 --pretty --oneline # 顯示全部提交過的用戶,按提交次數排序 $ git shortlog -sn # 顯示指定文件是什麼人在什麼時間修改過 $ git blame file # 顯示暫存區和工做區的代碼差別 $ git diff # 顯示暫存區和上一個commit的差別 $ git diff --cached file # 顯示工做區與當前分支最新commit之間的差別 $ git diff HEAD # 顯示兩次提交之間的差別 $ git diff [first-branch]...[second-branch] # 顯示今天你寫了多少行代碼 $ git diff --shortstat "@{0 day ago}" # 顯示某次提交的元數據和內容變化 $ git show commit-SHA # 顯示某次提交發生變化的文件 $ git show --name-only commit-SHA # 顯示某次提交時,某個文件的內容 $ git show commit-SHA:filename # 顯示當前分支的最近幾回提交 $ git reflog # 從本地master拉取代碼更新當前分支:branch 通常爲master $ git rebase branch-name
# 下載遠程倉庫的全部變更 $ git fetch origin # 顯示全部遠程倉庫 $ git remote -v # 顯示某個遠程倉庫的信息 $ git remote show origin # 增長一個新的遠程倉庫,並命名 $ git remote add shortname url # 取回遠程倉庫的變化,並與本地分支合併 $ git pull origin branch-name # 上傳本地指定分支到遠程倉庫 $ git push origin branch-name # 強行推送當前分支到遠程倉庫,即便有衝突 $ git push origin --force # 推送全部分支到遠程倉庫 $ git push origin --all
# 恢復暫存區的指定文件到工做區 $ git checkout file # 恢復某個commit的指定文件到暫存區和工做區 $ git checkout commit-SHA file # 恢復暫存區的全部文件到工做區 $ git checkout . # 重置暫存區的指定文件,與上一次commit保持一致,但工做區不變 $ git reset file # 重置暫存區與工做區,與上一次commit保持一致 $ git reset --hard # 重置當前分支的指針爲指定commit,同時重置暫存區,但工做區不變 $ git reset commit-SHA # 重置當前分支的HEAD爲指定commit,同時重置暫存區和工做區,與指定commit一致 $ git reset --hard commit-SHA # 重置當前HEAD爲指定commit,但保持暫存區和工做區不變 $ git reset --keep commit-SHA # 新建一個commit,用來撤銷指定commit # 後者的全部變化都將被前者抵消,而且應用到當前分支 $ git revert commit-SHA # 暫時將未提交的變化移除,稍後再移入 $ git stash $ git stash pop
rebase過程當中,也許會出現衝突(conflict)
git add
添加衝突的文件,更新暫存區git rebase --continue
繼續剩下的rebasegit rebase --abort
終止rebase行爲,而且feature會回到rebase開始以前的狀態$ git rebase develop CONFLICT (content): Rebase conflict in readme.txt Automatic rebase failed; fix conflicts and then commit the result. $ git status On branch feature You have unmerged paths. (fix conflicts and run "git rebase --continue") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a")
查看readme.md 內容
Git tracks changes of files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a new branch is quick AND simple. >>>>>>> feature
選擇保留HEAD
或者feature
的版本
Git tracks changes of files. Creating a new branch is quick AND simple.
在提交:
$ git add readme.md $ git rebase --contine
1. Source Tree(號稱最好用):特點支持git flow,一鍵建立工做流
2. Tortoise git:文件的右鍵菜單很容易上手
相關閱讀
【每日課程推薦】機器學習實戰!快速入門在線廣告業務及CTR相應知識
此文已由做者受權騰訊雲+社區發佈,更多原文請點擊
搜索關注公衆號「雲加社區」,第一時間獲取技術乾貨,關注後回覆1024 送你一份技術課程大禮包!