這是一個極為簡單的git筆記,只包含相關命令
1.git安裝后自報家門,姓名和郵箱
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
2.創建工作區,初始化工作區
$ pwd /c/notos/code/gitpractice 74565@jason MINGW64 /c/notos/code/gitpractice $ git init Initialized empty Git repository in C:/notos/code/gitpractice/.git/
3添加新文件並添加到緩存區,並commit
$ git add readme.txt //為什么會有add commit兩步驟呢,你可以添加很多次文件然后依次commit warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory. 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git commit -m "create readme.txt" // -m 是對本次提交的描述,最好是有意義的 [master (root-commit) 33dd7af] create readme.txt warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory. 1 file changed, 2 insertions(+) create mode 100644 readme.txt
4.查看git 狀態
剛提交完時查詢 $ git status On branch master nothing to commit, working directory clean 修改readme文件后 $ 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: readme.txt no changes added to commit (use "git add" and/or "git commit -a") add文件后 $ git status warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory. On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)//將提交到暫存區的file 撤回,相當於沒有add modified: readme.txt
5.比較工作區修改前后文件,修改未add
$ git diff readme.txt diff --git a/readme.txt b/readme.txt index 46d49bf..9247db6 100644 --- a/readme.txt +++ b/readme.txt @@ -1,2 +1,2 @@ -Git is a version control system. +Git is a distributed version control system. Git is free software. warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory.
6.git查看log
$ git reflog e47dc70 HEAD@{0}: commit: append GPL e3c958d HEAD@{1}: commit: add distributed 33dd7af HEAD@{2}: commit (initial): create readme.txt ---------------------------------------------------------------------------- $ git log commit e47dc706520adc55fd9de97bb911dbd8a406ab19 //commitid 每個人的都不一樣 Author: jasondong <745650624@qq.com> Date: Mon Apr 2 10:25:45 2018 +0800 append GPL commit e3c958de9f539e0caad6e8a2a845c60defff2940 Author: jasondong <745650624@qq.com> Date: Mon Apr 2 10:20:28 2018 +0800 add distributed commit 33dd7afe64ff9e73688de94a1fc61f1b8966207d Author: jasondong <745650624@qq.com> Date: Mon Apr 2 09:58:34 2018 +0800 create readme.txt
7.git版本回退
$ git reflog //版本回退需要知道commitid的前幾位(前七位),所以需要先查看commitid e47dc70 HEAD@{0}: reset: moving to e47dc70 33dd7af HEAD@{1}: reset: moving to 33dd7af e47dc70 HEAD@{2}: commit: append GPL e3c958d HEAD@{3}: commit: add distributed 33dd7af HEAD@{4}: commit (initial): create readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git reset --hard e3c958d //假如要會退到第二版本 HEAD is now at e3c958d add distributed //git 只需簡單的吧指針指向老的版本即可,所以速度很快
8. 取消工作區修改,實際上使用版本庫中文件替換 當前文件內容
在readme 中添加如下內容 Git is a distributed version control system. Git is free software. +My stupid boss still prefer to svn. //新添加內容 ----------------------------------------------------- $ 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: readme.txt no changes added to commit (use "git add" and/or "git commit -a") $ git checkout -- readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ cat readme.txt Git is a distributed version control system. Git is free software.
9.取消暫存區文件的修改
現在修改了文件,而且進行了 git add $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) // 用這個命令即可從暫存區撤銷add modified: readme.txt $ git reset HEAD readme.txt Unstaged changes after reset: M readme.txt
10.刪除版本庫中文件
git rm test.txt //git rm 和git add 其實是等效的,都是對暫存區的操作 git commit -m "rm test.txt"
11.添加遠程倉庫並push
$ git remote add origin git@github.com:jasondong-1/gitpractice.git // 添加遠程倉庫,一般給遠程倉庫命名為origin 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git push -u origin master//第一次向遠程倉庫提交加 -u參數 The authenticity of host 'github.com (13.250.177.223)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts. Counting objects: 10, done. Delta compression using up to 4 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (10/10), 870 bytes | 0 bytes/s, done. Total 10 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To git@github.com:jasondong-1/gitpractice.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
12.從遠程倉庫克隆
$ git clone git@github.com:jasondong-1/gitpractice.git // 后面也可再跟一個名字,即重命名git庫
13.創建新的分支
$ git checkout -b dev // Switched to a new branch 'dev' 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git branch //查看git下所有分支 ,*表示當前分支 * dev master git checkout -b dev 這條命令可以用一下兩條命令替代 $ git branch dev //創建分支 $ git checkout dev //切換分支
14.合並分支
//在新分支上修改文件並提交 $ git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git commit -m "branch test" [dev 84a8f86] branch test 1 file changed, 1 insertion(+) $ git checkout master //首先切換回master分支 Already on 'master' Your branch is up-to-date with 'origin/master'. 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git branch dev * master 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git merge dev // 合並得知 分支到當前分支 git merge --no-ff -m "merge with no-ff" dev 采用非fast-forward方式合並分支 Updating 4b00677..84a8f86 Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
15.刪除分支
$ git branch -d dev //刪除分支 Deleted branch dev (was 84a8f86). 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git branch * master
16.解決沖突
假如在一個新的分支上修改了readme並進行提交,master上也對readme進行了修改,而且修改的是同一行,也commit了,那么合並新分支到master時會報錯 $ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result.//自動合並失敗,手動解決沖突后,重新提交 手動解決沖突航班重新提交 git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master|MERGING) $ git commit -m "fixed conflict" [master 4cdbdc4] fixed conflict
17.查看分支合並圖
$ git log --graph
18.分支策略
分支策略
在實際開發中,我們應該按照幾個基本原則進行分支管理:
首先,master分支應該是非常穩定的,也就是僅用來發布新版本,平時不能在上面干活;
那在哪干活呢?干活都在dev分支上,也就是說,dev分支是不穩定的,到某個時候,比如1.0版本發布時,再把dev分支合並到master上,在master分支發布1.0版本;
你和你的小伙伴們每個人都在dev分支上干活,每個人都有自己的分支,時不時地往dev分支上合並就可以了。
19.git stash 隱藏,必須把修改的文件進行 git add 后 才可以 gits tash
$ git stash Saved working directory and index state WIP on dev: b5e0aa9 new file hello.txt HEAD is now at b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git stash list stash@{0}: WIP on dev: b5e0aa9 new file hello.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (a3e21e6f0042b48f4160e886cf6f79f346f15543) 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git stash list 74565@jason MINGW64 /c/notos/code/gitpractice (dev)
20.git 查看遠程倉庫信息
$ git remote origin 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git remote -v origin git@github.com:jasondong-1/gitpractice.git (fetch) origin git@github.com:jasondong-1/gitpractice.git (push)
21.推送分支(會推送到遠端對應的分支上)
語法:$ git push <遠程主機名> <本地分支名>:<遠程分支名> 若省略遠程分支,代表將本地當前分支推送到遠端對應分支
git push origin master // 推送到遠端的master git push origin dev //推送到遠端的dev 什么樣的分支需要推送到遠端?我認為凡是需要大家共享的分支就需要push到遠端
22. 從遠程倉庫克隆
$ git clone git@github.com:jasondong-1/gitpractice.git //git clone 默認只會把遠端的master克隆下來 $ git branch * master
23.獲取遠端其他分支
$ git checkout -b dev origin/dev //現在有了對應的dev分支 $ git branch * dev master
24.兩人同時修改了同一份文件向遠端push沖突的解決
$ git push origin dev //git不知道該用哪一份文件 To git@github.com:jasondong-1/gitpractice.git ! [rejected] dev -> dev (fetch first) error: failed to push some refs to 'git@github.com:jasondong-1/gitpractice.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again.// git建議push之前先pull hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> //git pull 時報告沒有tracking information,可以用這句pull If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> dev $ git pull origin dev 解決沖突,並重新add commit
25.git打標簽(人類可以識別的標志)
git tag v0.1 33dd7af //33dd7af是commit-id 或 $ git tag -a v0.2 -m "stable edition" e47dc70 查看 tag git tag
26.操作標簽
git push origin <tagname>可以推送一個本地標簽; git push origin --tags可以推送全部未推送過的本地標簽; git tag -d <tagname>可以刪除一個本地標簽; git push origin :refs/tags/<tagname>可以刪除一個遠程標簽。
27..gitignore 可以忽略.gitignore 文件中的文件
# Python: *.py[cod] *.so *.egg *.egg-info dist build
28.遠程倉庫遷移
由於原來的服務器不夠安全,所以遠程git服務器遷移到了其他服務器,為了將本地git和新的遠端git建立連接,需要進行以下幾步操作: 假設舊的遠端名稱叫 origin_old,xnde 遠端名稱叫origin_new 1.將origin_old上的代碼同步到本地 git pull origin master 也可以用ide工具直接操作,比如idea 2.刪除原來的遠端git git remote remove origin_old 3.在新的git服務器創建項目,假設地址叫:xxxx.git 4.建立本地和新遠端的對應關系 git remote add origin_new xxxx.git 5.向新的遠端push代碼 git push origin_new master