前端資源系列(1)-Git經常使用命令&設置快捷命令&小工做流

歡迎提issues斧正喜歡star:Git經常使用命令&設置快捷命令html

Git-經常使用命令&快捷命令&小工做流

Git 是一個很強大的分佈式版本控制系統。它不但適用於管理大型開源軟件的源代碼,管理私人的文檔和源代碼也有不少優點。在團隊合做時做用不言而喻,更是爲了解決團隊合做代碼衝突而生。git

Git經常使用命令

初始化新版本庫:git init
全局設置:git config --global user.name "xzavier"  git config --global user.email "xzavier.xxx.com"
克隆版本庫:git clone "url"
查看分支:git branch
建立分支:git branch branch_name
切換分支:git checkout branch_name
建立+切換分支:git checkout -b branch_name
合併某分支到當前分支:git merge branch_name
重命名分支:git branch -m branch_name branch_new_name //不會覆蓋已經存在的分支
重命名分支:git branch -M branch_name branch_new_name //會覆蓋已經存在的分支
刪除分支:git branch -d branch_name 
強制刪除分支: git branch -D branch_name
刪除遠程分支: git push origin : branch_name 
拉取代碼:git pull origin branch_name
查看更改:git status 
查看更改細節: git diff file_name
查看誰修改過代碼: git blame filename
回到上次修改: git reset --hard
添加單個文件:git add filename.js 
添加全部js文件:git add *.js
添加全部文件:git add .
提交添加的文件:git commit -m "your description about this branch"
提交單個文件:git commit -m "your description about it" filename.js
push分支:git push origin your_branch_name
備份當前分支內容:git stash //在後面再講講這個
查看歷史記錄:git log
建立標籤:git tag 1.0.0  //標籤沒法重命名
顯示標籤列表:git tag 
切出標籤:git checkout 1.0.0
刪除標籤:git tag -d 1.0.0
查看git遠程網址:git remote -v
更改git遠程網址:git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git  ; git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git

git遠程網址參考:Changing a remote's URLgithub

設置Git快捷命令

若是你用的是 git bash 或者 XSHell 的話,在記住了命令含義的狀況下,工做時鍵入快捷命令會提升工做效率。
在git bash中進入c://User/user,執行touch .bashrc,而後在此目錄下會生成一個.bashrc文件,將下面的快捷命令拷入保存便可,快捷命令可自行修改添加,主要是本身習慣和喜歡。
在XSHell裏快捷命令是在~/.bashrc中配置,在XSHell中 vim ~/.bashrc,把以上命令加到合適的位置保存(:wq)便可。vim

alias gs='git status'
alias gd='git diff'
alias ga='git add'
alias gc='git commit'
alias gck='git checkout'
alias gb='git branch'
alias gl='git log'
alias gthis='git rev-parse --abbrev-ref HEAD'
alias gpushthis='git push origin `gthis`'
alias gpullthis='git pull origin `gthis`'
alias gup='git remote update'
alias gpl='git pull origin'

git小工做流

早上來上班,處理好郵件,開完早會,打開虛擬機,切到master分支,拉一下代碼:bash

git pull origin develop/master

開始寫代碼。固然,做爲團隊一員,確定不能在master分支上隨便寫代碼。分佈式

新建分支:工具

git checkout -b branch_name(分支名按照必定規範會很好)

而後工做安安靜靜的寫代碼,這時候若是有別的分支上須要處理點緊急bug什麼的,又不能如今提交代碼。學習

那就先保存起來吧:this

git stash  (見後面git status)

切到別的分支修改代碼 :url

git checkout -b branch_name

修復bug後提交代碼查看修改:

git status

須要查看修改的細節:

git diff file_name

沒有問題了,那就提交吧(三部曲):

git add file_name (通常來講你能夠 git add . 點符號表明全部修改文件) 
git commit -m "your description about this branch" 
git push origin your_branch_name

bug算是解決了,那就回到正常的工做吧,切回原來的分支:

git chekcout -b your_old_branch

恢復剛剛保存的內容: git stash pop (至於這個pop,詳細須要本身去找官網或者博客學習,簡單介紹

git stash: 備份當前的工做區的內容,保存到Git棧中。
git stash pop: 從Git棧中讀取最近一次保存的內容,恢復工做區的相關內容。因爲可能存在多個Stash的內容,因此用棧來管理,pop會從最近的一個stash中讀取內容並恢復。
git stash list: 顯示Git棧內的全部備份,能夠利用這個列表來決定從那個地方恢復。
git stash clear: 清空Git棧。此時使用git等圖形化工具會發現,原來stash的哪些節點都消失了。

代碼恢復了,就開始工做了。代碼寫完了,又是提交三部曲(快捷命令)。

ga .
gc -m "your description about this branch" 
gpullthis

最後合併到master,多個隊員一個開發,通常合併master時要遇到衝突,這時相應開發人員就要出來解決衝突,衝突通常是多個開發員修改了同一處代碼形成的。拉取master代碼(隊友也沒合master就商量好拉他的代碼,最後解決完衝突只把你這個分支提交就好了):

git pull origin master

或者

git pull origin his/her_branch_name

出現:

Auto-merging xzavier.js CONFLICT (content): Merge conflict in xzavier.js

看到conflict(衝突),找到相應文件,出現這樣的地方:

1<<<<<<< HEAD 
     your code
     ===============
     others code
    1>>>>>>>

解決完衝突後提交便可,提交三部曲。

ga .
    gc -m "your description about this branch" 
    gpullthis

阮老師的Git教程:經常使用 Git 命令清單

相關文章
相關標籤/搜索