Git學習心得

本文參考:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

安裝好git配置全局的環境:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"


初始化本地git倉庫:
======================================================
$ git init
初始化空的 Git 倉庫於 /home/champly/learngit/.git/
======================================================


創建一個readme.txt文件:
vim readme.txt =>
Git is a version control system.
Git is free software.


1、把文件添加到倉庫:
======================================================
$ git add readme.txt
======================================================


2、把文件提交到倉庫:
======================================================
$ git commit -m "wrote a readme file"                 
[master (根提交) be5acd4] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt
======================================================
-m: 本次提交的說明
說明:commit可以一次提交很多文件,可以多次add,一次commit


查看現在的git工作區狀態:
======================================================
$ git status
位於分支 master
無文件要提交,乾淨的工作區
======================================================


修改readme.txt文件,之後執行git status:
======================================================
$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內容)
  (使用 "git checkout -- <文件>..." 丟棄工作區的改動)


修改:     readme.txt


修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
======================================================


查看修改的具體內容,git diff:
======================================================
diff --git a/readme.txt b/readme.txt
index 46d49bf..05e6349 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 Git is a version control system.
 Git is free software.
+Git is a distributed version control system.
(END)
======================================================


再次git add readme.txt之後查看git狀態:
======================================================
$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)


修改:     readme.txt


======================================================


再次提交到倉庫:
======================================================
$ git commit -m "add distributed"
[master 72eac27] add distributed
 1 file changed, 1 insertion(+)
======================================================


git log查看歷史記錄:
======================================================
commit 831e12e0807a7824ee05967b691aa9532726df24
Author: ChamPly <[email protected]>
Date:   Fri Aug 18 14:45:43 2017 +0800


    version 3


commit 72eac27770531f96b717fce93349f8e93c7e67b8
Author: ChamPly <[email protected]>
Date:   Fri Aug 18 14:42:45 2017 +0800


    add distributed


commit be5acd46cfbcb7d50a1a6d248cde553f53515e68
Author: ChamPly <[email protected]>
Date:   Fri Aug 18 14:06:05 2017 +0800


    wrote a readme file
(END)
======================================================


git log --pretty=oneline簡化歷史記錄輸出信息:
======================================================
831e12e0807a7824ee05967b691aa9532726df24 version 3
72eac27770531f96b717fce93349f8e93c7e67b8 add distributed
be5acd46cfbcb7d50a1a6d248cde553f53515e68 wrote a readme file
(END)
======================================================




退回"add distributed"版本的內容:
======================================================
$ git reset --hard 72eac27770531f96b717fce93349f8e93c7e67b8
HEAD 現在位於 72eac27 add distributed
======================================================
git log:
======================================================
commit 72eac27770531f96b717fce93349f8e93c7e67b8
Author: ChamPly <[email protected]>
Date:   Fri Aug 18 14:42:45 2017 +0800


    add distributed


commit be5acd46cfbcb7d50a1a6d248cde553f53515e68
Author: ChamPly <[email protected]>
Date:   Fri Aug 18 14:06:05 2017 +0800


    wrote a readme file
======================================================


退回到上一個版本,hard後面可以只輸入一部分:
======================================================
# champly @ champly in ~/learngit on git:master o [15:25:43] 
$ git reset --hard 831e
HEAD 現在位於 831e12e version 3


# champly @ champly in ~/learngit on git:master o [15:26:19] 
$ cat readme.txt 
Git is a version control system.
Git is free software distributed under the GPL.
Git is a distributed version control system.
======================================================


查看git的日誌:git reflog:
======================================================
831e12e HEAD@{0}: reset: moving to 831e
72eac27 HEAD@{1}: reset: moving to 72eac27770531f96b717fce93349f8e93c7e67b8
831e12e HEAD@{2}: commit: version 3
72eac27 HEAD@{3}: commit: add distributed
be5acd4 HEAD@{4}: commit (initial): wrote a readme file
(END)
======================================================


git log 查看提交歷史
git reflog 查看命令歷史


git add 把文件添加進去,實際上就是把文件修改添加到暫存區
git commit 提交更改,實際上就是把暫存的所有內容提交到當前分支


修改readme.txt文件,添加一個新文件license,然後執行git status:
======================================================
$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內容)
  (使用 "git checkout -- <文件>..." 丟棄工作區的改動)


修改:     readme.txt


未跟蹤的文件:
  (使用 "git add <文件>..." 以包含要提交的內容)


license


修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
======================================================


git checkout --filename,可以丟棄工作區的修改
一種是自然修改後沒有被放到暫存去,現在,撤銷修改就是回到和版本庫一模一樣的狀態;
一種是已經添加到暫存區後,又作了修改,現在,撤銷修改就回到添加到暫存區後的狀態。
總之,就是讓這個文件回到最近一次git commit或git add時的狀態。


刪除一個文件,然後git status:
======================================================
$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add/rm <文件>..." 更新要提交的內容)
  (使用 "git checkout -- <文件>..." 丟棄工作區的改動)


刪除:     test.txt
======================================================
使用 git rm test.txt:
======================================================
$ git rm test.txt
rm 'test.txt'
======================================================
然後 git status:
======================================================
$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)


刪除:     test.txt
======================================================
要使用git commit才能提交刪除




