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?
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.).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With