Home > database >  How to get the previous state of the repository in git?
How to get the previous state of the repository in git?

Time:10-12

I need to get back to the specific state in the repository.

I try this:

git push -f origin <sha_of_previous_commit>:master

but I think it does even more mess...

This is my current state:

*comit4 <-branch_name[1 note]
|
*comit3 <-[1 note]
|
*comit2 <- master
|
*comit1
|
*Added basic information.
|
*Initial commit

And I would like it to be like this:

*Added basic information. <-master
|
*Initial commit 

The last SHA of commit that I see in the log is from commit2.

Actually commit3 and commit4 should be in another branch but when I pushed it stay in the master branch. Then I try the command above and this happened.

Please can you help me how to get back the state above?

CodePudding user response:

The following command should help you:

git reset HEAD@{1}

I also highly recommend using git reflog. This command manages the information recorded in the reflogs.

CodePudding user response:

The answer is trivial because every commit holds a full snapshot of every file. So:

*comit4 <-branch_name[1 note]
|
*comit3 <-[1 note]
|
*comit2 <- master
|
*comit1
|
*Added basic information.
|
*Initial commit

(I'm using your misspelling of commit here where appropriate, to match your question.)

Branch names, like master, simply serve to help you (and Git) find one specific commit, namely the last commit of the branch. So:

... I would like it to be like this:

*Added basic information. <-master
|
*Initial commit 

This merely requires that you tell Git to make the name master find that commit.

To do that, you use git branch or git reset, with whatever options are required. For git branch:

  • you must not be on the branch at the time (so you must be "on" some other branch), and
  • you need to use the --force option.

If you don't have another branch to be "on", you can use git reset, which is a bit more complicated (you need to choose from --soft, --mixed, or --hard). But you go on to say:

Actually commit3 and commit4 should be in another branch but when I pushed it stay in the master branch.

Commits are often on multiple branches at the same time, not just one branch. The set of commits that are "on" a branch are those you can reach by following the graph line backwards ("down the page", given the way you have drawn the commits here). So you would just set some other branch name to point to the last commit. That way those commits are on that branch—and, until you adjust master, on both branches.

I [ran]

git push -f origin <sha_of_previous_commit>:master

The git push command:

  1. sends any needed commits to some other Git repository, then
  2. asks or commands the other Git repository to set some of its branch name(s)

so, assuming you used the hash ID for comit2 here, this sent comit2 to the other Git repository if it did not already have it, sent comit1 if they did not have that, sent Added basic information if they did not have that, and sent Initial commit if they did not have that. At this point they had all the needed commits, so it commanded (-f) them to set their name master.

If you were not denied permission—basic Git can't do that as it has no permissions controls at all, but most hosting sites add on permissions for obvious reasons—this got what you wanted in some other Git repository. It did not do anything for your Git repository.

To fix up your Git repository, you should first create the name you want:

git branch new-branch-name

and then switch to it:

git switch new-branch-name

(you can combine these into git switch -c new-branch-name if you prefer, or the old git checkout -b new-branch-name if your Git is too old to have git switch). That makes it easy to use git branch -f to move your master back:

git branch -f master <hash ID for second commit>

which forcibly moves your master backwards to point to comit2.

Since this doesn't need git reset, we don't have to get complicated with the soft/mixed/hard stuff. That's the way to go here.

  • Related