Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I uncommit the last commit in a git bare repository?

Taking into consideration that there are several git commands that make no sense in a bare repository (because bare repositories don't use indexes and do not have a working directory),

git reset --hard HEAD^ 

is not a solution to uncommit the last change in such a repository.

Searching through the Internet, all I could find related to the topic is this, in which I am presented three ways of doing this:
1. "update the ref manually (which involves plumbing)";
2. "git push -f from a non-bare repository";
3. "git branch -f this $that".

Which solution do yo think is more appropriate or what other ways are there to do this? Unfortunately, the documentation I found about git bare repositories is fairly poor.

like image 318
Lavinia-Gabriela Dobrovolschi Avatar asked Jan 07 '11 11:01

Lavinia-Gabriela Dobrovolschi


People also ask

How do I undo the last commit in git?

The revert command You can find the name of the commit you want to revert using git log . The first commit that's described there is the last commit created. Then you can copy from there the alphanumerical name and use that in the revert command.

How do I Unpush my last commit?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit.

How do I revert last commit in remote repository?

To undo the last commit from a remote git repository, you can use the git reset command. command. This will undo the last commit locally. command to force push the local commit which was reverted to the remote git repository.

How can I see last commit?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.


3 Answers

You can use the git update-ref command. To remove the last commit, you would use:

$ git update-ref HEAD HEAD^

Or if you're not in the branch from which you cant to remove the last commit:

$ git update-ref refs/heads/branch-name branch-name^

You could also pass a sha1 if you want:

$ git update-ref refs/heads/branch-name a12d48e2

See the documentation of the git-update-ref command.

like image 59
Sylvain Defresne Avatar answered Oct 25 '22 19:10

Sylvain Defresne


If you use the following in a bare repo:

git reset --soft <commit>

then you don't run into the issues you have using --hard and --mixed options in a bare repo since you're not trying to change something the bare repo doesn't have (i.e. working tree and index). In your case specifically you would want to use (from the bare repo):

git reset --soft HEAD^

To switch branches on the remote repo do:

git symbolic-ref HEAD refs/heads/<branch_name>

To see current selected branch use:

git symbolic-ref HEAD

https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html

like image 43
Hazok Avatar answered Oct 25 '22 17:10

Hazok


The git push -f should work fine:
if you clone that bare repo, remove the last commit (git reset --hard HEAD^ as you mention, but in a local non-bare repo) and push back (-f):

  • you don't change any SHA1 for the other commits preceding the one you remove.
  • you are sure you push back the exact content of the bare repo minus the extra commit (because you just cloned it first).
like image 6
VonC Avatar answered Oct 25 '22 17:10

VonC