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.
Related
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 😉
I want my entire repository to roll back to a certain previous commit (that is 20 commits behind my latest commit).
I tried using git reset --hard <commit#> and git revert <commit#>
Both the git commands run successfully with the message - Head is now at <commithead>
But then when I see the code in my repo, I can still see the edits I made in my latest commits. How can I remove all the edits after the particular commit I am reverting to?
Am I missing out on some step here?
After your git reset --hard, use to be sure the git restore command
cd /path/to/repository
git restore -- .
That should restore (as its name implies) the working tree with what is in HEAD (and you just reset HEAD to the right commit)
Yesterday when I was at work, I was working on my project that i have on my home computer. The problem was when I tried cloning the repo it eclipse would crash. So instead I downloaded it manually and initialized the repo and then force pushed to my home computer. This deleted all the history and files in on the repo. I did some googling and tried reverting the commit but it the force push erased the commit history so I can only go back to the force push commit.
I have a computer at home which hasn't pulled the updates since this mistake and I was able to get its commit SHA. I used that on the web and found the last version its still there but I can't pull that commit because it got "deleted" and won't show in the repos history. Is there a way I can use the SHA from the last good commit and make that the master branch restoring my history?
When changing the HEAD (the working position in the git repository), it tracks your history in the reflog. Executing git reflog in the command line will show your complete history. You can read more about it on git-scm.com
When you have the desired commit SHA, you can do a git checkout with it. Executing git checkout [SHA1] will take you to the desired position in the git history. From there, you can make a new branch from it by running git checkout -b "new_branchname".
If you want to set your current branch to the commit, you can run git reset --hard [SHA1]. This would reset the current branch, but the previous state can however be found in the reflog.
I do NOT recommend doing this as general practice BUT...You should be able to force push from the other computer, thus resetting the repo to its former state. Providing you don't want to keep anything you did while making a giant mess of your git repo yesterday.
Since you know the right version you want to recover, it will make things easier.
In your local repo, use below commands:
git reset --hard <commit sha value>
git push -f
Note: if you execute the commands in the local not initialized (not cloned from remote), you should fetch first by git fetch --all, and then execute the two commands .
Now the version will go back to the latest as you need both locally and remotely.
I got a Github repository from client where there is no code.
I cloned this repository and pasted my code folder to this repository and pushed the code to git.
But now I need to revert back my commit. For this I did the following steps:
git revert xxxxxxxxxxxxxxxx(commit)
git add .
git commit -a -m "reverted back"
git push origin master
After this I went to my github page, there I find project folder with app/views, config, db, log, public, tmp folders where revert is not properly done.
How to undo my last 2 commits (both commit and revert) and get back to the main repository as it is?
Please help me, I am worried.
The simplest way is just to hard reset to commit preceding the bad one:
git reset --hard xxxxxxxxxxxxxxxx^
git push -f origin <your-branch-name>
There xxxxxxxxxxxxxxxx is your sha, that you reverted in the first time.
Note the ^ before xxxxxxxxxxxxxxxx. It means that you reset to the commit before xxxxxxxxxxxxxxxx.
Or you can specify exact hash to reset to, i.e.
git reset --hard A
Before resetting your branch is looking like:
A -> xxxxxxxxxxxxxxxx -> B(bad)
After git reset --hard xxxxxxxxxxxxxxxx^
A
git push -f origin <your-branch-name> pushes your local changes made on the previous step to the Github server
Be careful with force pushing though, because you force your local repository state on the server. So you lose server state forever
There was some github.com down time today that I wasn't aware of until I went to push about one dozen local commits.
https://status.github.com/messages
https://twitter.com/githubstatus
Here's the message I received when trying to push to github.com:
remote: Unexpected system error after push was received.
remote: These changes may not be reflected on github.com!
remote: Your unique error code: abcdefghijklmnopqrstuvwxuz
Now that github.com is back up, when I view the project commit history online, I can see these dozen commits have not been pushed up to the repo.
I figured I could just push these changes again with git push origin master, but I am told Everything up-to-date. Similarily a git pull origin master also shows Everything up-to-date.
How can I get these local changes pushed up to my repo on github.com?
I hate to answer my own question so quickly, but with a little tinkering, here's a quick work around I discovered:
echo "bar" >> foo.txt
git add foo.txt
git commit -m "Add foo.txt"
git push origin master
git rm foo.txt
git commit -m "Remove foo.txt"
git push origin master
This properly refreshed the commit history for my github.com repo. This should be safe to do, but definitely take a backup of your local code before trying it.
I agree with Yen Chi, he should have made this an answer. At the least, do an empty commit:
git commit --allow-empty
Pushing another commit for me didn't work.
Instead, creating a dummy branch, from the web interface, solved the problem.
I just had this too, and yes, pushing another commit fixed the problem.
I think that the source of the problem may be that I was pulling from the same repo at the same time (I use submodules). That pull yielded everything up to date, while the first push was still hanging (and then eventually spitting out that error message).
git commit --amend
git push -f origin HEAD
or if you don't like that
git commit --allow-empty
git push origin HEAD