0

In our project, we use git stash and jira. The normal rule of process is:

  1. we create a new branch from under our jira assignment (there is a hyperlink "create branch" in jira site)
  2. we commit/push the change into that newly-created branch from git console
  3. from git stash, we "create pull request" and proj lead will review and merge the change into dev branch.

My problem was that I forgot the step#1, and I committed/pushed my change to the lastly created branch, let's call it "other-ppl's-branch"

Now I've recreated a new branch, let's call it "my-new-branch", my question is, how can I remove my commit from "other-ppl's-branch" and recommit it to "my-new-branch" ?

J.E.Y
  • 1,173
  • 2
  • 15
  • 37

2 Answers2

0

My problem was that I forgot the step#1, and I committed/pushed my change to the lastly created branch, let's call it other-ppl's-branch

Read this, it will describe all the ways to fix it.
How to move HEAD back to a previous location? (Detached head)


how can I remove my commit from other-ppl's-branch and recommit it to my-new-branch ?

The simple thing for you to do it to use the git reflog to get back to the point before the mistake and continue from there

As described in the above post you can always checkout to any given commit in the past, then delete the "bad" branch and create new one from this commit which you want it to be.


How to add the commit to the right branch?

Few options:

git cherry-pick

Now in order to do so you can use the git cherry-pick and choose the commit range you

git format-patch & git apply

Use the git format-patch to generate the patches which were made the desired commits and then add them apply them to the desired branch.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • I still don't get how to "delete" a commit if there are other commits on top of that. I know I can git 'reset' to a previous point and rewrite/delete the bad commit I made, but then it would rewrite others' commit as well, right? – Max Mar 15 '16 at 20:40
  • Read the post: How to move HEAD (checkout, revert, reflog, reset) [old commit] and do the checkout part, its exactly what you need to do – CodeWizard Mar 15 '16 at 20:42
0

What I can think of to delete a commit from origin is:

Method 1

git revert <commit>, create a new commit to revert the unwanted commit.

Method 2

If you are sitting on top of the last unwanted commit. And you are sure others will not commit during your change.

git reset --hard <commit>

git push --force

Delete commits from a branch in Git

Community
  • 1
  • 1
Max
  • 129
  • 8