Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How do you replace your current working directory with a previous commit without branching or git revert?

Tags:

git

Is there an easy way to the following with git?

commit copy paste diagram

Basically I want to create a new commit at the top of my commit history that is equivalent to a previous commit from my history (e.g. to restore a project to a previous state).

I don’t want to use branches because I’m teaching this to people who have never used git before, and I want to keep things as “linear” as possible. (Most of my audience just needs to back stuff up, review project history, and restore a project to a previous state if necessary, but nothing else remotely fancy.)

I don’t want to use git revert --no-commit 49a732c..HEAD because this gives an error message if there does happen to be a merge after 49a732c (which I admit is unlikely in my audience, but it does sometimes happen through frantic attempts to make error messages go away).

I also don’t want to delete/rewrite history.

Essentially, is there a simpler way to do this?

# make sure on master and working directory clean
me@laptop:~/Desktop/proj$ git status
On branch master
nothing to commit, working directory clean

# check out commit to restore
me@laptop:~/Desktop/proj$ git checkout 49a732c
Note: checking out '49a732c'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 49a732c... Fix bugs.

# copy project checked out at 49a732c to temp folder
me@laptop:~/Desktop/proj$ cp -r . ../temp

# check out master again
me@laptop:~/Desktop/proj$ git checkout master
Previous HEAD position was 49a732c... Fix bugs.
Switched to branch 'master'

# remove everything except .git
me@laptop:~/Desktop/proj$ rm -rfv !(".git")
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'
removed 'file_1.py'
removed 'file_2.py'

# copy everything except .git from temp (checked out at 49a732c) into working directory
# (so working directory is identical to project at commit 49a732c)
me@laptop:~/Desktop/proj$ cp -nr ../temp/* ./

# make new commit identical to project state at 49a732c
me@laptop:~/Desktop/proj$ git add *
me@laptop:~/Desktop/proj$ git add -u
me@laptop:~/Desktop/proj$ git commit -m “Restore project to commit 49a732c.”
[master 2b6416c] Restore project to commit 49a732c.
 3 files changed, 18 deletions(-)

# remove temp
me@laptop:~/Desktop/proj$ rm -rf ../temp

# new commit 2b6416c is now identical to 49a732c

Alternatively, if branching is the best way to do this, is there a sequence of commands that will always work and won't yield any merge conflicts or tell you to use git stash (assuming current working directory is clean)?

like image 410
rkp Avatar asked Aug 13 '18 03:08

rkp


2 Answers

To create a new commit, restoring the content of an old commit, you can:

  • First, mark the current HEAD of your branch (assuming master here): we will need to move that HEAD without modifying master, so let's create a new temporary branch called 'tmp' where master is.

    git checkout master
    git checkout -b tmp
    

(yes, that is against the "without branching" of the question, but you will delete that tmp branch soon)

  • Then we go back to an older commit with the right content.

    git reset --hard <old_commit>
    

That resets the index (and working tree) to the right content, but that also moves HEAD. However, that moves tmp HEAD, not master HEAD.

  • move back tmp HEAD to where master is, but without modifying the index or the working tree (which are representing what we need for a new commit)

    git reset --soft master
    
  • make a new commit, on top of master/tmp HEAD, which represents the right content (the old commit).

    git commit -m "new commit, image of an old one"
    
  • Finally, force master to be where tmp is: one new commit later.

    git branch -M tmp master
    git checkout master
    git branch -d tmp
    

Now a regular git push is enough, any collaborator can simply pull as usual, and still get the old reset content.

git push
like image 125
VonC Avatar answered Oct 22 '22 05:10

VonC


Assuming you start from a clean worktree, you could do:

cd <root_directory_of_your_repo>
git checkout master
git checkout 49a732c -- .

When you specify a file (in this case . (the root directory of your repo)) as an argument to git checkout, the checkout will not switch branch (the repo HEAD will remain the same). It will just update the index to make that file match the version of that file from the specified commit. Since you specified the root directory of the repo, all files in the index will be updated to match the specified commit 49a732c

like image 29
Alderath Avatar answered Oct 22 '22 04:10

Alderath