I need advice on how to merge master into a feature branch.
I began working on a new feature last year, and then was pulled away to other things. Master had substantially changed, so I wanted to merge it back into the feature branch, because I can't keep working on the feature until those changes are incorporated into that branch.
I tried this in Github, but I could only merge into master, even when I selected merge into the feature branch. I finally just merged into master and manually updated the changes on the feature branch (which had been overwritten) through cut-and-paste on the feature branch. But this seems like a common task, and I don't want to have to do that again if I can help it.
Thanks in advance for any help; I did consult the Git book, but I couldn't find anything specific about this task.
How I would do this
git checkout master
git pull origin master
git merge test
git push origin master
If I have a local branch from a remote one, I don't feel comfortable with merging other branches than this one with the remote. Also I would not push my changes, until I'm happy with what I want to push and also I wouldn't push things at all, that are only for me and my local repository. In your description it seems, that test is only for you? So no reason to publish it.
git always tries to respect yours and others changes, and so will --rebase. I don't think I can explain it appropriately, so have a look at the Git book - Rebasing or git-ready: Intro into rebasing for a little description. It's a quite cool feature
This is a very practical question, but all the answers above are not practical.
Like
git checkout master
git pull origin master
git merge test
git push origin master
This approach has two issues:
It's unsafe, because we don't know if there are any conflicts between test branch and master branch.
It would "squeeze" all test commits into one merge commit on master; that is to say on master branch, we can't see the all change logs of test branch.
So, when we suspect there would some conflicts, we can have following git operations:
git checkout test
git pull
git checkout master
git pull
git merge --no-ff --no-commit test
Test merge before commit, avoid a fast-forward commit by --no-ff,
If conflict is encountered, we can run git status to check details about the conflicts and try to solve
git status
Once we solve the conflicts, or if there is no conflict, we commit and push them
git commit -m 'merge test branch'
git push
But this way will lose the changes history logged in test branch, and it would make master branch to be hard for other developers to understand the history of the project.
So the best method is we have to use rebase instead of merge (suppose, when in this time, we have solved the branch conflicts).
Following is one simple sample, for advanced operations, please refer to http://git-scm.com/book/en/v2/Git-Branching-Rebasing
git checkout master
git pull
git checkout test
git pull
git rebase -i master
git checkout master
git merge test
Yep, when you have uppers done, all the Test branch's commits will be moved onto the head of Master branch. The major benefit of rebasing is that you get a linear and much cleaner project history.
The only thing you need to avoid is: never use rebase on public branch, like master branch.
Never do operations like the following:
git checkout master
git rebase -i test
Details for https://www.atlassian.com/git/tutorials/merging-vs-rebasing/the-golden-rule-of-rebasing
appendix:
- if you are not sure about rebasing operations, please refer to: https://git-scm.com/book/en/v2/Git-Branching-Rebasing
I merged master branch into my feature-branch and resolved conflicts.
git checkout master
git pull
git checkout feature-branch
git merge master
This didn't disturb any of the comment history in existing sub-branches of my feature-branch.
I did rebase the sub-branches on top of the feature-branch(with latest changes) and all looks fine.
Let's try to answer this with a couple of general illustrations, since we don't know exactly how your particular case look like. First of all, let's agreeing on how merge and rebase differs, before looking at the case your are asking for, where multiple feature branches based on each other.

As you are probably aware merge preserves history as it happened, while rebase rewrites it. The difference can be seen in above illustration.
Now, let's try to answer your initial question with a similar illustration; where two feature branches (feature-1 & feature-2) are based on each other, and currently trails behind master.

