How to fix Git branch submitted wrong?

I just made the wrong branch did a very good submission. How do I undo my main branch of the last commit, then make the same changes and upgrade them into my branch?


#1st Floor

This topic four years late, but it may be helpful to someone.

If you forget to create before submitting a new branch and submit all, regardless of how many times you perform a commit on master, the following method will be easier:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

Now, your main branch equal origin/master, all are newly submitted my_feature. Please note that my_featurethe local branch, rather than the remote branch.


#2nd Floor

If you have a clean (unmodified) working copy

To roll back a submission (to ensure hash submitted at the next step in mind):

git reset --hard HEAD^

The pull submit another branch:

git checkout other-branch
git cherry-pick COMMIT-HASH

If you have modified or track changes

Also note that git reset --hardwill kill you might have any untracked changes and modifications , so if you have these changes , you may prefer:

git reset HEAD^
git checkout .

#3rd floor

If you have not pushed to change, you can also perform a soft reset:

git reset --soft HEAD^

This will restore submitted, but the changes will be submitted back into the index. Suppose your date is with respect to each other, git will allow you to enter another branch, and then you can simply submit:

git checkout branch
git commit

The disadvantage is that you need to re-enter commit message.


#4th floor

If you have contributed to change, you need a push in force after reset HEAD.

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

Warning: A hard reset will undo any uncommitted changes in the working copy, and forced reset will have complete coverage of the state branch of the current state of the local branch of the remote.

Just in case, on Windows (using the Windows command line, rather than Bash) it is actually four ^^^^instead of one, so it is

git reset --hard HEAD^^^^

#5th Floor

So, if your plan is that you have promised masterbut meant that the author another-branch(or may not exist may not yet exist), but you have not pushed, it is very easy to fix.

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

Now you want masterall submissions will be in another-branch.

From love from: HTTP : //haacked.com/archive/2015/06/29/git-migrate/


#6th floor

If you want to apply changes to the branch already exists (eg, branch development ), please follow the fotanus description provided by the operation, and then:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

Obviously, if you prefer, you can use tempbranch or any other branch name instead my_feature .

In addition, if applicable, the delay storage pop-up (application), until you merge the target branch so far.


#7th floor

If you encounter this problem but you have Visual Studio, you can do the following:

Right-click your branch, and then select View History:

The input image description

Right-click to submit the return. And in accordance with the need to restore or reset.

The input image description


Building # 8

I recently did the same thing, I accidentally changed the owner, when I should be committed to other branches. But I did not push anything.

If you just submitted the wrong branch, and then do not change anything, and there is no push to repo, then you can do the following:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

Note: In the above example, I use git reset HEAD ~ 1 rewritten once submitted. But if you want to fall back n commits, then you can do git reset HEAD ~ n.

Also, if you ultimately submit to the wrong branch, and eventually wrote more code before realizing that you have submitted to the wrong branch, you can use git stash to save work in progress:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

Note: I use this site as a reference https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/


House # 9

To detail this answer, if you have multiple submissions to be transferred, for example developto new_branch:

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started
Original articles published 0 · won praise 0 · Views 2235

Guess you like

Origin blog.csdn.net/p15097962069/article/details/103890259