Git的使用總結

Python 2.7
MacOS Sierra 10.12.1


前言

實習中的第一個小分享,剛好彌補了我想學git的一些需求,主要還是公司的gitlab代碼管理相關,以後自己也可以上傳github了


git的安裝

採用HomeBrew安裝,HomeBrew官網 http://brew.sh/

  1. 下載HomeBrew打開終端,輸入
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如報錯提示如下

Error: /usr/local/Cellar is not writable.

則再次在終端輸入

sudo chown -R $USER /usr/local
  1. 安裝git,仍在原來的終端輸入
didi@localhost:/$ brew install git

待安裝後,輸入如下命令即可確認完成安裝

didi@localhost:/$ brew list
git lrzsz openssl thefuck wget

常用指令集合,本地倉庫管理與更迭版本

首先創建一個倉庫(文件夾),這裏爲gittest,然後初始化倉庫

didi@localhost:~/Desktop/gittest$ git init
Initialized empty Git repository in /Users/didi/Desktop/gittest/.git/

此條命令會在本地桌面上的gittest文件夾中創建.git文件夾,默認不可見,用於管理版本,不用修改。

舉個栗子

新建文件,以test.txt爲例,可使用文本編輯器編輯建立,也可以使用vim命令,甚至python腳本來建立,文件格式不限對文件進行編輯,任意內容輸入。然後對這個第一版本進行操作:

  • 第一步是用git add把文件添加進去,實際上就是把文件修改添加到暫存區;
  • 第二步是用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支,也就是版本庫中。
  • 第三步是使用git log 對提交到版本庫的文件進行查看

這樣就完成了一個版本的創立提交,詳細命令如下所示

didi@localhost:~/Desktop/gittest$ git add test.txt #從工作區提交到暫存區
didi@localhost:~/Desktop/gittest$ git commit -m "txtfile V1" #這是對版本進行提交,和sql中的commit差不多,txtfile V1是指提交時候理解爲備註,最好是自己能夠確認出這個版本的添加信息
[master f7444c8] txtfile V1
1 file changed, 1 insertion(+)

版本回滾

使用命令git reset –hard 版本號 ,假設已在原始的第一個版本的txt中重新添加了編輯,之後進行add和commit操作,現在版本庫中就有兩個版本的test.txt文件了,但是,從文件夾中看到的版本是最後HEAD指向的版本

didi@localhost:~/Desktop/gittest$ vim test.txt
didi@localhost:~/Desktop/gittest$ git add test.txt
didi@localhost:~/Desktop/gittest$ git commit -m "txtfile V2"
didi@localhost:~/Desktop/gittest$ git log
commit 28e6401eaf9c2121e9d2c48732770e698787f8b1
Author: didi <shenyucong@didichuxing.com>
Date:   Fri Dec 9 16:59:50 2016 +0800
    txtfile V2

commit d8649ea01e9bc5118ee3e8eb361aae92e1403151
Author: didi <shenyucong@didichuxing.com>
Date:   Fri Dec 9 16:22:35 2016 +0800
    txtfile V1

didi@localhost:~/Desktop/gittest$ git reset --hard d864

如果找不到原來的位置了,使用git reflog來查看

文件刪除

工作區的文件刪除後,但還未刪除版本庫中的文件,可以使用git checkout – filename命令進行恢復文件到工作區

didi@localhost:~/Desktop/gittest$ ls
test.txt
didi@localhost:~/Desktop/gittest$ rm test.txt
didi@localhost:~/Desktop/gittest$ ls
didi@localhost:~/Desktop/gittest$ git status
On branch dev
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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
didi@localhost:~/Desktop/gittest$ git checkout -- test.txt
didi@localhost:~/Desktop/gittest$ ls
test.txt

版本庫中文件刪除

如果要真的刪除數據,需要將版本庫中的一併刪除,使用命令git rm filename然後再從commit

didi@localhost:~/Desktop/gittest$ git rm testpy.py
rm 'testpy.py'
didi@localhost:~/Desktop/gittest$ git commit -m "del testpy.py"
[dev e313c41] testpy.py
1 file changed, 1 deletion(-)
delete mode 100644 testpy.py

版本merge

版本之間的merge,在主線master進行merge合併,會提示兩條線路之間差異,主線merge後可刪除支線

didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

didi@localhost:~/Desktop/gittest$ ls
test.txt testpy.py
didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'

didi@localhost:~/Desktop/gittest$ ls
test.txt
didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

didi@localhost:~/Desktop/gittest$ git merge dev
Updating 28e6401..e313c41
Fast-forward
testpy.py | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 testpy.py

didi@localhost:~/Desktop/gittest$ ls
test.txt

支線刪除

如果在dev這條支線上再新建一條新功能支線feature作爲新加入的需求,但是在最後關頭,feature支線以commit,還未merge時,需求不再需要,那麼刪除支線則需要使用-D而不是-d


didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'
didi@localhost:~/Desktop/gittest$ git branch -d feature
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
didi@localhost:~/Desktop/gittest$ git branch -D feature
Deleted branch feature (was c126095).
didi@localhost:~/Desktop/gittest$ git branch
* dev
master

