push


git: How to move locally committed (but not pushed) changes to a new branch 2

Recently, we’ve been working on a certain branch, we did some changes and performed a couple of commits that were not pushed on the remote system.

There was a complication and it was decided that the local changes should not be pushed to the branch that we were working on.
Rather, they changes should go to a new branch which eventually will be merged.

As mentioned above, we already had done some changes and we already had performed the commits.

git status would give us the following:

$ git status;
On branch scanner_pdu_parser_master
Your branch is ahead of 'origin/scanner_pdu_parser_master' by 2 commits.
  (use "git push" to publish your local commits)

So, we needed to change the branch for those local commits.

Solution – Move the local commits to a new branch

First we got the name of the current branch using the command:

git branch;

Then, we switched to a new local branch

git checkout -b banana_peeler;

And, we pushed the local branch to the remote system:

git push --set-upstream origin banana_peeler;

Afterwards, we switched back to the previous branch

git checkout apple_peeler;

And reset it back to its original form, removing our local commits from it:

git reset --hard origin/apple_peeler;

Please note that the last command will delete all changes that are not committed as well.
In other words, any file you modified and did not commit or push, they will be reverted back to the original code as well.


Git: Create a branch on your local machine, switch to it and then push it on the server

Following are a couple of simple commands that we use daily to create a new branch on git and push it to the git server.

The following command will create locally a new branch called branch_name and switch to it.

git checkout -b branch_name;

The following command will push the local branch you are currently switched to on the git server.
It will be made available to the server using the name branch_name.

git push --set-upstream origin branch_name;