Best Way To Merge A Git Branch Into Master

Question:

There are two branches, master and develop. What is the best and safest way merge develop branch into master branch?

Answer:

Here is the solution. After you commit everything in develop branch.

Do as following steps:

1
2
3
4
git checkout master
git pull origin master
git merge develop
git push origin master

As other answers mentioned, this solution is only acceptable for a small project, like only few branches, and other people won’t make changes in master branch. From my personal perspective, if there are some conflicts between master and develop branch, this might not be a good way to solve this problem.

Reference

Best (and safest) way to merge a git branch into master


This is the end of post

如何用最好的方式将一个分支合并到 Master 上

问题:

在一个 git 上有两个分支, masterdevelop. 如何用最好而且最安全的方法,把 develop 分支合并到 master 分支上?

答案:

这是一个相当简单的解决方法,当你在 develop 分支上提交了所有的修改后。

跟着这几步做:

1
2
3
4
git checkout master
git pull origin master
git merge develop
git push origin master

正如其他答案所提及的,这个解决方法只适合一些小的项目,比如就只有少数几个分支,别人不会在 master 做改变之类的。

从我个人的观点来说,如果在 masterdevelop 分支上有一个冲突,这并不是一个很好的解决方案,我们需要寻求更好的方法来解决不同分支上的冲突。

参考

Best (and safest) way to merge a git branch into master


本文完

Git操作手札

Git 是什么?

请参考 Git 在维基百科上的定义。

Git 命令

创建与修改

复制一个已创建的仓库:

1
$ git clone ssh://user@domain.com/repo.git

创建一个新的本地仓库:

1
$ git init

本地修改

显示路径下已修改的文件:

1
$ git status

显示与上次提交版本文件的不同:

1
$ git diff

把当前所有修改添加到下次提交中:

1
$ git add

把对某个文件的修改添加到下次提交中:

1
$ git add -p <file>

提交本地的所有修改:

1
$ git commit -a

提交之前已标记的变化:

1
$ git commit

附加消息提交:

1
$ git commit -m 'message here'

搜索

从当前目录的所有文件中查找 “Hello” 内容:

1
$ git grep "Hello"

历史记录

显示所有的提交记录:

1
$ git log

显示某个用户的所有提交记录:

1
$ git log --author="username"

显示某个文件的所有修改记录:

1
$ git log -p <file>

分支

列出所有的分支:

1
$ git branch

切换分支:

1
$ git checkout <branch>

创建并切换到新分支:

1
$ git checkout -b <branch>

基于当前分支创建新分支:

1
$ git branch <new-branch>

删除本地分支:

1
$ git branch -d <branch>

合并与重置

将分支合并到当前HEAD中:

1
$ git merge <branch>

使用配置好的merge tool 解决冲突:

1
$ git mergetool

撤销

放弃工作目录下的所有修改:

1
$ git reset --hard HEAD

放弃某个文件的所有本地修改:

1
$ git checkout HEAD <file>

重置一个提交

1
$ git revert <commit>

将HEAD重置到指定的版本,并抛弃该版本之后的所有修改:

1
$ git reset --hard <commit>

将HEAD重置到上一次提交的版本,并将之后的修改标记为未添加到缓存区的修改:

1
$ git reset <commit>

将HEAD重置到上一次提交的版本,并保留未提交的本地修改:

1
$ git reset --keep <commit>

参考

Git操作手册|命令速查表


本文完