Stash保存工作臺

工作未完成跳轉分支,保存工作臺
如果還有任務在一個分支上還未完成,且沒有提交,需要緊急在另一個支線上修復bug,合併入master主線,則需要使用stash命令,將這一支線的狀態保存下來,待日後進行處理,不然將無法回到主線上。


didi@localhost:~/Desktop/gittest$ vi test.txt
didi@localhost:~/Desktop/gittest$ git add test.txt
didi@localhost:~/Desktop/gittest$ git status
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: test.txt

didi@localhost:~/Desktop/gittest$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
test.txt
Please commit your changes or stash them before you switch branches.
Aborting
didi@localhost:~/Desktop/gittest$ git stash
Saved working directory and index state WIP on dev: 41ee99f unfinshwork
HEAD is now at 41ee99f unfinshwork
didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

在主線上另開一條支線專門用來修復bug,之後合併merge主線,刪除修復bug的那條支線,之後切回原來的工作臺支線,使用git stash pop命令恢復工作臺,恢復的同時把stash內容也刪除了.值得注意的是,當沒有使用stash恢復時候,工作臺上並沒有狀態,需要將stash保存的內容恢復回來纔會有。

didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'
didi@localhost:~/Desktop/gittest$ git status
On branch dev
nothing to commit, working tree clean
didi@localhost:~/Desktop/gittest$ git stash list
stash@{0}: WIP on dev: 41ee99f unfinshwork
didi@localhost:~/Desktop/gittest$ 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: test.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (254dd1c1d5ebcee38672435b082a58c3b732fde7)
didi@localhost:~/Desktop/gittest$ 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: test.txt

no changes added to commit (use "git add" and/or "git commit -a")


GitLab的使用

這裏簡單介紹下如何push自己的project到gitlab上,如果是新的project,建議先在gitlab上新建一個project,然後clone下來,之後將自己的project文件全部複製到clone的文件夾中,之後執行

這裏因爲公司原因,切掉了自己的下載地址,不過內網啦,別人也訪問不了,這裏是下載了gitlab上自己的Doubancrawl工程

add和commit進行提交,然後執行push進行雲端提交,這裏示例一下,(用了win的git bash,mac的git可以在shell中操作,步驟代碼相同)

這裏寫圖片描述

更多使用請詳見: http://blog.csdn.net/huaishu/article/details/50475175


Git 上傳分支-協同工作

一般來說,一個project提供給別人clone的是一個master主線上的東西,如果再次上傳支線,將不會被別人所clone下來,比如我在另一臺機器push自己的分支,那麼分支被
push上去之後,如果沒有merge到master,那麼分支不會被clone。如果需要clone其分支進行二次開發,需要使用語言git checkout -b LOCALBRANCH origin/EXISTBRANCH其中LOCALBRANCH是需要在本地進行開發的branch,而EXISTBRANCH是gitlab端存在的分支,即需要進行再二次開發的分支。
比如,我在gitlab上已有分支如圖,其中一個master是主分支,而其餘兩個是分支,我現在要對newfeature這個分支進行再開發

image

需要執行的命令如下,包括clone其master分支,之後與遠程newfeature建立連接,在本地執行需要二次開發的分支進行操作,之後push分支即可

這裏寫圖片描述

執行之後的效果如下

image


Git Tips–自動補全

Git通過bash-completion軟件包實現命令補齊,在Mac OS X下可以通過Homebrew進行安裝。

didi@localhost:brew search completion
bash-completion
didi@localhost:brew install bash-completion

根據bash-completion安裝過程中的提示,修改文件~/.bash_profile文件,並在其中加入如下內容,以便在終端加載時自動啓用命令補齊。


if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

或者使用git源碼進行安裝自動補全,之後重啓終端即可自動補全git命令


didi@localhost:~$ git clone https://github.com/git/git.git
didi@localhost:~$ cp git/contrib/completion/git-completion.bash ~/.git-completion.bash
didi@localhost:~$ source ~/.git-completion.bash

Git Tips–圖形化界面

詳見:https://my.oschina.net/amstrong/blog/159114 總結不錯,不再贅述

Git Tips–查看主線支線合併情況

image

使用命令git log –graph –pretty=oneline –abbrev-commit

didi@localhost:~/Desktop/gittest$ git log --graph --pretty=oneline --abbrev-commit
* 53eea08 submit
|\
| * 41ee99f unfinshwork
| * 15b3b98 dev change test
| * f702c04 testdevo
| * 191a958 rm stash.txt
| * 74b027c stash
| * 76e61c4 unchangefile
* | f2b569b fix the bug
* | c8e48c9 another
* | c4f7a47 another
* | 5a9f011 finish job
* | ecdde9e fix the bugs from issue101
|/

Git Tips–多端訪問同一賬戶

比較實際的例子,比如我需要在另一臺電腦上上傳一個project到自己的gitlab上,那麼需要這臺電腦生成的ssh keys進行登記,在win上產生ssh key代碼如下

