我的git 工作流

安裝git

$ yum install git 

使用ssh方式下載代碼

  1. 在當(dāng)前主機(jī)上生成ssl秘鑰對
    一直按回車就可以了。
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
66:90:48:62:24:99:ae:9b:d5:cf:ef:6d:52:a7:82:7b root@uop-test-0cf36b77-f909-4bf8-a728-eb82f379c6bf
The key's randomart image is:
+--[ RSA 2048]----+
|.++ .            |
|oo o . .         |
|.   . o          |
| .     .         |
|.  .    S        |
|. . .  o  . .    |
| +   o . . o     |
|o     + E.o      |
|      .=o+.      |

打開 /root/.ssh/id_rsa.pub,把公鑰復(fù)制添加到 ssh kyes 中。

下載代碼:
這里是下載了一個(gè)uop-backend的代碼

$ git clone git@172.28.4.61:devops/uop-backend.git

查看所有分支:

$ git branch -a

代碼檢出:
建立一個(gè)和遠(yuǎn)程分支同名的本地分支

$ git checkout remotes/origin/mpc  -b origin/mpc

修改代碼:

使用編輯器進(jìn)行編輯,保存。

比較代碼差異

$ git diff

查看修改狀態(tài):

$ git status
# 位于分支 origin/mpc
# 尚未暫存以備提交的變更:
#   (使用 "git add <file>..." 更新要提交的內(nèi)容)
#   (使用 "git checkout -- <file>..." 丟棄工作區(qū)的改動)
#
#       修改:      run.py
#       修改:      uop/auth/handler.py
#

提交之前先更新一些本地代碼

$ git pull --rebase

如果不加 --rebase, git 會自動進(jìn)行merge。如果本地文件和遠(yuǎn)程文件有人同時(shí)修改,可能會有沖突。

配置commit模板:

建立一個(gè)文本文件,這里建立的文本文件為:.commit_template

$ vim .commit_template
 
Feature:/
BugId:/
Description:

配置commit.template

$ git config commit.template .commit_template
或者
$ git config --global commit.template .commit_template

提交之前需要一些配置:

$ git commit

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

比如這里添加了我的公司賬號:

$ git config --global user.email "jiaxiaolei@syswin.com"
$ git config --global user.name "jiaxiaolei"

添加要提交的文件

$ git add run.py

查看修改歷史:

$ git log
commit 137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
Author: jiaxiaolei <jiaxiaolei@syswin.com>
Date:   Tue Jul 25 12:11:19 2017 +0000

    Feture:/ add .commit_template as template of commit
    BugId:/
    Description:

commit a1b3ca5f3e0e5c491d585aa35574b5095d71b379
Merge: 75c2f25 965a188
Author: wuzhenggang <wuzhenggang@syswin.com>
Date:   Tue Jul 25 18:53:30 2017 +0800

    Merge branch 'skin_green' of 172.28.4.61:devops/uop-frontend into skin_green

一個(gè)我自己喜歡的git log 配置:

$ git log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative --show-signature

有圖表,配色也比較喜歡。

查看某次修改的內(nèi)容:

git show <commit-hash-id> 查看某次commit的修改內(nèi)容

$ git show  137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
commit 137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
Author: jiaxiaolei <jiaxiaolei@syswin.com>
Date:   Tue Jul 25 12:11:19 2017 +0000

    Feture:/ add .commit_template as template of commit
    BugId:/
    Description:

diff --git a/.commit_template b/.commit_template
new file mode 100644
index 0000000..5bb8ad5
--- /dev/null
+++ b/.commit_template
@@ -0,0 +1,3 @@
+Feture:/
+BugId:/
+Description:

配置自己的git alias

oh-my-zsh 下的自動補(bǔ)全非常好用,有了zsh, 幾乎不需要自定義git alias 了。

oh-my-zsh 推薦的一組配置文件為:
https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet#git

  1. 命令行修改:
    或者直接利用全局命令修改即可,格式如下

$ git config --global alias.別名 '指定代碼'

  1. 編輯配置文件:


[user]
    name = jiaxiaolei
    email = jiaxiaolei@cicaero.com
[commit]
    template = .commit_template.commit_template
[alias]
        st = status
        ci = commit
        co = checkout
        br = branch
        unstage = reset HEAD --
        last = log -1 HEAD
    push-for-review = push origin dev:refs/for/dev
    push-for-review-luckyair-dev = push origin HEAD:refs/for/luckyair-dev
[gui]
    recentrepo = /Users/jiaxiaolei/Documents/git_project/cic-push

配置舉例:

[alias] 
lg = log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative --show-signature 
# 對log 做了自定義。有合并圖表,配色也比較漂亮。

st = status 
l = log --pretty=oneline -n 20 --graph --abbrev-commit 
#NOTE: 比較簡陋
ll = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -- 
#NOTE: 不如第一個(gè) 
b = branch 
ci = commit 
ca = commit -a 
pl = pull 
ps = push 
co = checkout 

log --graph --decorate --pretty=oneline --abbrev-commit --all
#NOTE: 一般