遠程倉庫(github.com):
將本地倉庫和github倉庫關聯起來
git remote add origin [email protected]:accountName/learngit.git
把本地倉庫的內容推送到遠程
(git pull origin master --allow-unrelated-histories 如果本地和遠程分支不一樣的話加上這個)
git push -u origin master
加上-u會把本地master分支內容推送到遠程新的master分支,還會把本地的master分支和遠程的master分支關聯起來,
以後使用:git push origin master:
======================================================
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 259 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:champly/learngit.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
======================================================
就可以把本地最新的master分支推送到github上面
總結:
關聯遠程倉庫:git remote add origin git@server-name:path/repo-name.git
第一次推送master分支所有內容:git push -u origin master
以後修改:git push origin master




從遠程倉庫獲取代碼:
git clone [email protected]:accountName/learngit.git:
======================================================
$ git clone [email protected]:ChamPly/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
======================================================
git支持多種協議,包括https,ssh,但是通過ssh支持原生git協議速度最快




git創建分支,git checkout -b:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master o [21:53:31]
$ git checkout -b develop
Switched to a new branch 'develop'


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop o [21:54:58]
$
======================================================
git checkout 加上-b表示創建並切換,相當於:
git branch develop
git checkout develop


git branch 查看當前分支:
======================================================
$ git branch
* develop
  master
======================================================


git merge develop把develop分之合併到master:
======================================================
$ git merge develop
Updating da671bb..f487454
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
======================================================


git branch -d develop,刪除develop分之:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master o [22:00:39]
$ git branch -d develop
Deleted branch develop (was f487454).


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master o [22:01:10]
$ git branch
* master
======================================================
因爲創建,合併分支非常快,所以git鼓勵你使用分支完成某個任務,合併分支後再刪掉,這和志傑在master分支上工作效果是一樣段,但過程更安全。
總結:
git branch 查看分支
git branch name 創建分支
git checkout name 切換分支
git checkout -b name 創建+切換分支
git merge name 合併某分支到當前分支
git branch -d name 刪除分支


創建一個feature分支,修改readme.txt,提交,切換master,修改readme.txt提交:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:feature x [22:10:41]
$ git commit -m "change simple"
[feature 5bd589f] change simple
 1 file changed, 1 insertion(+), 1 deletion(-)


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:feature o [22:10:54]
$ 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)


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master o [22:11:07]
$ vim readme.txt


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master x [22:11:27]
$


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master x [22:11:27]
$ git add readme.txt


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master x [22:11:33]
$ git commit -m "blank end"
[master ac63ece] blank end
 1 file changed, 1 insertion(+), 1 deletion(-)
======================================================


然後合併,產生衝突:
======================================================
$ git merge feature










Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
======================================================


查看git status:
======================================================
$ 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:   readme.txt


no changes added to commit (use "git add" and/or "git commit -a")
$ cat readme.txt
Git is a free software
<<<<<<< HEAD
Creating a new branch is quick and simple .
=======
Creating a news branch is  quick and simple.
>>>>>>> feature
======================================================


修改之後在提交,然後看看修改分支的情況:
======================================================
$ git log --graph --pretty=oneline --abbrev-commit
*   6aa11ea conflict fixed
|\
| * 5bd589f change simple
* | ac63ece blank end
|/
* 1e98fab and simple
* f487454 branch
* da671bb add readme file
(END)
======================================================
git log --graph 可以看到分支合併圖
git merge --no-ff -m "commit content" name:使用普通模式合併分支




修復bug,創建分支,保存當前修改內容:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:26:29]
$ git stash
Saved working directory and index state WIP on develop: 6aa11ea conflict fixed
HEAD is now at 6aa11ea conflict fixed


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop o [22:26:33]
$ git status
On branch develop
nothing to commit, working tree clean


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop o [22:26:49]
$ 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)


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master o [22:27:50] C:129
$ git checkout -b issue-101
Switched to a new branch 'issue-101'


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:issue-101 x [22:29:23]
$ git checkout master
M readme.txt
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master x [22:30:31] C:129
$ git merge --no-ff -m "merged bug fix 101" issue-101
Already up-to-date.


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:master x [22:30:40]
$ git checkout develop
M readme.txt
Switched to branch 'develop'


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:31:40]
$ git status
On branch develop
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")


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:31:49]
$ git stash list
stash@{0}: WIP on develop: 6aa11ea conflict fixed
(END)


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop o [22:34:14]
$ git stash apply
On branch develop
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")


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:34:19]
$ git stash drop
Dropped refs/stash@{0} (011f14fc4edc713c933c526a3808275d17e242b7)
======================================================


工作現場還在,git把stash內容存在某個地方了恢復可以使用git stash apply(恢復) + git stash drop(刪除)
另外一種方式就是git stash pop
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:34:43]
$ git stash
Saved working directory and index state WIP on develop: 6aa11ea conflict fixed
HEAD is now at 6aa11ea conflict fixed


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop o [22:35:04]
$ git stash pop
On branch develop
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")
Dropped refs/stash@{0} (29581d03d69cde7e5407f8fbe260e907ab2896ac)
======================================================
git stash隱藏修改,git stash pop恢復修改




git branch -D name 刪除分支,丟棄修改,強行刪除




git 查看遠程倉庫信息:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:38:04]
$ git remote
origin


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:40:33]
$ git remote -v
origin [email protected]:ChamPly/learngit.git (fetch)
origin [email protected]:ChamPly/learngit.git (push)
======================================================
如果沒有推送權限,就看不到push的地址


從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠程的新提交
建立本地分支和遠程分支的關聯,使用 git branch --set-upstream branch-name origin/branch-name


git添加標籤:
======================================================
# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:42:31]
$ git tag v1.0


# champly @ ChamPlydeMacBook-Pro in ~/src/go/learngit on git:develop x [22:47:57]
$ git tag
v1.0
======================================================
                                                      2017年8月20日
                                                        by:ChamPly
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章