How to undo the last commit on GitHub? - github

I want to undo the last commit pushed to GitHub on the master branch of a repository, and make it so that it's as if this commit never existed and doesn't appear in the commit history.
How do I go about this?
Note to those voting to close — the proposed alternative questions are needlessly complex, have condescending answers, and are littered with giant walls of text that are difficult to sift through.
Hence, this simple question with a simple answer for my benefit and that of posterity.

I think you're looking for the --force argument.
You can reset to a specific commit with git reset --hard {commit sha} and then force the change to origin with git push --force.
Note that if others are using your repo, they will need to use git pull --force or they may inadvertently put your unwanted commit back into the repo.
I recommend reading the help documentation for git reset and git push before taking action.

If you wish to rewind the master branch of the repository to a previous pushed commit, simply run this command — of course, with the appropriate commit hash:
git reset --hard a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9
git push --force
This may have unintended consequences if, for instance, there are collaborators on your repository. But, I am sure you know what you're doing 😉

Related

I am trying to go back to a specific commit in Github to erase the mistake I made

I have looked at many past posts without a specific response. I used git reset --hard to get me to a point in time when my code was not all jacked up. Yes I should have used a branch but I didn't. Now I have a message of HEAD is now at 0600b73 fixed error. What I would like to know is how to commit those changes, so I can push them back to Git, so they are the current head? I am not even sure if VS Code even recorded the changes. Git can be confusing so any help would be appreciated.
You haven't actually proven you have changes, HEAD is now at 0600b73 fixed error., that does not mean you have changes. That basically means that you moved the HEAD of git to a commit, generally you want it on a branch.
git status will tell you if you have things that need to be committed.
I am not even sure if VS Code even recorded the changes
VScode has some cool features around git but it doesn't actually record changes, unless you are referring to saving files. If you save the file, well it saves.
What you probably want to do is (I am assuming you actually do have changes):
confirm you have changes git status
move to a branch git checkout -b my-fix
add those changes git add . (adds all changes/also make sure you are at the root of the repo) or git add -p will let you look closely at changes and give you an interface to decide if you want to stage the change or not.
commit the changes git commit -m "Your message" or you can do a longer message with git commit -p
At this point you can merge this branch in to master, push the branch. It's up to you.
push your changes, git push origin my-fix, make a pull request and merge to master. You will then want to change to master and pull master
merge git checkout master , git merge my-fix
rebase git checkout master , git rebase my-fix
Here is some git learning material:
https://learngitbranching.js.org/
https://www.codecademy.com/learn/learn-git
https://try.github.io/
https://www.atlassian.com/git

How to revert a pull request commit on GitHub

I am trying to revert my latest commit on GitHub. All the information I have looked through says that there should be a revert button in the pull request but I do not see it, and cannot find it.
Is there anyway to do this on GitHub? Or could I do it terminal with a few rebase commands?
Assuming this pull request merge is a commit merge (what I would expect), then you may try the following from the Git bash:
git checkout your_branch
git revert <hash of merge commit> -m 1
git push origin your_branch
This solution assumes that you want to revert back to the branch into which the pull request merge was made. If you want to follow the incoming branch, then use -m 2 instead.
To find the SHA-1 hash of the merge commit, you may use git log, and then copy over the hash from the first commit, which should appear at the top.
Note that nuking the merge commit and then doing a force push is generally a bad idea here. The reason it is bad is because your branch is published on GitHub. This means that rewriting the history of that branch could cause problems for anyone besides you who happens to be sharing this branch.
In android studio click version control tab in the bottom. Then click log
Then your all the commits will be visible. Then right click on relvent commit and revert it.
Then commit changes and push again.
Get the hash of the commit in which you want to revert back. Then do:
git checkout 54722c31619b0662b379923d0be4b8f8f1f125c9
The long number you are seeing is the hash of that particular commit in which you want to revert back.
Then force push into the branch you want to revert back.
git push origin <your_branch_name> --force
I hope this helps. Happy coding :)

GitHub: Commit a point in history as the head of master

There is a certain commit I did to my Git repository which I host in GitHub. After that commit I've made several other commits, which were bad and redundant, in a second look. I thus need to revert to the certain commit / certain point in history before these bad changes.
I didn't find a button like "revert to this version" or "commit this version as the head of this branch (master)".
As you can see, I just want to make that older version the head of the master branch. How will you do that from GitHub?
Update
I emphasize: I ask on GitHub, not on git or any GUI other than GitHub.
If I understand you correctly you want a past commit as the last commit on the branch.
If so, using examples with origin and master:
Use git reset <comit_id> and then git push origin +master to push & delete all commits past the one you reset to. Notice the + sign before the branch name (master).
Note that this is irreversible (as far as I know) so take the necessary precautions.

Revert git reset and push

I've executed the commands
git reset --hard 6eb7d13f32cf110a1122206e58df381c000a987e
git push --force
on my github repository. The problem is that the full SHA I used is incorrect. Now, my repository state is so old. So, I'm trying to revert the repository to the last commit I pushed before the execution of these two commands.
The output of a git reflogis
6eb7d13 HEAD#{0}: reset: moving to 6eb7d13f32cf110a1122206e58df381c000a987e
4d0a0fd HEAD#{1}: pull: Fast-forward
82859b3 HEAD#{2}: commit: including command to install dependencies
e3c3ab8 HEAD#{3}: clone: from https://github.com/<user>/<repository>.git
The first output line points out the mistake I'd like to undo.
Is it possible to revert it? Thanks.
To get back to the state you were in just before the bad reset and push, reset and push again. The commit should still be there, unreferenced commits typically hang around for weeks. Looks like that's 4d0a0fd.
git reset --hard 4d0a0fd
Check everything is ok before pushing!
git push --force
This is why one should always hesitate before pushing, especially a force push. It gives you a moment to check everything is ok before sharing a mistake.

How to undo a Git rollback

I wanted to rollback to the last commit after making a massive error, but I managed to rollback a little too fair. The commit I wanted to reassert doesn't appear when I enter 'git log' in bash (I suppose because it's no longer in the history). Is there any way I can recover the last commit by date?
I'm also using eGit in eclipse for the same project if that makes things easier. Thanks.
If you are ok with command line, go to you repo, do a git reflog and get the commit which you want to "rollback" to and do a git reset --hard <commit>
You would also be able to do git reset --hard HEAD#{1} and then come back to egit and rollback to the desired commit.
I find that generally it's better to make your changes forward in time rather than backward.
Git's approach is to "revert" the commit. When you revert a commit, you check out into your working directory the inverse of the commit in question. Then you add and commit that, and you've just made a NEW commit, that commits the "undoing" of the commit you're reverting, AND it leaves a record in history that such a thing happened, so if you want to undo your undoing, it's easy to do.