Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I synchronize two branches in the same Git repository?

Here's a common workflow hurdle I encounter often:

master is our "stable" branch

$ git status # On branch master nothing to commit (working directory clean) 

Create a module on a branch

$ git checkout -b foo $ echo "hello" > world $ git add . $ git commit -m "init commit for foo module" $ git checkout master $ git merge foo 

Do work on master or other branches

Over the next couple weeks, more code will be committed to master directly and by other branches. The foo branch will go untouched for this time period.

Resume work/make updates on the foo branch

$ git checkout foo 

Oh no! foo is massively out of date! I understand why, but I do need foo back in sync.

The question

How do I get the latest contents from the master branch?

like image 644
maček Avatar asked Oct 24 '10 23:10

maček


People also ask

How do I make two branches the same in git?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

Can git work with two branches at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.


2 Answers

If you don't need the branch around:

If you've merged foo back to master, "git branch -d foo" to kill the topic branch, and then "checkout -b foo" in the future when you need to hack on it again.

If you do need the branch around:

You can rebase your topic branch against the master branch:

git checkout foo git rebase master 

Or:

git rebase master foo 
like image 69
Adam Vandenberg Avatar answered Oct 13 '22 09:10

Adam Vandenberg


Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. The general process can be visualized as the following:

Git Rebase visual explanation

The example below combines git rebase with git merge to maintain a linear project history. This is a quick and easy way to ensure that your merges will be fast-forwarded.

# Start a new feature git checkout -b new-feature master # Edit files git commit -a -m "Start developing a feature" 

In the middle of our feature, we realize there’s a security hole in our project

# Create a hotfix branch based off of master git checkout -b hotfix master # Edit files git commit -a -m "Fix security hole" # Merge back into master git checkout master git merge hotfix git branch -d hotfix 

After merging the hotfix into master, we have a forked project history. Instead of a plain git merge, we’ll integrate the feature branch with a rebase to maintain a linear history:

git checkout new-feature git rebase master 

This moves new-feature to the tip of master, which lets us do a standard fast-forward merge from master:

git checkout master git merge new-feature 

Taken from Atlassian Git Rebase Tutorial

like image 33
Mauricio Gracia Gutierrez Avatar answered Oct 13 '22 09:10

Mauricio Gracia Gutierrez