我的git 工作流

安装git

$ yum install git 

使用ssh方式下载代码

  1. 在当前主机上生成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,把公钥复制添加到 ssh kyes 中。

下载代码:
这里是下载了一个uop-backend的代码

$ git clone [email protected]:devops/uop-backend.git

查看所有分支:

$ git branch -a

代码检出:
建立一个和远程分支同名的本地分支

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

修改代码:

使用编辑器进行编辑,保存。

比较代码差异

$ git diff

查看修改状态:

$ git status
# 位于分支 origin/mpc
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#       修改:      run.py
#       修改:      uop/auth/handler.py
#

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

$ git pull --rebase

如果不加 --rebase, git 会自动进行merge。如果本地文件和远程文件有人同时修改,可能会有冲突。

配置commit模板:

建立一个文本文件,这里建立的文本文件为:.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 "[email protected]"
  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 "[email protected]"
  git config --global user.name "Your Name"

比如这里添加了我的公司账号:

$ git config --global user.email "[email protected]"
$ git config --global user.name "jiaxiaolei"

添加要提交的文件

$ git add run.py

查看修改历史:

$ git log
commit 137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
Author: jiaxiaolei 
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 
Date:   Tue Jul 25 18:53:30 2017 +0800

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

一个我自己喜欢的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

有图表,配色也比较喜欢。

查看某次修改的内容:

git show 查看某次commit的修改内容

$ git show  137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
commit 137d5cc94f5f2da9cdaadec48b3e5bdaa290aee7
Author: jiaxiaolei 
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 下的自动补全非常好用,有了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 = [email protected]
[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: 不如第一个 
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

扩展阅读:

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

v2ex上的讨论和回复。

比较经典的的配置:

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 现在位于 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
冲突(内容):合并冲突于 requirements.txt
自动合并 cmdb/settings.py
冲突(内容):合并冲突于 cmdb/settings.py

取消本地的一个修改:

$ git checkout -- utils/cobbler_api.py

查看修改记录

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

只查看某个文件的修改,比如server.py 
git log  server.py


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


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

git log -n 1 --stat


想看到最近一次提交所有更改的细节,包括修改的文件,文件中的修改细节。
git log -n 1 -p


合并到主分支:



常见问题和解决

  • 问题:

git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 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

您的分支领先 'remotes/origin/mpc' 共 1 个提交。

(使用 "git push" 来发布您的本地提交)

无文件要提交,干净的工作区

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

位于分支 origin/mpc

您的分支领先 'remotes/origin/mpc' 共 1 个提交。

(使用 "git push" 来发布您的本地提交)

无文件要提交,干净的工作区
(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

你可能感兴趣的:(我的git 工作流)