Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting to a specific commit without losing history

I know this type of a question has a lot duplicates, but I wanted to open a new one because I didn't found in all of the other questions the explaination of the best way to do it as I want.

I know i can revert and keep the history by doing:

git reset --soft c14809fa

I want to revert the development branch and keep the history on a different branch.

If I checkout the development to a new branch before I revert the commits - For example

git checkout -b beforeRevert

Than I will checkout back to the development branch and do the reveting ( because I want to continue working on the data from the commits i had revert to )

The other branch, beforeRevert branch, will keep all the history and data of the "before reverting" that will use again someday, but won't include in the current development branch? Or the reverting on the development branch will somehow effects the beforeRevert branch?

like image 298
Aviv Paz Avatar asked Sep 29 '15 10:09

Aviv Paz


People also ask

Can I go back to a specific commit in git?

Steps to revert a Git commit Locate the ID of the commit to revert with the git log or reflog command. Issue the git revert command and provide the commit ID of interest. Supply a meaningful Git commit message to describe why the revert was needed.

Does git revert remove history?

In Git, this is actually called a reset, not a revert. Reverting has two important advantages over resetting. First, it doesn't change the project history, which makes it a “safe” operation for commits that have already been published to a shared repository.


1 Answers

If you're sure that neither soft reset nor creating multiple branches work for your use case, you could do

git diff HEAD commit_hash_to_go_to | git apply

This will create a diff of changes between the latest commit on your branch and the commit with the desired state and automatically apply it. That will simply change the files, it's your job to add them to staging and commit the result. Might be useful if you want to try out different solutions and keep the history of your changes WITHIN the same branch or avoid multiplying local branches.

If you encounter "cannot apply binary patch to without full index line" error, add --binary flag:

git diff HEAD commit_hash_to_go_to --binary | git apply

Before doing this ensure that you've got no uncommitted changes - otherwise the patch won't be applied (it's atomic so either all changes go through or none, so you won't end up in an inconsistent state)

NOTE: this simply changes the files and marks them as modified. It does NOT alter commit history or create new commits

like image 151
wondersz1 Avatar answered Sep 24 '22 17:09

wondersz1