Reputation: 107
I have cloned a repository from master branch and did lots of modifications. Suddenly I remembered that I'm using the master brach and I wanted to commit it to newly created remote branch not the master branch.Actually I'm very new to git. please help me,Thank you
Upvotes: 0
Views: 90
Reputation: 1903
If you made changes to wrong branch then do git checkout -b newBranchName
.
This will move all changes to new branch named newBranchName
Then do git branch
to see which branch are you currently working on.
If this is the newBranchName
then do
git add . //stages all changed files
git commit -m "any message here" //commit with a message
git push -u origin newBranchName //push local branch to remote with name newBranchName
Doing this will push your local branch to a new remote branch named newBranchName
Now doing git checkout master
will again take you back to you local master branch.
Cross check which branch are you currently at by git branch
If this shows master with green color or *master
then do
git reset --hard HEAD
to revert back the changes you have made in your local.
Once you reset it then your local master branch will be exactly same as remote master branch.
Upvotes: 4
Reputation: 521103
This is a common workflow mistake. One simple option is to create a new branch from your current point in master
, then revert your local master
branch back to the point right before you started working:
# from master
# git commit any outstanding changes
git branch feature
git reset --hard HEAD~2 # replace 2 with the actual number of commits you did make
This assumes that you made two commits to your local master
branch before you realized you were on the wrong branch. The hard reset command simply removes those commits, which however are now still part of the feature
branch. You may now push feature
to the remote if you want to do that.
Upvotes: 2