Dinesh Kumar
Dinesh Kumar

Reputation: 1293

How to undo the wrong commit and update it back in git

I have been using git commands for my project.

  1. If I made one wrong commit and pushed into my branch. On that time, how can I update it back ?

  2. If I need to copy last three commits to new branch, how can I do?

Need programmers help soon... Thanks in advance.

Upvotes: 2

Views: 3749

Answers (2)

JB.
JB.

Reputation: 42094

To revert your faulty commit, simply use git revert and push that.

It's not going to remove it from the project history, though. For that you'd need to locally rewrite said history, and push that; but many upstream repositories prevent that since it messes up everyone else-s workflow.

To copy the three last commits from a branch feature to your current branch, use the following:

git cherry-pick feature~3..feature

Upvotes: 2

ton
ton

Reputation: 1624

A1. You should reset and push -f.

Now situation.

A - B - C - D ← HEAD and wrong commit

You should rollback HEAD.

git reset --hard HEAD~1

now

A - B - C ← HEAD

finally, you should force push to your remote repository.

git push origin your_branch -f

A2. use cherry-pick

Now situation.

        need to copy
       |---------|
A - B - C - D - E← old branch
 \
  F - G - H ← new branch

use cherry-pick on your new branch

git cherry-pick <C commit hash>
git cherry-pick <D commit hash>
git cherry-pick <E commit hash>

now

A - B - C - D - E← old branch
 \
  F - G - H - C' - D' - E'← new branch

Upvotes: 2

Related Questions