How to undo the most recent comment on git - github

While using git hub I wanted to delete/undo a comment that I had recently posted. But I was unable to do it. So I want to know how to undo the recent comment in git.

Here is one of the several and easiest way to do it.
First of all lets check commit history.
git log --oneline
Now in the output you will see a list of commits. Something like shown below.
241b8e7 (HEAD -> papers) Added paper-logic according to new requirements
8ae8f7d paper backend fixed
3c9a3ce toggle sidebar change added
af1af34 link to style tag added
Now as per your question you want to undo previous commit i.e you want your project to be at 2nd last commit.
So "code"(left of commit statement- "paper backend fixed" ) for 2nd last commit from the above example is 8ae8f7d. Copy this code.
To go to that commit enter the following in terminal.
git reset --hard <code>
What this will do is forcefully change all your project to the stage as it was on mentioned commit.
Note that it is irreversible i.e once you have gone to previous commit you cannot come to commits above it

You need to undo the commit as well.
Undo Last Git Commit with reset
$ git reset --soft HEAD~1
If you are not familiar with this notation, “HEAD~1”
“HEAD~1” means that you want to reset the HEAD (the last commit) to one commit before in the log history.
You can use this as well
$ git reset --hard <commit sha1 | reference>
$ git push --force
to check the commit hash and reference you can use
$ git log --oneline
or
$ git log
Undo Last Git Commit with revert
$ git log
$ git revert <commit sha1 | reference>
$ git push --force
You might want to check these as well
How do I undo the most recent local commits in Git?

Related

Git revert and reset : Files in the repository are not getting rolled back

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)

delete branch and messed up backup..need to recover or jump off bridge [duplicate]

I just deleted the wrong branch with some experimental changes I need with git branch -D branchName.
How do I recover the branch?
You can use git reflog to find the SHA1 of the last commit of the branch. From that point, you can recreate a branch using
git branch branchName <sha1>
Edit: As #seagullJS says, the branch -D command tells you the sha1, so if you haven't closed the terminal yet it becomes real easy. For example this deletes and then immediately restores a branch named master2:
user#MY-PC /C/MyRepo (master)
$ git branch -D master2
Deleted branch master2 (was 130d7ba). <-- This is the SHA1 we need to restore it!
user#MY-PC /C/MyRepo (master)
$ git branch master2 130d7ba
If you know the last SHA1 of the branch, you can try
git branch branchName <SHA1>
You can find the SHA1 using git reflog, described in the solution --defect link--.
If you just deleted the branch, you will see something like this in your terminal:
Deleted branch branch_name(was e562d13)
where e562d13 is a unique ID (a.k.a. the "SHA" or "hash"), with this you can restore the deleted branch.
To restore the branch, use:
git checkout -b <branch_name> <sha>
for example:
git checkout -b branch_name e562d13
If you haven't push the deletion yet, you can simply do :
$ git checkout deletedBranchName
Follow these Steps:
1: Enter:
git reflog show
This will display all the Commit history, you need to select the sha-1 that has the last commit that you want to get back
2: create a branch name with the Sha-1 ID you selected eg: 8c87714
git branch your-branch-name 8c87714
First: back up your entire directory, including the .git directory.
Second: You can use git fsck --lost-found to obtain the ID of the lost commits.
Third: rebase or merge onto the lost commit.
Fourth: Always think twice before using -D or --force with git :)
You could also read this good discussion of how to recover from this kind of error.
EDIT: By the way, don't run git gc (or allow it to run by itself - i.e. don't run git fetch or anything similar) or you may lose your commits for ever.
If you deleted a branch via Source Tree, you could easily find the SHA1 of the deleted branch by going to View -> Show Command History.
It should have the next format:
Deleting branch ...
...
Deleted branch %NAME% (was %SHA1%)
...
Then just follow the original answer.
git branch branchName <sha1>
Thanks, this worked.
git branch new_branch_name sha1
git checkout new_branch_name
//can see my old checked in files in my old branch
This worked for me:
git fsck --full --no-reflogs --unreachable --lost-found
git show d6e883ff45be514397dcb641c5a914f40b938c86
git branch helpme 15e521b0f716269718bb4e4edc81442a6c11c139
if you deleted a branch using GUI of a Jetbrains IDE(Goland, phpstorm etc)
go to
git windows(left-down corner of IDE) -> console tab -> now you can see log of executed commands by IDE and find the branch name and SHA1 from this log
If you are using IntelliJ IDEA, in Event Log you'll see something like that:
And may simply restore your branch.
First of all, don't get panic. You are in the right place.
Go on champ, we all make mistakes! That's how we learn!
I wish you health, happiness and success!
Oh for the answer! I think you already figured out!
If not! here is the answer.
use git reflog
git checkout branch branch_name commitsha
For more clarification, in the second command branch_name is the name you want to give your branch. commitsha is sha number you want to check out. which you will get from git reflog command.
Once again Happy coding!

git(hub) lost all my changes in group project

I am working on an eclipse project with two friends. We use Git(Hub) to work as efficient as possible. Here is the problem: I worked on the project and uploaded the changes. Unfortunately, my friends were not able to see my modifications. Nevertheless, I was still able to see my new version in eclipse. Now I tried to upload it again but "there was nothing to upload".
Then I used git fetch and rebase to see if something would happen.
Unfortunately, all changes are gone now. I used git commit --amend and they "found a swap file by the name xx with the date of my last upload!".
Does anyone know how can I get back to my working version now?
If you didn't push your changes after messed up local history, then reset your local with remote.
$ git branch <branch-name> # backup the branch for safety
$ git fetch # sync with remote
$ git reset --hard origin/<branch-name> # reset local branch with remote branch
Alternate: git reflog will show you the whole git history you executed the commands. Copy the last working commit-hash you want to go back.
$ git reflog
Checkout to that commit.
$ git checkout <commit-hash>
$ git log # see the commit history
If all things are ok. Then checkout to a new branch. If you do any change in new branch then do Add, Commit and Push or, just Push to remote.
$ git checkout -b new-branch # create a new branch called 'new-branch' and checkout to that branch
$ git add .
$ git commit -m 'message'
$ git push origin HEAD # push to remote new-branch

committed without git pull, not pushed due to conflict

I am using netbeans to pull/commit/push to git. I have accidentally make commit before pull request and now it's asking me Rebase/Merge. Either of option gives me error. I tried with following links using Windows git shell
How to undo last commit(s) in Git?
How to revert Git repository to a previous commit?
How to undo last commit
Remove a git commit which has not pushed
I have tried following commands:
git checkout <commit sha key>
git reset --hard <commit sha key>
NOTE: I have commited change but not pushed!
try git reset --soft HEAD~1
when you haven't pushed yet, a soft reset will take all the changes from the latest commit, and place them back onto stage again. in other words, the status of your repo will look exactly as it was the moment before you commited.

How to delete commit history in gerrit in eclipse?

I have done some incorrect commits using gerrit in eclipse. Now, I want to delete my commit history.
*Doing hard reset to the original head i.e. before any commit does not work.
Open command prompt or git bash.
git revert HEAD~1..HEAD
It will checkout the previous commit. It will get you in to a state called detached head. create new branch from this point and continue your work,
if the commit is a merge add -m flag
git revert -m HEAD~1..HEAD