-1

I'm new to git and I have project managed with git on bitbucekt.

I want return to a specific version :
I'm on 4b2d8be commit and I want to go to 77ba516 commit.
commits after 77ba516 are unnecessary that's why I would like to delete the work in it.

I tried with git revert unnecessary 77ba516 but just the files in this commit changes in current branche and the other work added in commits after this commit are not deleted.
any helps !! enter image description here

  • If you want to rewrite history of your current branch so that the commits after `77ba512`do not show up anymore, you need to do: `git reset --hard 77ba516` (careful because that will clean index and working tree). If you want to snoop into that commit without moving the branch that you are on, then you can do a checkout `git checkout 77ba516` – eftshift0 Jan 27 '23 at 13:42
  • @eftshift0 Ok, so if I do `git reset --hard 77ba516` I will have the working tree on this commit that's it ? – MAHA OUEGHLANI Jan 27 '23 at 13:47
  • working tree (removing any changes you might have laying around there, that's why you need to be careful) and index too.... it's like the commits that were following it never happened. – eftshift0 Jan 27 '23 at 13:54
  • @eftshift0 I run `git reset --hard 77ba516` and the head is on 77ba516 now, but when I do git status I have `Your branch is behind 'origin/feature/dev-maha' by 12 commits, and can be fast-forwarded.` the 12 commits are the commits that's I don't want them, I can't push now new changes just when I pull this 12 commits, and if I pull them it's like we didn't do anything ... – MAHA OUEGHLANI Jan 27 '23 at 14:08
  • 1
    are more people using the upstream branch? If you are sure that it's ok to remove those commits from the remote branch too (not something to be done if more people are working on that branch already) then you can do: `git push origin HEAD:feature/dev-maha` so that the remote branch gets those commits removed too. – eftshift0 Jan 27 '23 at 14:58

1 Answers1

1

There are a few different ways to "return" to a specific version of a project managed with Git, depending on what you mean by "return" and what your goal is.

To go back to a specific commit and discard any changes made after that commit, you can use the git reset command. The git reset command allows you to move the current branch pointer to a different commit, and can also be used to discard commits.

You can use the command git reset --hard to return to the commit hash "77ba516" and discarding all commits after that.

git reset --hard 77ba516

Please be aware that this command discards commits permanently and can't be undone, so make sure to backup your work before proceeding with this step.

And if you want to save the changes made in commits after 77ba516, before you move to 77ba516 commit, you can create a new branch from this commit, then you can move to this new branch and keep your changes in the other branch.

git branch new_branch
git reset --hard 77ba516
git checkout new_branch