2

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.

0

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

1 Comment

what is cherry-pick here ? like github ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.