From your feature branch (e.g configUpdate) run:
git fetch
git rebase origin/master
Or the shorter form:
git pull --rebase
Why this works:
git merge branchnametakes new commits from the branchbranchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.git rebase branchnametakes new commits from the branchbranchname, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip ofbranchname, with any changes you made on top of that.git pullis basically the same asgit fetch; git merge origin/master.git pull --rebaseis basically the same asgit fetch; git rebase origin/master.
So why would you want to use git pull --rebase rather than git pull? Here's a simple example:
You start working on a new feature.
By the time you're ready to push your changes, several commits have been pushed by other developers.
If you
git pull(which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.If you
git pull --rebaseinstead, git will fast forward your master to upstream's, then apply your changes on top.
From your feature branch (e.g configUpdate) run:
git fetch
git rebase origin/master
Or the shorter form:
git pull --rebase
Why this works:
git merge branchnametakes new commits from the branchbranchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.git rebase branchnametakes new commits from the branchbranchname, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip ofbranchname, with any changes you made on top of that.git pullis basically the same asgit fetch; git merge origin/master.git pull --rebaseis basically the same asgit fetch; git rebase origin/master.
So why would you want to use git pull --rebase rather than git pull? Here's a simple example:
You start working on a new feature.
By the time you're ready to push your changes, several commits have been pushed by other developers.
If you
git pull(which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.If you
git pull --rebaseinstead, git will fast forward your master to upstream's, then apply your changes on top.
I found out it was:
$ git fetch upstream
$ git merge upstream/master
I'm a git rookie, so I apologize in advance if this is a stupid question.
I'm not sure if it's necessary, but I'll provide some context. I'm working on a team project. We use feature branches to implement features, which are then merged to our 'development' branch.
I created a merge request to merge my feature branch into 'development'. Then, before the feature branch was merged, I started working on a new feature branch. After working on this new feature branch I realized that I need a component from the old feature branch, which is now merged into 'development'.
How can I get the latest version of 'development' to my new feature branch? The solutions I've found online seem to suggest that I merge the branches, but that is not really an option since I don't want to touch/change the 'development' branch or anything on it.
Hopefully this makes sense.
Not sure if this is the right place to ask this. My branch is behind origin master by many commits, I tried to git merge master to my branch earlier but my git log history now shows a bunch of other people’s commits, but I actually just wanted to keep my branch up-to-date with master in one commit only, something like “Merge branch ‘master’ into ‘some-branch’” any help is appreciated. Thanks
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
You can reference those remote tracking branches ~(listed with git branch -r) with the name of their remote.
You need to fetch the remote branch:
git fetch origin aRemoteBranch
If you want to merge one of those remote branches on your local branch:
git checkout aLocalBranch
git merge origin/aRemoteBranch
Note 1: For a large repo with a long history, you will want to add the --depth=1 option when you use git fetch.
Note 2: These commands also work with other remote repos so you can setup an origin and an upstream if you are working on a fork.
Note 3: user3265569 suggests the following alias in the comments:
From
aLocalBranch, rungit combine remoteBranch
Alias:combine = !git fetch origin ${1} && git merge origin/${1}
Opposite scenario: If you want to merge one of your local branch on a remote branch (as opposed to a remote branch to a local one, as shown above), you need to create a new local branch on top of said remote branch first:
git checkout -b myBranch origin/aBranch
git merge anotherLocalBranch
The idea here, is to merge "one of your local branch" (here anotherLocalBranch) to a remote branch (origin/aBranch).
For that, you create first "myBranch" as representing that remote branch: that is the git checkout -b myBranch origin/aBranch part.
And then you can merge anotherLocalBranch to it (to myBranch).
Whenever I do a merge, I get into the branch I want to merge into (e.g. "git checkout branch-i-am-working-in") and then do the following:
git merge origin/branch-i-want-to-merge-from
You create a merge of your feature branch locally, push the branch you merged to (probably "master") to github. Github will now contain the merge. Then delete your local and remote copy of the feature branch.
Just merge the branch locally into the master branch and push that to github's master branch.