How to restore a previous version as a new commit in Git?

Firstly, I saw the answer to this question here earlier, but he was buried so many “answers” ​​that do not answer my question correctly, that I can not find it again.

So, like this: How do I restore to Git the previous version in my story so that it becomes a new commit on top of my current story?

Basically, this is the most fundamental thing I would like to do with a version control system. Just doing a reset does not work, because it discards my history, a simple rollback does not work either, because sometimes it gives me error messages (what I want to do should be possible without error messages).

Edit: If I just do git revert, this happens:

git revert HEAD~1
error: could not revert f1b44e3... Towards translating API to kernel.
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
+4
source share
3 answers

Just " checkoutcommit". This will overwrite your current working directory with the indicated snapshot (commit) of your repo from the history and make your new working set, which you can execute and commit as you wish. If you are commitright after this, then your repo will have the same contents of the file system as the commit that you performed checkout(unless you have other unstated or phased changes)

This will not rewrite the history, as well as edit or delete any previous commits - therefore it works similarly to the "rollback" in Mediawiki:

cd ~/git/your-repo-root
git log
# find the commit id you want
git checkout <commitId> . 
# IMPORTANT NOTE: the trailing `.` in the previous line is important!
git commit -m "Restoring old source code"

. : Git

. ()

. () " " - - git, , Windows, Linux, MacOS MS -DOS. , .. " ". :

checkout

: checkout - Git - (a la svn switch) (a la svn update -r <id>). : https://git-scm.com/docs/git-checkout - , , , Git TFS ( "Checkout" - ).

+4

: , , . . , .

.

rebase.

git rebase -i HEAD~n

n , , . , , 3 :

git rebase -i HEAD~4

, VIM Nano ( ). .

VIM, esc :exit enter.

. , .

git push -f

, . !

, .

docs: https://git-scm.com/docs/rebase

0

git revert https:// git -scm.com/docs/git -revert

git revert HEAD~1
-1

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


All Articles