Regardless of how you decide to integrate the changes from master into feature-1 (merge or rebase), feature-2 will be left as is (i.e. without the newly integrated changes from master into feature-1). If you then want to integrate all changes into feature-2 you are once again left with the option of merging or rebasing.
If you would decide to rebase feature-2 onto feature-1 (post the initial master integration) Git would then figure out that commits ol42g and 09qr2 are already present, and hence automatically strip those patches out from your rebased version of feature-2.
A cautionary warning: Rebasing branches that have already been published can cause headaches for your team mates, so make sure to keep a tight dialog if that would be the case. To stay on the safe side, don't rebase branches that are already publicly available.
Hopefully it should be clear now what your options are. =)
In advance, I'm new to Git and GitHub and I'm sorry if this is a silly question AND if this is the wrong place to post this.
My boss said, "don't forget to merge master back into the feature branch before making new PRs so I don't have to go through all the changes."
So, the following is my flow:
- Create a fork of the company Repo (only once, and obviously not every time)
- Checkout a branch
- Git add .
- Git commit
- Git push origin <branch name>
- Go into GitHub and make a PR and assign it to my boss
My assumption was that the boss wanted to merge with the master himself. However, is he asking me to do it?
You’re being asked to make sure that your feature branch is up to date with master. New changes may have been made since you created the branch, and you might run into a nasty merge if you don’t integrate them into your branch beforehand.
You can do something like git pull origin master while checked out on your branch to do this.
If what kind of company would you just not ask your boss for clarification
Hi folks,
I'd like to hear you about something that bothers me every time I see.
Often I see someone merging the master branch into a feature branch in order to update it with the last changes.
My understanding is the most appropriated would be to *rebase* the feature branch, so the branch history don't get dirty with a lot of meaningless merge commits.
Moreover, any rebase operation on the feature branch (maybe for change a commit message or for squash commits) becomes a nightmare when these merge commits are on the tip of the feature branch.
Does anybody has a good tech reason for not merging master into feature branches or am I just being annoying?
How do we merge the master branch into the feature branch? Easy:
git checkout feature1
git merge master
There is no point in forcing a fast forward merge here, as it cannot be done. You committed both into the feature branch and the master branch. Fast forward is impossible now.
Have a look at GitFlow. It is a branching model for git that can be followed, and you unconsciously already did. It also is an extension to Git which adds some commands for the new workflow steps that do things automatically which you would otherwise need to do manually.
So what did you do right in your workflow? You have two branches to work with, your feature1 branch is basically the "develop" branch in the GitFlow model.
You created a hotfix branch from master and merged it back. And now you are stuck.
The GitFlow model asks you to merge the hotfix also to the development branch, which is "feature1" in your case.
So the real answer would be:
git checkout feature1
git merge --no-ff hotfix1
This adds all the changes that were made inside the hotfix to the feature branch, but only those changes. They might conflict with other development changes in the branch, but they will not conflict with the master branch should you merge the feature branch back to master eventually.
Be very careful with rebasing. Only rebase if the changes you did stayed local to your repository, e.g. you did not push any branches to some other repository. Rebasing is a great tool for you to arrange your local commits into a useful order before pushing it out into the world, but rebasing afterwards will mess up things for the git beginners like you.
You should be able to rebase your branch on master:
git checkout feature1
git rebase master
Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), Git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with
git rebase --skip
If you perform a git log on your feature branch, you'll see the bugfix commit appear only once, and in the master portion.
For a more detailed discussion, take a look at the Git book documentation on git rebase (https://git-scm.com/docs/git-rebase) which cover this exact use case.
================ Edit for additional context ====================
This answer was provided specifically for the question asked by @theomega, taking his particular situation into account. Note this part:
I want to prevent [...] commits on my feature branch which have no relation to the feature implementation.
Rebasing his private branch on master is exactly what will yield that result. In contrast, merging master into his branch would precisely do what he specifically does not want to happen: adding a commit that is not related to the feature implementation he is working on via his branch.
To address the users that read the question title, skip over the actual content and context of the question, and then only read the top answer blindly assuming it will always apply to their (different) use case, allow me to elaborate:
- only rebase private branches (i.e. that only exist in your local repository and haven't been shared with others). Rebasing shared branches would "break" the copies other people may have.
- if you want to integrate changes from a branch (whether it's master or another branch) into a branch that is public (e.g. you've pushed the branch to open a pull request, but there are now conflicts with master, and you need to update your branch to resolve those conflicts) you'll need to merge them in (e.g. with
git merge masteras in @Sven's answer). - you can also merge branches into your local private branches if that's your preference, but be aware that it will result in "foreign" commits in your branch.
Finally, if you're unhappy with the fact that this answer is not the best fit for your situation even though it was for @theomega, adding a comment below won't be particularly helpful: I don't control which answer is selected, only @theomega does.
edit: this is talking about making a PR if that isn't clear my bad
Assuming history doesn't matter/squash.
I find it much easier to:
-
clean up my dev branch/commit
-
checkout master
-
pull down latest master
-
checkout my dev branch again
-
do
$git merge master
I do this because I've found the rebase method I had to go through each commit... possibly deal with conflicts more than once(per commit).
I have a feature branch checked out on which I have made many changes that I havent pushed to master yet. Out repo was on gitlab, but it has locked out because we exceeded max user limits. So we cannot push to this repo anymore, though we can pull. Now the team has decided to move to github and it might take some time to move the repo to github.
My doubt is can I merge master branch to feature branch locally and then commit it to github once the migration from gitlab to github completes? If yes, can you please give some overview of the process or at least link of some webpage discussing the same?