git 日常使用

从feature/20170926_1326586_lseii_1分支中拿一个文件到当前的分支

git checkout feature/20170926_1326586_lseii_1 -- onstar_lseii_adapter/settings.py

 

一,合并代码

1.1 git merge vs git merge --no-ff

1.1.1 一个例子

  1. 在 master上 init
  2. 在 dev 分支上 add b,再 add c。
  3. 在 master 上合并 dev 进 master

1.1.2 git merge

在能快速合并时,直接移动 当前分支HEAD指针,会导致删除分支后,分支信息丢失(就像没有过这个分支一样)
d661c384a9c97b44.png

1.1.3 git merge --no-ff

会创建一次新的commit 记录合并情况,当分支删除后,分支信息不会丢失。

 

1.1.4 git merge --squash

压缩分支提交历史

git checkout master
git merge --squash bugfix
git commit

此操作会把 bugfix 分支所有提交当成一次提交合并进 master 分支中。
git commit -m 'Your custom commit message' 如果想保留旧的提交信息作为参考,可以直接用 git commit(效果如下)

 

2. git merge vs git rebase

2.1 例子

mkdir understand_git
cd !$

git init

touch a
git add a && git commit -m "add a"

git checkout -b dev
touch b c
git add b && git commit -m "add b"
git add c && git commit -m "add c"

git checkout master
touch x y
git add x && git commit -m "add x"
git add y && git commit -m "add y"

2.2 git merge

Merge made by the 'recursive' strategy.
b | 0
c | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b
create mode 100644 c
3ec1b7018854a389.png

2.3.1 git merge --no-ff

Merge made by the 'recursive' strategy.
b | 0
c | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b
create mode 100644 c

8752e8391a094878.png

2.3 git rebase

5baf357111163041.png

Short Version
Merge takes all the changes in one branch and merges them into another branch in one commit.
Rebase says I want the point at which I branched to move to a new starting point

The only differences between a rebase and a merge are:
The resulting tree structure of the history (generally only noticeable when looking at a commit graph) is different (one will have branches, the other won't).
Merge will generally create an extra commit (e.g. node in the tree).
Merge and rebase will handle conflicts differently.
Rebase will present conflicts one commit at a time where merge will present them all at once.

https://stackoverflow.com/a/9147389/2714931

git cherry pick

类似于 merge 和 rebasecherry pick 可以将某一次commit合并过来。

git cherry-pick -x <commit-hash>

http://stackoverflow.com/a/9339460/2714931

合并分支时,忽略换行

全局配置:git config merge.renormalize true 旧项目不推荐使用!
单行命令:合并其它分支或commit,并且忽略换行

git merge --no-edit -s recursive -Xignore-space-at-eol --no-ff commit-id/branch-name

增加一个快捷方式

alias gm="git merge --no-edit -s recursive -Xignore-space-at-eol --no-ff"

强制退出 merge 状态

git fetch origin
git reset --hard origin

比如你查看了某个冲突,但是发现是其它人的,你不确定怎么改,让他处理了冲突。但是这时候你的代码还是在 merge 状态下,可以使用上面的办法强制退出 merge 状态。

从指定分支 xxx 中 checkout 出某个文件

git checkout [commit-ref] -- [filename]

git fetch
git checkout xxx -- path/to/file

这个命令可以用在解决冲突时,比如一个 swagger config 文件,你确定某个分支的文件是最新的,直接拿出来冲突就解决了。

修改上次提交信息

git commit --amend -m 'new commit info'

posted @ 2017-12-20 11:47  RockyLee  阅读(234)  评论(0编辑  收藏  举报