![]() Note: of course, all these changes are done locally, you should git push afterwards to apply the changes to the remote. Yet another alternative: git revert -no-commitĪs a last resort, if you need full freedom of history editing (eg, because git don't allow you to edit what you want to), you can use this very fast open source application: reposurgeon. Quick rebase: remove only a specific commit using its id: git rebase -onto commit-id^ commit-idĪlternative: you could also try: git cherry-pick commit-id ![]() Interactive rebase (this will show the log of the last 5 commits and delete the lines you don't want, or reorder, or squash multiple commits in one, or do anything else you want, this is a very versatile tool): git rebase -i HEAD~5Īnd if a mistake is made: git rebase -abort If -soft does not work, rather try -mixed or -keep. Note: Avoid git reset -hard as it will also discard all changes in files since the last commit. ![]() Harsh way, remove altogether only the last commit: git reset -soft "HEAD^" Note: if the commit to remove is a merge commit, you will need to append -m 1 (thanks to for the tip!). There are four ways of doing so (replace "commid-id" with your commit's hash):Ĭlean way, reverting but keep in log the revert: git revert -strategy resolve commit-id Mark the corrected paths with 'git add ' or 'git rm ' Here is an example of how I have been trying to revert $ git revert 342f9bbĪutomatic revert failed. Initialized empty Git repository in /Users/josh/deleteme/git_revert_test/.git/ġ files changed, 1 insertions(+), 0 deletions(-) Given the following scenario, how do I remove commit 2? $ mkdir git_revert_test & cd git_revert_test So here is a simplified version of the question: I've been looking and playing for a long time, trying to figure out how to remove the commits that contain the edits to those files, it seems to be a toss up between revert and rebase, and there are no straightforward examples, and the docs assume I know more than I do. Somehow I merged his work into mine, either when I pulled it, or when I tried to just pick the specific files out that I wanted. Secretgeek.I was working with a friend on a project, and he edited a bunch of files that shouldn't have been edited. Push to the remote - so you can create a pull-request (PR) from there git push (Optional) Look at your local outgoing commits to make sure you're about to push what you want to push. Switch to the main branch, if you're not there already: git checkout mainĬreate a new branch git checkout -b my-new-branch-nameĬherry pick the commits you like into your new branch, e.g.: git cherry-pick -x 5f8c11b8f5ca131fdc00776ac428cc9b0a90ee19 When creating a cherry pick for a hot-fix to main - you probably want to follow this slightly more involved process, so that it becomes a pull request, not just a direct commit to main. which I think is a nice and classy touch! Cherry Pick for a hot fix The -x is optional, but if included it will get the commit message of the original commit, and append this to it: (cherry picked from commit 5f8c11b8f5ca131fdc00776ac428cc9b0a90ee19) To cherry-pick that commit into the current branch: git cherry-pick -x 5f8c11b8f5ca131fdc00776ac428cc9b0a90ee19 (if you grab the merge commit's SHA - you'll end up with an error message that is pretty clear about the issue, so it's quite a recoverable error anyway) If they are commits that have been merged into another branch - then make sure to grab the original commit, not the merge commit. Many of the guides online talk about using particular tools, so i just want to write down the technique used for the commandline.įirst - find the full SHA of the commits you want to cherry-pick. ![]() * Cherry-pick some commits from one branch to another
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |