身爲技術人員,都知道Git是幹嗎的。從服務端角度它是代碼倉庫,能夠多人協做、版本控制、高效處理大型或小型項目全部內容;從客戶端講,它可以方便管理本地分支、且與服務端代碼的同步,從拉取、合併、提交等等管理分支都靠它!git
Git輕量、易於學習,若是不用搭建和維護代碼倉庫的話(運維職責),只要掌握幾個git經常使用命令便可在工做中輕鬆應對。緩存
下面簡單介紹幾個概念,同時列出工做中經常使用命令:bash
快速入門,弄明白如下幾個概念便可:運維
.git
,這個不是工做區,而是Git的版本庫;git add
能夠把要提交的內容放到暫存區;git commit
把暫存區全部內容提交到當前分支;工做中,通常咱們提交代碼只要四步:學習
git pull
拉取代碼,提交代碼前確保和服務端倉庫一致,避免衝突;git add ./your_file.txt
把文件添加進去,實際就是從工做區提交到暫存區;git commit -m 'first commit'
提交更改,再把暫存區全部內容提交到當前分支(默認master);git push [remoteName]
推送到遠程倉庫,也就是推到服務端,這樣別人就能拉取pull
你的代碼;平時工做也就用到上面四個步驟,固然了凡事有例外,下面說幾個例外的處理辦法:3d
git checkout <branch>
:切換到你須要的分支(dev、hotfix)版本控制
git checkout -b <branch>
: 若是沒有分支,加上-b參數表示建立並切換;rest
參考連接:https://git-scm.com/docs/git-checkoutcode
撤銷得分三種狀況:教程
git add
的撤銷方法;.gitignore
文件修改以後且沒有git add
,直接經過git checkout -- <file>
撤銷;λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\work (master -> origin) λ git checkout -- .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
擴展:
命令git checkout -- .gitignore
意思就是,把.gitignore文件在工做區的修改所有撤銷,這裏有兩種狀況:
一種是.gitignore自修改後尚未被放到暫存區,如今撤銷修改就回到和版本庫如出一轍的狀態;
一種是.gitignore已經添加到暫存區,又做了修改,如今,撤銷修改就回到添加到暫存區後的狀態。
總之,就是讓這個文件回到最近一次git commit或git add時的狀態。
參考連接:https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536
git add
的撤銷方法git reset .gitignore
撤銷到未git add
狀態,再執行第一步便可。λ git add .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: .gitignore D:\learning\git\work (master -> origin) λ git reset .gitignore Unstaged changes after reset: M .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\work (master -> origin) λ git checkout -- .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
git reset --hard commitid
直接回到未修改狀態。λ git add .gitignore λ git commit -m "test" #(省略無用部分) λ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean D:\learning\git\work (master -> origin) λ git log commit b7de9378f39834dbc8304d4a8d30f39a4003c673 (HEAD -> master) Author: test <test@163.com> Date: Mon Sep 14 02:59:02 2020 +0800 test commit b3ed1078e543cdb26b984dac584df9db7553d506 (origin/master, origin/HEAD) Author: test <test@163.com> Date: Mon Sep 14 02:39:54 2020 +0800 09142020 D:\learning\git\work (master -> origin) λ git reset --hard b3ed1078e543cdb26b984dac584df9db7553d506 HEAD is now at b3ed107 09142020 D:\learning\git\work (master -> origin) λ git log commit b3ed1078e543cdb26b984dac584df9db7553d506 (HEAD -> master, origin/master, origin/HEAD) Author: test <test@163.com> Date: Mon Sep 14 02:39:54 2020 +0800 09142020 D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
git stash
能夠將你已經修改,但不想提交(git push)的代碼臨時保存到堆棧中,也就是迴歸到你git pull
時的狀態。而後就能隨意切換分支救火,完成後切換回來再git push pop
便可恢復以前的修改內容。stash
不只能夠恢復到原先開發的分支,也能夠恢復到其餘任意指定的分支上(可跨分支)。
git status
,會發現當前是一個乾淨的工做區,沒有任何改動。git stash list
顯示保存進度的列表。也就意味着,git stash命令能夠屢次執行。
git stash pop
恢復最新的進度到工做區。git默認會把工做區和暫存區的改動都恢復到工做區。
git stash pop stash@{stash_id}
恢復指定的進度到工做區。stash_id經過git stash list命令獲得的
經過git stash pop命令恢復進度後,會刪除當前進度。
git stash drop stash@{stash_id}
可使用git stash drop命令,後面能夠跟stash_id
或使用git stash clear命令,刪除全部緩存的stash
git stash show
查看堆棧中最新保存的stash和當前目錄的差別
git add
以後再stash發現工做區是乾淨的;git stash pop
恢復。λ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: .gitignore Untracked files: (use "git add <file>..." to include in what will be committed) test.txt no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track) D:\learning\git\timed_tasks (master -> origin) λ git add test.txt D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git stash list stash@{0}: WIP on master: 542a055 create .gitignore stash@{1}: WIP on master: 542a055 create .gitignore stash@{2}: WIP on (no branch): 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
D:\learning\git\timed_tasks (master -> origin) λ git stash show test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) D:\learning\git\timed_tasks (master -> origin) λ git stash pop On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt Dropped refs/stash@{0} (b69da2894d5e7f511be18277c5a0cd4582fbf453) D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt
Tip:若是你修改的全部文件都不想要了怎麼辦?可經過git stash清空,懂吧?
λ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git stash clear D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
----by 鋼鐵 648403020@qq.com
參考資料:
Git官方文檔:https://git-scm.com/docs
廖雪峯Git教程:https://www.liaoxuefeng.com/wiki/896043488029600