8

I just want to clarify how committing on specific branches works.

Let's say I am working on a branch called "Metro". I make some changes to a few files, but I am not ready to push these up to the remote repository.

A hotfix comes in that I need to fix asap. I need to switch over to a clean branch called "Master", but I cannot because I would overwrite the files I have changed. I need to commit these before I can switch.

My question is, if I commit these changes on the "Metro" branch, then switch to the clean "Master" branch, will the changes made in "Metro" get pushed to the remote "Master" repo because I have committed them, even though I am pushing to another branch?

To make it succinct, are commits isolated to branches, or do all commits get added when pushing to the remote repo?

3 Answers 3

3

Before you switch branches to master via git checkout master, you must first git commit -m 'some message', otherwise, git will try to take your current branch's changes along with you to the master branch.

will the changes made in "Metro" get pushed to the remote "Master" repo because I have committed them, even though I am pushing to another branch?

Absolutely not. git will only merge committed changes from Metro to master if you tell it to do that. That's by design (Note: please read about git remotes, because using remotes, you can actually set up your pushes to do that by "default", which would be a case where you need to be careful.).

Sign up to request clarification or add additional context in comments.

Comments

2

To make it succinct, are commits isolated to branches, or do all commits get added when pushing to the remote repo?

To make it succinct: yes to the first part :-).

Technically speaking, a branch in git is a chain of commits, along with a name. If you push a branch, you will push all the commits that belong to it (unless the remote repo already has them), but nothing else. So no, git will not somehow push all commits.

Note however that you must take care only to push the branch that you want to push - git push can be configured to push all your branches. To avoid this, set the config var push.default:

git config push.default current

This makes sure that git push will only push the current branch (and not all branches, which used to be the default).

Comments

1

To understand how branches work in Git, I think it’s important to change ones mindset to always have the commit tree in mind. You have a acyclic tree of commits where each commit has a number of parent commits and nothing else. Branches are now just simple pointers to specific commits within that tree. If you make a new commit to a branch, all that really happens is that you create a new commit object with the changes that has the previous version as its parent and your branch pointer moves to the new commit that is now added to the whole tree.

So, branches are completely independent from each others, and in the same way, they don’t really mean much. They are just pointers.

In your case, when you switch from metro to master, you are (probably) operating on a different side of the tree. All commits you now create will only move the current branch pointer—master—but will not affect any other branches.

And if you push a branch, all that happens is that you tell the remote the commit the branch points at, and then you give the remote all the objects it needs to complete the tree.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.