푸시되지 않은 git 커밋을 어떻게 삭제합니까?
우연히 잘못된 지점에 맡겼습니다. 해당 커밋을 어떻게 삭제합니까?
수행 한 작업을 유지하면서 가장 최근 커밋을 삭제합니다.
git reset --soft HEAD~1
가장 최근 커밋을 삭제하고 수행 한 작업 을 삭제합니다 .
git reset --hard HEAD~1
그것을 삭제하지 마십시오. 단 하나의 커밋 git cherry-pick
으로 충분합니다.
그러나 잘못된 브랜치에 여러 커밋 이 있다면 그게 git rebase --onto
빛납니다.
다음이 있다고 가정합니다.
x--x--x--x <-- master
\
-y--y--m--m <- y branch, with commits which should have been on master
, 그런 다음 master
원하는 위치로 표시 하고 이동할 수 있습니다.
git checkout master
git branch tmp
git checkout y
git branch -f master
x--x--x--x <-- tmp
\
-y--y--m--m <- y branch, master branch
, 있어야하는 곳에 y 분기를 재설정합니다.
git checkout y
git reset --hard HEAD~2 # ~1 in your case,
# or ~n, n = number of commits to cancel
x--x--x--x <-- tmp
\
-y--y--m--m <- master branch
^
|
-- y branch
, 마지막으로 커밋을 이동하십시오 (다시 적용하여 실제로 새로운 커밋을 만드십시오)
git rebase --onto tmp y master
git branch -D tmp
x--x--x--x--m'--m' <-- master
\
-y--y <- y branch
내가 찾은 최고의 답변이 댓글에만있는 이유가 궁금합니다! ( 86 찬성표로 Daenyth에 의해 )
git reset --hard origin
This command will sync the local repository with the remote repository getting rid of every change you have made on your local.
Do a git rebase -i FAR_ENOUGH_BACK
and drop the line for the commit you don't want.
If you want to move that commit to another branch, get the SHA of the commit in question
git rev-parse HEAD
Then switch the current branch
git checkout other-branch
And cherry-pick
the commit to other-branch
git cherry-pick <sha-of-the-commit>
For your reference, I believe you can "hard cut" commits out of your current branch not only with git reset --hard, but also with the following command:
git checkout -B <branch-name> <SHA>
In fact, if you don't care about checking out, you can set the branch to whatever you want with:
git branch -f <branch-name> <SHA>
This would be a programmatic way to remove commits from a branch, for instance, in order to copy new commits to it (using rebase).
Suppose you have a branch that is disconnected from master because you have taken sources from some other location and dumped it into the branch.
You now have a branch in which you have applied changes, let's call it "topic".
You will now create a duplicate of your topic branch and then rebase it onto the source code dump that is sitting in branch "dump":
git branch topic_duplicate topic
git rebase --onto dump master topic_duplicate
Now your changes are reapplied in branch topic_duplicate based on the starting point of "dump" but only the commits that have happened since "master". So your changes since master are now reapplied on top of "dump" but the result ends up in "topic_duplicate".
You could then replace "dump" with "topic_duplicate" by doing:
git branch -f dump topic_duplicate
git branch -D topic_duplicate
Or with
git branch -M topic_duplicate dump
Or just by discarding the dump
git branch -D dump
Perhaps you could also just cherry-pick after clearing the current "topic_duplicate".
What I am trying to say is that if you want to update the current "duplicate" branch based off of a different ancestor you must first delete the previously "cherrypicked" commits by doing a git reset --hard <last-commit-to-retain>
or git branch -f topic_duplicate <last-commit-to-retain>
and then copying the other commits over (from the main topic branch) by either rebasing or cherry-picking.
Rebasing only works on a branch that already has the commits, so you need to duplicate your topic branch each time you want to do that.
Cherrypicking is much easier:
git cherry-pick master..topic
So the entire sequence will come down to:
git reset --hard <latest-commit-to-keep>
git cherry-pick master..topic
When your topic-duplicate branch has been checked out. That would remove previously-cherry-picked commits from the current duplicate, and just re-apply all of the changes happening in "topic" on top of your current "dump" (different ancestor). It seems a reasonably convenient way to base your development on the "real" upstream master while using a different "downstream" master to check whether your local changes also still apply to that. Alternatively you could just generate a diff and then apply it outside of any Git source tree. But in this way you can keep an up-to-date modified (patched) version that is based on your distribution's version while your actual development is against the real upstream master.
So just to demonstrate:
- reset will make your branch point to a different commit (--hard also checks out the previous commit, --soft keeps added files in the index (that would be committed if you commit again) and the default (--mixed) will not check out the previous commit (wiping your local changes) but it will clear the index (nothing has been added for commit yet)
- you can just force a branch to point to a different commit
- you can do so while immediately checking out that commit as well
- rebasing works on commits present in your current branch
- cherry-picking means to copy over from a different branch
Hope this helps someone. I was meaning to rewrite this, but I cannot manage now. Regards.
참고URL : https://stackoverflow.com/questions/3197413/how-do-i-delete-unpushed-git-commits
'Programing' 카테고리의 다른 글
forEach 루프와 함께 async / await 사용 (0) | 2020.09.29 |
---|---|
CommonJS, AMD 및 RequireJS 간의 관계? (0) | 2020.09.29 |
Python에서 싱글 톤 만들기 (0) | 2020.09.29 |
Entity Framework와 LINQ to SQL (0) | 2020.09.29 |
“POSIX”의 의미는 무엇입니까? (0) | 2020.09.29 |