Return to a specific commit without losing history

I know that this type of question has many duplicates, but I wanted to open a new one because I did not find in all other questions an explanation of the best way to do this the way I want.

I know that I can go back and save the story by doing:

git reset --soft c14809fa 

I want to return the development branch and save the history in another branch.

If I check development on a new branch before I return my commits - for example

 git checkout -b beforeRevert 

Then I will return to the development branch and do a re-check (because I want to continue working with the data from the commits that I returned to)

The other branch, the beforeRevert branch, will save the entire history and data "before returning", which will be used again someday, but will not be included in the current development branch? Or will beforeRevert development branch somehow affect the beforeRevert branch?

+6
source share
3 answers

The easiest way to do this, as you say, is simply to create a new branch where HEAD is, and then return development to the commit you want to resume work with:

 git checkout development # Make HEAD point to the 'development' branch git branch beforeRevert # Create a new branch reference pointing to HEAD git reset --hard c14809fa # Move HEAD, the index and your working copy to c14809fa 

Here is a graphical representation of what will happen:

  Step 1 Step 2 Step 3 develop develop, beforeRevert develop beforeRevert / / / / ABCDEF ABCDEF ABCDEF ^ ^ ^ HEAD HEAD HEAD 

The important thing is that HEAD always points to the development branch, so that the branch that moves when git reset --hard c14809fa . A new branch beforeRevert will indicate where HEAD was before returning.

+6
source

Do not revert your changes, just go back to the previous commit and then open a new branch.

 git checkout development git checkout c14809fa git checkout -b newBranch 

So you can visit the development branch to see the old changes and history, and you can make further changes to the 'newBranch'

You can rename branches if you want to use another method -

 git branch -m <oldname> <newname> 
+1
source

If you are sure that neither a soft reset, nor the creation of several branches are suitable for your use case, you can do

 git diff HEAD commit_hash_to_go_to | git apply 

This will create the difference between the last commit in your branch and the commit with the desired state and automatically apply it. It will simply change the files, it is your job to add them to the production and record the result. This can be useful if you want to try out different solutions and keep a history of changes in the same branch or avoid multiplying local branches.

If you encounter the error "it is impossible to apply binary correction without a full index line", add the --binary flag:

 git diff HEAD commit_hash_to_go_to --binary | git apply 

Before doing this, make sure that you do not have uncommitted changes - otherwise the patch will not be applied (it is atomic, so either all changes go through or not, so you will not end up in an inconsistent state)

NOTE: this simply modifies the files and marks them as modified. This does NOT change the history of commits and does not create new commits.

+1
source

Source: https://habr.com/ru/post/1232521/


All Articles