目錄html
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
git config user.name
git config user.email
git config --list
ssh-keygen -t rsa -C"example@qq.com"
,連按三次回車,便可生成ssh-key;ssh -T git@github.com
cd ~/.ssh
ls -a
電腦上已經安裝了git;git
在新的空的目錄下建立一個文件夾practiceGit
,在這個目錄下建立文件promoteGit.txt
,經過對該文件的操做來進行基本命令的學習!github
#git文件夾下建立practiceGit目錄 localhost:git mac$ mkdir practiceGit #切換到practiceGit目錄下 localhost:git mac$ cd practiceGit #添加一個文件promoteGit.txt localhost:practiceGit mac$ touch promoteGit.txt
建立git倉庫shell
主要命令git init
、git add
、git commit
。vim
#初始化倉庫 localhost:practiceGit mac$ git init Initialized empty Git repository in /Users/... #查看這個目錄 會看到初始化倉庫後有 .git文件 localhost:practiceGit mac$ ls -la #編輯這個文件 localhost:practiceGit mac$ vim promoteGit.txt #填寫如下單詞,保存並退出。 today! start practice git! #把文件添加到倉庫 localhost:practiceGit mac$ git add promoteGit.txt #把文件提交到倉庫 -m後面輸入的是本次提交的說明。 localhost:practiceGit mac$ git commit -m "start git learn" [master (root-commit) 960dad7] start git learn 1 file changed, 2 insertions(+) create mode 100644 promoteGit.txt
學習git status
、git diff
命令。app
#修改該文件 localhost:practiceGit mac$ vim promoteGit.txt #添加一行單詞ONE! today! start practice git! ONE! #使用 git status命令,查看結果,它提示已經修改可是還沒提交 localhost:practiceGit mac$ git status On branch 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: promoteGit.txt no changes added to commit (use "git add" and/or "git commit -a") #使用 git diff 查看修改的內容 localhost:practiceGit mac$ git diff promoteGit.txt diff --git a/promoteGit.txt b/promoteGit.txt index 8db65ab..2144d7b 100644 --- a/promoteGit.txt +++ b/promoteGit.txt @@ -1,2 +1,3 @@ today! start practice git! +ONE! #以後即可以進行add和commit操做 localhost:practiceGit mac$ git add promoteGit.txt #查看狀態 localhost:practiceGit mac$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: promoteGit.txt #提交 localhost:practiceGit mac$ git commit -m "add ONE" [master 5869fb0] add ONE 1 file changed, 1 insertion(+) #查看狀態 localhost:practiceGit mac$ git status On branch master nothing to commit, working tree clean
學習git log
、git reset
、git reflog
等命令ssh
#編輯promoteGit.txt文件,添加TWO單詞 localhost:practiceGit mac$ vim promoteGit.txt today! start practice git! ONE TWO! #添加至倉庫並提交 localhost:practiceGit mac$ git add promoteGit.txt #提交 localhost:practiceGit mac$ git commit -m "add TWO" [master dea363c] add TWO 1 file changed, 1 insertion(+), 1 deletion(-) #查看提交歷史 git log localhost:practiceGit mac$ git log commit dea363cb40cacc3b0d6efb7dc8855e9c977fbf31 (HEAD -> master) Author: ***** Date: Wed Jan 8 23:11:31 2020 +0800 add TWO commit 5869fb088f10361fb3b1a028285d39d29a1f20ce Author: ***** Date: Wed Jan 8 23:04:32 2020 +0800 add ONE commit 960dad7ae9fde8d903b9c2e311c2aa63a4974472 Author: ***** Date: Wed Jan 8 22:49:45 2020 +0800 start git learn #查看提交歷史 簡潔版的 git log --pretty=oneline localhost:practiceGit mac$ git log --pretty=oneline dea363cb40cacc3b0d6efb7dc8855e9c977fbf31 (HEAD -> master) add TWO 5869fb088f10361fb3b1a028285d39d29a1f20ce add ONE 960dad7ae9fde8d903b9c2e311c2aa63a4974472 start git learn #版本回退到 「ONE」的版本 上一個版本:HEAD^ 上上一個版本:HEAD^^ 上10個版本:HEAD~10 localhost:practiceGit mac$ git reset --hard HEAD^ HEAD is now at 5869fb0 add ONE #查看文件 localhost:practiceGit mac$ cat promoteGit.txt today! start practice git! ONE! #查看提交歷史 localhost:practiceGit mac$ git log commit 5869fb088f10361fb3b1a028285d39d29a1f20ce (HEAD -> master) Author: ***** Date: Wed Jan 8 23:04:32 2020 +0800 add ONE commit 960dad7ae9fde8d903b9c2e311c2aa63a4974472 Author: ***** Date: Wed Jan 8 22:49:45 2020 +0800 start git learn #回到最新版本 localhost:practiceGit mac$ git reset --hard dea36 HEAD is now at dea363c add TWO localhost:practiceGit mac$ cat promoteGit.txt today! start practice git! ONE TWO! #git reflog 記錄每一次命令 localhost:practiceGit mac$ git reflog dea363c (HEAD -> master) HEAD@{0}: reset: moving to dea36 5869fb0 HEAD@{1}: reset: moving to HEAD^ dea363c (HEAD -> master) HEAD@{2}: commit: add TWO 5869fb0 HEAD@{3}: commit: add ONE 960dad7 HEAD@{4}: commit (initial): start git learn
工做區和暫存區概念ide
學習 git checkout -- file
、git reset HEAD file
學習
git checkout -- file
撤銷工做區修改。#修改promoteGit.txt文件,添加 delect me. 這一行,未提交至暫存區,在工做區的修改所有撤銷! localhost:practiceGit mac$ cat promoteGit.txt today! start practice git! ONE TWO! delect me. #查看工做區狀態 localhost:practiceGit mac$ git status On branch 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: promoteGit.txt no changes added to commit (use "git add" and/or "git commit -a") #在工做區的修改所有撤銷 localhost:practiceGit mac$ git checkout -- promoteGit.txt #查看結果 localhost:practiceGit mac$ cat promoteGit.txt today! start practice git! ONE TWO!
git reset HEAD file
、在撤銷git checkout -- file
。#添加至暫存區 localhost:practiceGit mac$ git add promoteGit.txt #查看變化 localhost:practiceGit mac$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: promoteGit.txt #使用最新的版本 git reset HEAD 把暫存區的修改撤銷掉(unstage),從新放回工做區。 localhost:practiceGit mac$ git reset HEAD promoteGit.txt Unstaged changes after reset: M promoteGit.txt #查看狀態 localhost:practiceGit mac$ git status On branch 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: promoteGit.txt no changes added to commit (use "git add" and/or "git commit -a") #撤銷工做區修改 localhost:practiceGit mac$ git checkout -- promoteGit.txt localhost:practiceGit mac$ cat promoteGit.txt today! start practice git! ONE TWO!
學習刪除文件命令git rm <file>
刪除版本庫中的文件。測試
#背景:添加deleteFile.txt,測試刪除該文件 localhost:practiceGit mac$ git add deleteFile.txt #查看倉庫狀態 localhost:practiceGit mac$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: deleteFile.txt #提交到版本庫 localhost:practiceGit mac$ git commit -m "測試文件刪除" [master d79d315] 測試文件刪除 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 deleteFile.txt #查看記錄每一次命令 localhost:practiceGit mac$ git reflog d79d315 (HEAD -> master) HEAD@{0}: commit: 測試文件刪除 dea363c HEAD@{1}: reset: moving to dea36 5869fb0 HEAD@{2}: reset: moving to HEAD^ dea363c HEAD@{3}: commit: add TWO 5869fb0 HEAD@{4}: commit: add ONE 960dad7 HEAD@{5}: commit (initial): start git learn #在工做區刪除該文件 localhost:practiceGit mac$ rm deleteFile.txt #查看倉庫狀態 localhost:practiceGit mac$ git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: deleteFile.txt no changes added to commit (use "git add" and/or "git commit -a")
#第一種狀況:工做區刪除文件,恢復刪除的文件 localhost:practiceGit mac$ git checkout -- deleteFile.txt localhost:practiceGit mac$ ls -la total 8 drwxr-xr-x 5 mac staff 160 1 13 17:42 . drwxr-xr-x 4 mac staff 128 1 9 23:20 .. drwxr-xr-x 13 mac staff 416 1 13 17:42 .git -rw-r--r-- 1 mac staff 0 1 13 17:42 deleteFile.txt -rw-r--r-- 1 mac staff 36 1 13 17:20 promoteGit.txt
#第二種狀況:將版本庫中的文件刪除並提交 localhost:practiceGit mac$ git rm deleteFile.txt rm 'deleteFile.txt' #提交 localhost:practiceGit mac$ git commit -m "版本庫中文件刪除" [master b21ea4a] 版本庫中文件刪除 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 deleteFile.txt #查看每一次的提交 localhost:practiceGit mac$ git reflog b21ea4a (HEAD -> master) HEAD@{0}: commit: 版本庫中文件刪除 d79d315 HEAD@{1}: commit: 測試文件刪除 dea363c HEAD@{2}: reset: moving to dea36 5869fb0 HEAD@{3}: reset: moving to HEAD^ dea363c HEAD@{4}: commit: add TWO 5869fb0 HEAD@{5}: commit: add ONE 960dad7 HEAD@{6}: commit (initial): start git learn
先在碼雲上建立一個倉庫,寫上倉庫名稱、歸屬、基本介紹等信息;
建立好倉庫後,在本身存在的項目下,依次使用如下命令:
#本地初始化項目 $ git init #將本地倉庫和遠程倉庫相關聯 $ git remote add origin <克隆或者複製的倉庫名稱> #拉取遠程倉庫 $ git pull origin master
初始化、設置遠程倉庫地址後再作push、pull命令拉取諸如 .gitignore 文件等;
在.gitignore文件中編寫須要忽略的文件,例如 .idea、.gradle等。
就能夠提交新的更改的代碼分支了。
準備工做:先在該倉庫下建立originPractise.txt文件,並在文件下添加一行文字!
$ touch originPractise.txt $ vim originPractise.txt $ cat originPractise.txt 練習遠程命令!
建立分支、合併分支、刪除分支等命令
git checkout -b dev
加上-b
建立並切換分支,至關於git branch dev
、git checkout dev
命令;git branch
,查看當前分支;git checkout master
切換至master分支;git merge dev
,合併指定分支到當前分支;git branch -d dev
,合併後刪除該分支;git switch -c dev
,建立+切換分支;git switch master
,切換至master分支;#建立dev分支並切換 $ git checkout -b dev Switched to a new branch 'dev' #查看當前分支 $ git branch * dev master #在dev分支上修改該文件 $ vim originPractise.txt $ cat originPractise.txt 練習遠程命令! 建立dev分支! #添加倉庫並提交 $ git add originPractise.txt $ git commit -m "在dev分支上進行修改!" [dev cedbf27] 在dev分支上進行修改! 1 file changed, 1 insertion(+) #切換至master分支 $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. #查看該分支上該文件,發現dev分支上的修改在master分支上並無修改 $ cat originPractise.txt 練習遠程命令! #合併dev分支到當前分支(master) $ git merge dev Updating 85c20fd..cedbf27 Fast-forward originPractise.txt | 1 + 1 file changed, 1 insertion(+) #查看該分支,發現dev上的修改已經合併至master上 $ cat originPractise.txt 練習遠程命令! 建立dev分支! #刪除dev分支 $ git branch -d dev Deleted branch dev (was cedbf27). #查看分支 $ git branch * master
合併衝突!命令git log --graph --pretty=oneline --abbrev-commit
查看分支合併圖。
#新建立figure分支並切換到該分支 $ git checkout -b figure Switched to a new branch 'figure' #查看分支 $ git branch * figure master #在該分支編輯該文件 $ vim originPractise.txt $ cat originPractise.txt 練習遠程命令! 建立dev分支! 最新建立一個figure分支, 用來測試一下衝突! #添加到倉庫並提交 $ git add originPractise.txt $ git commit -m "figure分支修改文件,測試衝突!" [figure b2d73d8] figure分支修改文件,測試衝突! 1 file changed, 2 insertions(+), 1 deletion(-) #切換至master分支 $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) #查看分支 $ git branch figure * master #在master分支編輯該文件並提交 $ vim originPractise.txt $ git add originPractise.txt $ git commit -m "master分支上修改同一份文件!" [master 8770136] master分支上修改同一份文件! 1 file changed, 3 insertions(+), 1 deletion(-) #將figure分支上文件合併至master $ git merge figure Auto-merging originPractise.txt CONFLICT (content): Merge conflict in originPractise.txt Automatic merge failed; fix conflicts and then commit the result. #查看狀態 $ git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: originPractise.txt no changes added to commit (use "git add" and/or "git commit -a") #查看衝突文件,能夠看到「<<<<<<<」 "=======" ">>>>>>>" $ cat originPractise.txt 練習遠程命令! 建立dev分支! <<<<<<< HEAD 新建立一個figure分支; 使用master分支; 測試一下衝突! ======= 最新建立一個figure分支, 用來測試一下衝突! >>>>>>> figure #解決衝突並提交 $ vim originPractise.txt $ git add originPractise.txt $ git commit -m "衝突合併完成了!" [master 4bb257a] 衝突合併完成了! #查看合併衝突時的曲線圖 $ git log --graph --pretty=oneline --abbrev-commit * 4bb257a (HEAD -> master) 衝突合併完成了! |\ | * b2d73d8 (figure) figure分支修改文件,測試衝突! * | 8770136 master分支上修改同一份文件! |/ * 90d88e3 figure分支修改! * cedbf27 在dev分支上進行修改! * 85c20fd (origin/master) 修改一版文件 #刪除該分支 $ git branch -d figure Deleted branch figure (was b2d73d8).
不使用快速合併(Fast forward),git merge --no-ff -m "提交時的描述" dev
,該命令合併dev至master時,禁止使用Fast forward方式合併,此次合併時要建立一個新的commit,故其中有-m參數。
命令git stash
、git stash list
、git stash pop
、git stash apply
、git stash drop
、git cherry-pick <commit>
等命令。
$ git checkout -b dev Switched to a new branch 'dev' $ git branch * dev master $ vim originPractise.txt #查看狀態,工做區中有未提交的文件 $ git status On branch dev 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: originPractise.txt no changes added to commit (use "git add" and/or "git commit -a") #在dev分支中,將工做區修改的文件貯藏。 $ git stash Saved working directory and index state WIP on dev: 4bb257a 衝突合併完成了! #貯藏後能夠看到工做區乾淨 $ git status On branch dev nothing to commit, working tree clean #切換至maste $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 5 commits. (use "git push" to publish your local commits) #新拉分支改bug $ git checkout -b temp1 Switched to a new branch 'temp1' #第五行添加 「figure分支和master分支」 $ vim originPractise.txt $ cat originPractise.txt 練習遠程命令! 建立dev分支! 新建立一個figure分支; 使用master分支; figure分支和master分支測試一下衝突! 最新建立一個figure分支, 用來測試一下衝突! #添加並提交 $ git add originPractise.txt $ git commit -m "五行修改" [temp1 29ecde1] 五行修改 1 file changed, 1 insertion(+), 1 deletion(-) $ git status On branch temp1 nothing to commit, working tree clean $ git log --pretty=oneline 29ecde1a07943e71bafa4dc23d3fe60f37f2db14 (HEAD -> temp1) 五行修改 4bb257afa90d3d0b68ca18a065a9506f14a861fc (master, dev) 衝突合併完成了! ..... #切換分支 $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 5 commits. (use "git push" to publish your local commits) $ cat originPractise.txt 練習遠程命令! 建立dev分支! 新建立一個figure分支; 使用master分支; 測試一下衝突! 最新建立一個figure分支, 用來測試一下衝突! #將修改bug的temp1分支上合併至maste分支。 $ git merge --no-ff -m "temp1合併至master" temp1 Merge made by the 'recursive' strategy. originPractise.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) #查看提交歷史 $ git log --pretty=oneline 82f684466603a75a969ea18246953bb2400e8c34 (HEAD -> master) temp1合併至master 29ecde1a07943e71bafa4dc23d3fe60f37f2db14 (temp1) 五行修改 4bb257afa90d3d0b68ca18a065a9506f14a861fc (dev) 衝突合併完成了! .... #切換至dev $ git checkout dev Switched to branch 'dev' $ git status On branch dev nothing to commit, working tree clean #查看暫存的文件列表 $ git stash list stash@{0}: WIP on dev: 4bb257a 衝突合併完成了! #應用貯藏的文件並刪除 $ git stash pop On branch dev 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: originPractise.txt no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (c412328d523e22db6d04107c0968ad816ec2da56) #查看暫存的文件列表 能夠看到應用後貯藏的文件消失 $ git stash list #查看dev中修改的文件 $ cat originPractise.txt 練習遠程命令! 建立dev分支! 新建立一個figure分支; 使用master分支; 測試一下衝突! 最新建立一個figure分支, 用來測試一下衝突! 哈哈哈哈哈哈! 測試暫存功能! #添加並提交 $ git add originPractise.txt $ git commit -m "應用貯藏後dev分支修改後提交" [dev 2901f6a] 應用貯藏後dev分支修改後提交 1 file changed, 2 insertions(+), 1 deletion(-) #查看合併曲線圖 $ git log --graph --pretty=oneline --abbrev-commit * 2901f6a (HEAD -> dev) 應用貯藏後dev分支修改後提交 * 4bb257a 衝突合併完成了! |\ | * b2d73d8 figure分支修改文件,測試衝突! * | 8770136 master分支上修改同一份文件! |/ * 90d88e3 figure分支修改! ... #將temp1分支上的修改提交合併到dev上,由於這個修改是在master上修改的,dev中並非最新的修改。 $ git cherry-pick 29ecde [dev 7e10494] 五行修改 Date: Wed Jan 15 14:35:04 2020 +0800 1 file changed, 1 insertion(+), 1 deletion(-) #能夠看到已經合併完成 $ cat originPractise.txt 練習遠程命令! 建立dev分支! 新建立一個figure分支; 使用master分支; figure分支和master分支測試一下衝突! 最新建立一個figure分支, 用來測試一下衝突! 哈哈哈哈哈哈! 測試暫存功能!
命令git branch -D name
刪除未進行合併操做的分支
#新建分支temp2 $ git checkout -b temp2 Switched to a new branch 'temp2' $ vim originPractise.txt #添加並提交 $ git add originPractise.txt $ git commit -m "temp2分支添加最後一行" [temp2 fabd6f8] temp2分支添加最後一行 1 file changed, 1 insertion(+) #刪除該分支 在當前分支時不能刪除本分支 $ git branch -d temp2 error: Cannot delete branch 'temp2' checked out at '/***/**' #切換分支至dev $ git checkout dev Switched to branch 'dev' #刪除temp2分支報錯 $ git branch -d temp2 error: The branch 'temp2' is not fully merged. If you are sure you want to delete it, run 'git branch -D temp2'. #使用-D參數刪除 $ git branch -D temp2 Deleted branch temp2 (was fabd6f8).
多人共同開發
#查看遠程庫信息 $ git remote origin #更詳細的遠程庫信息 $ git remote -v origin https://gitee.com/***/***.git (fetch) origin https://gitee.com/***/***.git (push) #推送信息到遠程倉庫 $ git push origin master Counting objects: 26, done. Delta compression using up to 4 threads. Compressing objects: 100% (25/25), done. Writing objects: 100% (26/26), 2.53 KiB | 862.00 KiB/s, done. Total 26 (delta 16), reused 0 (delta 0) remote: Checking connectivity: 26, done. remote: Powered by GITEE.COM [GNK-3.8] To https://gitee.com/***/***.git 85c20fd..c32a72d master -> master #從遠程拉取信息 $ git pull Already up to date. #把本地未push的分叉提交歷史整理成直線以後在push $ git rebase
學習git tag tagname
、git tag
、git show tagname
、git tag -a tagname -m "標籤的說明"
等命令。
#查看當前分支 $ git branch dev * master temp1 #在該分支上打上標籤 $ git tag Version_001 #查看全部標籤 $ git tag Version_001 #查看提交歷史 $ git log --pretty=oneline --abbrev-commit c32a72d (HEAD -> master, tag: Version_001, origin/master) dev合併至master 7e10494 (dev) 五行修改 2901f6a 應用貯藏後dev分支修改後提交 82f6844 temp1合併至master 29ecde1 (temp1) 五行修改 ...... #在指定的commit上打上標籤 $ git tag Version_002 2901f6a #查看全部標籤 $ git tag Version_001 Version_002 #查看標籤信息 $ git show Version_002 commit 2901f6a13aa1a66eb122543cc9d052c151cbb2b0 (tag: Version_002) Author: *** Date: *** 應用貯藏後dev分支修改後提交 diff --git a/originPractise.txt b/originPractise.txt index bae6658..8290d74 100644 --- a/originPractise.txt +++ b/originPractise.txt @@ -5,4 +5,5 @@ 測試一下衝突! #在指定的commit上打上標籤 並添加標籤的說明 $ git tag -a Version_003 -m "用來測試填寫有說明文字的標籤" 7e10494 $ git show Version_003 tag Version_003 Tagger: *** Date: *** 用來測試填寫有說明文字的標籤 commit 7e104945ce32611ab687477d23885e948afeadb0 (tag: Version_003, dev) Author: *** Date: *** 五行修改
學習git push origin tagname
、git push origin --tags
、git tag -d tagname
、git push origin :refs/tags/tagname
等命令。
#查看全部標籤 $ git tag Version_001 Version_002 Version_003 #推送標籤到遠程 $ git push origin Version_001 Total 0 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-3.8] To https://gitee.com/***/***.git * [new tag] Version_001 -> Version_001 #刪除本地標籤 $ git tag -d Version_003 Deleted tag 'Version_003' (was 9c95d1a) #查看全部標籤 $ git tag Version_001 Version_002 #將全部標籤推送到遠程 推送後能夠經過碼雲看是否推送成功 $ git push origin --tags Total 0 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-3.8] To https://gitee.com/***/***.git * [new tag] Version_002 -> Version_002 #刪除已推送到遠程的標籤 先刪除本地的標籤 $ git tag -d Version_002 Deleted tag 'Version_002' (was 2901f6a) #再刪除遠程的標籤 刪除後可上碼雲上看是否刪除成功 $ git push origin :refs/tags/Version_002 remote: Powered by GITEE.COM [GNK-3.8] To https://gitee.com/***/***.git - [deleted] Version_002
#初始化倉庫 $ git init #將文件添加到倉庫 $ git add <file> #提交到倉庫 $ git commit -m <message> #時刻掌握倉庫當前的狀態 $ git status #查看文件的不一樣 查看difference $ git diff <file> #查看提交歷史 簡潔版的 git log --pretty=oneline $ git log (--pretty=oneline) #版本回退 $ git reset --hard <commit_id> #記錄每一次命令 歷史命令 $ git reflog #丟棄工做區的修改 $ git checkout -- <file> #丟棄暫存區上的修改,將它放回到工做區 $ git reset HEAD <file> #刪除文件 $ git rm <file>
#關聯遠程庫 $ git remote add origin git@server-name:path/repo-name.git #建立dev分支並切換 $ git checkout -b dev #查看當前分支 $ git branch #切換至master分支 $ git checkout master #合併dev分支到當前分支(master) $ git merge dev #刪除dev分支 $ git branch -d dev #禁止使用Fast forward方式合併 $ git merge --no-ff -m "提交時的描述" dev #將工做區修改的文件貯藏 $ git stash #查看暫存的文件列表 $ git stash list #應用貯藏的文件並刪除 $ git stash pop #恢復貯藏的文件後不刪除 $ git stash apply #刪除貯藏的文件 $ git stash drop #將已經提交的分支合併至當前分支。 $ git cherry-pick <commit> #刪除未進行合併操做的分支 $ git branch -D name #查看遠程庫信息 $ git remote #更詳細的遠程庫信息 $ git remote -v #推送信息到遠程倉庫 $ git push origin master #從遠程拉取信息 $ git pull #建立本地分支和遠程分支的連接 $ git branch --set-upstream-to <branch-name> origin/<branch-name> #把本地未push的分叉提交歷史整理成直線以後在push $ git rebase
#在默認分支上打上標籤 默認是HEAD $ git tag tagname (commit id) #查看全部標籤 $ git tag #查看標籤信息 $ git show tagname #在指定的commit上打上標籤 並添加標籤的說明 $ git tag -a tagname -m "標籤的說明文字" 7e10494 #推送標籤到遠程 $ git push origin tagname #將全部標籤推送到遠程 推送後能夠經過碼雲看是否推送成功 $ git push origin --tags #刪除本地標籤 $ git tag -d tagname #再刪除遠程的標籤 刪除後可上碼雲上看是否刪除成功 $ git push origin :refs/tags/tagname
#版本回退 $ git reset --hard <commit_id> #丟棄暫存區上的修改,將它放回到工做區 HEAD $ git reset HEAD <file> #建立並切換分支 $ git checkout -b <name> $ git switch -c <name> #切換分支 $ git checkout <name> $ git switch <name>
本次博文參考廖雪峯大佬的Git教程,附上連接:https://www.liaoxuefeng.com/wiki/896043488029600 !
原創不易,歡迎轉載,轉載時請註明出處,謝謝!
做者:瀟~蕭下
原文連接:http://www.javashuo.com/article/p-hdrvfsye-en.html