Jump to content

Git merge


sKunKbad

Recommended Posts

I use git every day, but don't do much with branches because I work alone. I'm curious about something related to the branches, when a new branch is on a remote, and then it needs to be fetched and merged separately from the master branch.

 

So pretend I'm on the master branch on my machine:

git checkout -b develop
# I make changes to files on develop
git add .
git commit -am "I have made changes to develop"
git push origin develop

Now there is a develop branch on the remote. Somebody else now fetches and merges (they are on master):

git fetch origin
# git shows that there is a new branch develop, and merge is also required for origin/master

Normally without the develop branch, I would run this command from master:

git merge origin/master

Do I also need to checkout develop and merge origin/develop?

# Do I need to do this too?
git checkout develop
git merge origin/develop

Is there a way to merge all branches with their remote counterparts, all in one command?

 

Link to comment
Share on other sites

Normally you work on one branch at a time. If you want your master to be up-to-date then you would do a fetch/merge (or a pull which is the same thing). Actually, for something like master, you should never be committing to it anything besides merges from other branches so I do a fast-forward merge.

Normally you would then, later, decide you want to work on the develop branch. You check it out, do a merge, resolve conflicts, and begin working.

 

No, you cannot merge into multiple branches at once with a single command. The main reason would be the difficulty with conflict resolution.

 

What you can do is script it yourself: fetch, for each branch { checkout, merge, do something in case of conflicts (eg, pause and wait for the user to resolve), and commit }

Link to comment
Share on other sites

Normally you work on one branch at a time. If you want your master to be up-to-date then you would do a fetch/merge (or a pull which is the same thing). Actually, for something like master, you should never be committing to it anything besides merges from other branches so I do a fast-forward merge.

Normally you would then, later, decide you want to work on the develop branch. You check it out, do a merge, resolve conflicts, and begin working.

 

No, you cannot merge into multiple branches at once with a single command. The main reason would be the difficulty with conflict resolution.

 

What you can do is script it yourself: fetch, for each branch { checkout, merge, do something in case of conflicts (eg, pause and wait for the user to resolve), and commit }

 

Interesting. I thought it would be normal to have a bunch of different branches, so that different things could be worked on separately from other things. For my own purposes,  a single branch is fine.

 

One thing that seems strange to me is that I am noticing that commits on develop that are not merged with master seem to be automatically merged with the local develop branch when I fetch/merge the master branch. For instance, right now develop is ahead of master by one commit, and when I fetched/merged local master with origin master, local develop was magically merged. So, it would seem that fetch/merge on master automatically brings all of the other branches up to date.

Link to comment
Share on other sites

Interesting. I thought it would be normal to have a bunch of different branches, so that different things could be worked on separately from other things. For my own purposes,  a single branch is fine.

No no, you can have multiple branches. You're supposed to. Mind you, if it's just you working then there's a good chance you'll just work on one branch until you're done, merge, and then start a new branch. Or maybe you have one, decide you want to do something else for a while, and start another one. But multiple branches is definitely a thing.

 

I meant you can only have one checked out at a time. In the working directory. If you want to work on a second branch then you either (a) commit/stash your current stuff and switch branches or (b) clone a whole new second repository and do the second branch on that. The former is the normal way of doing it, and while the latter isn't necessarily wrong it can make things confusing (like "which clone was I doing $branch with?").

 

One thing that seems strange to me is that I am noticing that commits on develop that are not merged with master seem to be automatically merged with the local develop branch when I fetch/merge the master branch. For instance, right now develop is ahead of master by one commit, and when I fetched/merged local master with origin master, local develop was magically merged. So, it would seem that fetch/merge on master automatically brings all of the other branches up to date.

...No? I don't know what you're seeing but when you do a merge/pull you're only updating the branch you have checked out. Fetch can update all remote branches but any local branch remains where it was.
Link to comment
Share on other sites

...No? I don't know what you're seeing but when you do a merge/pull you're only updating the branch you have checked out. Fetch can update all remote branches but any local branch remains where it was.

 

Well, maybe our versions are different or something. Fetch followed by merge seems to be updating my local develop, even when I'm on the master branch. My git version is 2.7.0, and I'm on Ubuntu 14.04.

Link to comment
Share on other sites

Okay, just did a test.

 

0. Made a repo on GitHub

 

First directory:

1. Cloned repo, on "master"

2. Added and committed file #1

3. Branched to "branch"

4. Added and committed file #2

5. Pushed both branches, still on "branch"

 

master and origin/master have file #1, branch and origin/branch have files #1 #2.

 

Second directory,

6. Cloned repo, on "master"

7. Added and committed file #3

8. Pushed master

 

Second directory's master and origin/master have files #1 #3, branch and origin/branch have files #1 #2.

 

First directory,

9. Fetched (updated origin/master, local master still has #1)

10. Merged origin/master into master (got file #3)

 

The branch is still where it was: files #1 and #2.

Link to comment
Share on other sites

  • 2 weeks later...

I've been using branches a lot more since I created this thread, and I think this scenario is explained as follows:

 

When a local repo is fetching from origin, if origin has a repo that local has not yet had, when checking out that new branch for the first time, the branch is automatically current with origin/"branch".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.