1

I selected the master branch, made code changes, added couple of files, staged and committed everything locally. While pushing master to remote it gave me an error that pushes to master are not permitted. I understand that I was actually supposed to work with a new feature branch rather than on the master branch.

Now I have created a new feature branch off the master branch on the remote, connected to the feature branch locally and am not sure how to make the same commits on the feature branch?

One solution that I can think of is - connect to local master where the changes are made. Copy all the files from the folder and save it outside of the git folder. Then connect to the feature branch and paste the copied files, stage, commit and push to remote feature branch. Then, raise a pull request to merge feature branch into the master branch.

Another solution is to create a new feature branch off master locally. Then push that to the server and raise pull request on the server to merge into master. But then my local master and server master will be out of sync and once merge is done I will have to delete local folder and clone it again to match the local master with server master.

Is there any other elegant way than the one's mentioned above?

variable
  • 8,262
  • 9
  • 95
  • 215
  • Not sure if this answers your question directly, but if you haven't pulled the changes to server, you can stash your changes, checkout the new feature branch, then stash pop the changes and finally commit to new feature branch. [this link](https://stackoverflow.com/questions/556923/how-do-i-merge-my-local-uncommitted-changes-into-another-git-branch) has the answer, but [this one](https://stackoverflow.com/questions/2944469/how-to-commit-my-current-changes-to-a-different-branch-in-git) is quite complete, so I did not add an answer to your question. – aLuViAn Feb 01 '20 at 07:33
  • I have committed to local master, so how can I stash something that is already committed? – variable Feb 01 '20 at 07:34
  • Checkout [this](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) link. The situation is similar, and the answers are both accurate and very informative. Take your time to go through the answers and you will find yours there. – aLuViAn Feb 01 '20 at 07:37
  • if you have created new branch from current local master branch which have already commits then the new branch supposed to have all the commits of master branch to your new feature branch you just need to push that branch to remote before that you can chech for commit hash in both the branch master and feature are same or not using `git log` – Prateik Darji Feb 01 '20 at 08:43

2 Answers2

2

While your proposed way (or using a UI tool) is probably easier, you can of course do it via the command-line. Since you have already committed your changes, stashing them will not be possible, however you can simply transfer your changes to a new branch.

TL;DR

  • Get the hash of the latest commit on master that was not a local change (i.e. via git log)
  • Create a new branch from that commit (git checkout -b feature <hash>)
  • Rebase your local commits from master (git rebase master)
  • Push the new branch (git push origin feature)
  • Reset master to the old commit (git reset --hard <hash>)

Long version + explanation

First you need the hash of the latest commit you don't want in your feature branch. Assuming that is the latest commit from remote, you can simply look for origin/master in

git log

Current tree:

                 master
                 |     
 A---B---C---D---E     
 |                 
 origin/master         

Next you will have to create a branch from that commit:

git checkout -b feature <hash>

Current tree:

     feature     master
     |           |    
 A---B---C---D---E    
     |                
     origin/master

Now you have a new branch called feature at the location, where you should have created it earlier. Next you simply take all commits from master after that point and put them into feature. If you do not want all commits from master in feature, you can use the second command to cherry-pick.

git rebase master
git rebase -i

Current tree:

                  master,feature
                  |              
  A---B---C---D---E             
      |                         
      origin/master

At this point you can push the new branch to origin and raise a PR, but you still have to remove those commits from your master, or you could run into issues when pulling from origin/master later:

git checkout master
git reset --hard <hash>

Current tree:

      master      feature
      |           |      
  A---B---C---D---E      
      |                  
      origin/master

Now you can push, pull and raise a PR, as if you had made all changes on a new branch from the beginning.

cadast
  • 101
  • 1
  • 6
1

I think the most elegant way to do this is with git cherry-pick. Now how does git cherry-pick works. What this command actually does is to copy a commit from one branch and paste it to another branch. The actual command is git cherry-pick commitSha where commitSha is the hash of your commit, and you can find it using git log. So find the sha of your commit, checkout in the new branch that you want to apply the commit to and then execute the above command. Then when you are done with your new branch you can git reset the master branch to the commit before yours.

Giannis
  • 312
  • 2
  • 5
  • 16