Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to commit and push changes using Git

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?

like image 250
Carey Estes Avatar asked Feb 27 '13 17:02

Carey Estes


2 Answers

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.).

like image 54
Kristian Avatar answered Sep 27 '22 21:09

Kristian


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).

like image 31
sleske Avatar answered Sep 27 '22 22:09

sleske