ssh-keygen -t rsa -C "[email protected]"

命令中的[email protected]替換爲你註冊gitlab時用的Email地址。
按照提示,一直回車,然後ssh key生成,之後將生成的id_rsa.pub添加到gitlab上的ssh key即可
直接使用命令將id_rsa.pub文件裏的內容複製到剪切板中的命令:

clip < ~/.ssh/id_rsa.pub

然後複製過去就可以了同理可以使用n多臺電腦push工程到project,只需要提前添加ssh key即可


Git的fork別人的project,修改並提交自己倉庫

注意要先fork自己的倉庫,才能push

建議查看:http://www.cnblogs.com/muzinan110/p/5300600.html


remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
didi@localhost:~/Desktop$ ls
GitLabTest               xxxxxxxx
expect5.45.tar.gz        login                    test.py
SecureCRT731             check                    gittest                  login_public.sh          滴滴實習相關
SecureCRT731.zip         expect                   hadoop_test              shell_test               員工入職指導手冊
didi@localhost:~/Desktop$ cd xxxxxxxx/
didi@localhost:~/Desktop/xxxxxxxx$ ls
create_hive.sql eta_main.sh
didi@localhost:~/Desktop/xxxxxxxxt$ vi pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ ls
create_hive.sql    eta_main.sh        pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxxt$ git add pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ git commit -m "add pushnewfeatue.txt"
[master f2d0938] add pushnewfeatue.txt
 1 file changed, 2 insertions(+)
 create mode 100644 pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ 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
didi@localhost:~/Desktop/xxxxxxxx$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 326 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
.........
Branch master set up to track remote branch master from origin.

Git構建功能性分支進行開發

  • Step 1:源倉庫的構建

這一步通常由項目發起人(項目管理員)來操作,源倉庫爲op/Chanjet_Asset_Management,並初始化兩個分支master和develop.

  • Step 2:開發者fork源倉庫

源倉庫建立以後,每個開發就可以去複製一份源倉庫到自己的gitlab賬號中,然後作爲自己開發所用的倉庫。

  • Step 3:把自己開發者倉庫clone到本地
  • Step 4:構建功能分支進行開發

對於Step4,具體的栗子:

didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
didi@localhost:~/Desktop/xxxxxxxx$ git status
On branch develop
nothing to commit, working tree clean
didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
didi@localhost:~/Desktop/xxxxxxxx$ git checkout developissue
Switched to branch 'developissue'
didi@localhost:~/Desktop/xxxxxxxx$ vi issue.txt
didi@localhost:~/Desktop/xxxxxxxx$ git add .
didi@localhost:~/Desktop/xxxxxxxx$ git commit -m "change 4"
[developissue 8fbf26b] change 4
 1 file changed, 1 insertion(+)
didi@localhost:~/Desktop/xxxxxxxx$ git checkout develop
Switched to branch 'develop'
didi@localhost:~/Desktop/xxxxxxxx$ git merge --no-ff -m "merge developissue" developissue
Merge made by the 'recursive' strategy.
 issue.txt | 1 +
 1 file changed, 1 insertion(+)
didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
change 4
didi@localhost:~/Desktop/xxxxxxxx$ git branch
* develop
  developissue
  master
didi@localhost:~/Desktop/xxxxxxxx$ git branch -d developissue
Deleted branch developissue (was 8fbf26b).
didi@localhost:~/Desktop/xxxxxxxx$ git branch
* develop
  master
didi@localhost:~/Desktop/xxxxxxxx$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
didi@localhost:~/Desktop/xxxxxxxxt$ git push -u origin develop
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (16/16), 1.46 KiB | 0 bytes/s, done.
Total 16 (delta 9), reused 0 (delta 0)
......
 * [new branch]      develop -> develop
Branch develop set up to track remote branch develop from origin.

之後就可以看到push上了一個develop分支

image

和master不一樣的是,develop中多了一項issue.txt文件,這就完成了新功能得提交

  • Step 5:向管理員提交pull request

    在完成了“討論”功能(當然,也可能對自己的develop進行了多次合併,完成了多個功能),經過測試以後,覺得沒問題,就可以請求管理員把自己倉庫的develop分支合併到源倉庫的develop分支中

  • Step 6 管理員測試、合併

管理員登陸gitlab,看到了開發者對源倉庫發起的pull request。
管理員需要做的事情就是
1. 對開發者的代碼進行review。
2. 在他的本地測試新建一個測試分支,測試開發者的代碼:

判斷是否同意合併到源倉庫的develop中,如果經過測試沒問題,可以把開發者的代碼合併到源倉庫的develop中


最後

gitlab也好,github也好,最終只是個版本管理工具,現在我的使用只是當做高逼格版的百度雲而已,也就是個倉庫,或許以後進行代碼協助開發的時候纔會體會到gitlab的強大吧,科科~


PS:祝大家2017新年快樂!也祝自己在想要的道路上越走越遠!大家共勉!

發佈了97 篇原創文章 · 獲贊 424 · 訪問量 92萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章