plr = git pull --rebase

我自己的:

[alias] 

b = branch 
ci = commit 
ca = commit -a 
pl = pull 
ps = push 
co = checkout 
lg = log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative --show-signature 
# 對log 做了自定義。有合并圖表,配色也比較漂亮。
plr = pull --rebase

擴(kuò)展閱讀:

你們 git 的命令都 alias 成什么了
https://www.v2ex.com/t/112237
簡介:

v2ex上的討論和回復(fù)。

比較經(jīng)典的的配置:

pro git book:

Github秘籍:
https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.zh-cn.md#%E9%94%AE%E7%9B%98%E5%BF%AB%E6%8D%B7%E9%94%AE
簡介:
這里有很多github 的使用技巧。其中也包含 git alas.

on-my-zsh
https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet#git
簡介:
zsh對shell做了自定義。

定義了很多alias, 比如git, tmux, systemd上的alias.
可以參考一下,選擇性使用。

暫存本地修改

$ git stash save logging
Saved working directory and index state On develop: logging
HEAD 現(xiàn)在位于 3039b7f delete duplicate url registry

$ git stash list
stash@{0}: On develop: logging
stash@{1}: WIP on develop: b8b70e9 fix bug

$ git stash apply stash@{0}
自動合并 requirements.txt
沖突(內(nèi)容):合并沖突于 requirements.txt
自動合并 cmdb/settings.py
沖突(內(nèi)容):合并沖突于 cmdb/settings.py

取消本地的一個(gè)修改:

$ git checkout -- utils/cobbler_api.py

查看修改記錄

僅僅想看最近誰有提交,以及提交的描述
$ git log

只查看某個(gè)文件的修改,比如server.py 
git log  server.py


僅僅想看最后幾次次的提交
比如,最近3次
$ git log -n 3


想看到最近一次提交所有更改過的文件

git log -n 1 --stat


想看到最近一次提交所有更改的細(xì)節(jié),包括修改的文件,文件中的修改細(xì)節(jié)。
git log -n 1 -p


合并到主分支:



常見問題和解決

  • 問題:

git push
warning: push.default 未設(shè)置,它的默認(rèn)值將會在 Git 2.0 由 'matching'
修改為 'simple'。若要不再顯示本信息并在其默認(rèn)值改變后維持當(dāng)前使用習(xí)慣,
進(jìn)行如下設(shè)置:

git config --global push.default matching

若要不再顯示本信息并從現(xiàn)在開始采用新的使用習(xí)慣,設(shè)置:

git config --global push.default simple

參見 'git help config' 并查找 'push.default' 以獲取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有時(shí)要使用老版本的 Git,
為保持兼容,請用 'current' 代替 'simple' 模式)

Everything up-to-date

  • 解決:

(py2.7.13mpc) [root@uop-test-0cf36b77-f909-4bf8-a728-eb82f379c6bf mpc-backend]# git config --global push.default matching

git status

位于分支 origin/mpc

您的分支領(lǐng)先 'remotes/origin/mpc' 共 1 個(gè)提交。

(使用 "git push" 來發(fā)布您的本地提交)

無文件要提交,干凈的工作區(qū)

git branch -d -r origin/origin/mpc
已刪除遠(yuǎn)程分支 origin/origin/mpc(曾為 edd6b7a)。
(py2.7.13mpc) [root@uop-test-0cf36b77-f909-4bf8-a728-eb82f379c6bf mpc-backend]# git status

位于分支 origin/mpc

您的分支領(lǐng)先 'remotes/origin/mpc' 共 1 個(gè)提交。

(使用 "git push" 來發(fā)布您的本地提交)

無文件要提交,干凈的工作區(qū)
(py2.7.13mpc) [root@uop-test-0cf36b77-f909-4bf8-a728-eb82f379c6bf mpc-backend]# git branch -a
master

  • origin/mpc
    remotes/origin/HEAD -> origin/master
    remotes/origin/develop
    remotes/origin/master
    remotes/origin/mpc
    remotes/origin/zyb
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Add & Commit git init 初始化一個(gè) Git 倉庫(repository),即把當(dāng)前所在目錄變成...
    冬絮閱讀 5,155評論 0 9
  • git常用命令 GIT常用命令備忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章閱讀 8,887評論 1 26
  • 在來火車站的路上問李先生今天忙不忙 他說不忙就是困 我哈哈一笑說道 給你放三天假 開心吧 他隔了半天才回了兩個(gè)字“...
    yoochn閱讀 313評論 0 0
  • 這個(gè)世界并不需要如此多的我的身影 或許是我的錯 拿自以為是的光芒以及 給予的虛榮 讓你不厭其煩 也許你想探索 也喜...
    岑家小小書僮閱讀 184評論 0 1
  • 雨水無情的打在臉上 內(nèi)心還是止不住的迷茫 面對一個(gè)路口 我是拐彎還是直走 人生太多的無奈 生活太多的悲傷 我也無助...
    辭傷悲閱讀 285評論 0 2

友情鏈接更多精彩內(nèi)容