First, got "your branch is ahead of origin/master by 3 commits" then my app has reverted to an earlier time with earlier changes.
How can I get what I spent the last 11 hours doing back?
First, got "your branch is ahead of origin/master by 3 commits" then my app has reverted to an earlier time with earlier changes.
How can I get what I spent the last 11 hours doing back?
git reflog
is your friend. Find the commit that you want to be on in that list and you can reset to it (for example:git reset --hard e870e41
).
(If you didn't commit your changes... you might be in trouble - commit early, and commit often!)
git log HEAD@{1}
. If that looks like the right series of commits, then you can git reset HEAD@{1}
. –
Mesmerize git fsck --lost-found
. –
Abscise --hard
. Uncommitted changes will be lost. –
Soliloquize main
and pulling an update of the repo, those commits were seemingly gone. For me, merging my committed changes from the feature branch into main
fixed the issue. –
Doll Before answering, let's add some background, explaining what this HEAD
is.
First of all what is HEAD?
HEAD
is simply a reference to the current commit (latest) on the current branch.
There can only be a single HEAD
at any given time (excluding git worktree
).
The content of HEAD
is stored inside .git/HEAD
and it contains the 40 bytes SHA-1 of the current commit.
detached HEAD
If you are not on the latest commit - meaning that HEAD
is pointing to a prior commit in history it's called detached HEAD
.
On the command line, it will look like this - SHA-1 instead of the branch name since the HEAD
is not pointing to the tip of the current branch:
git checkout
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point, you can create a branch and start to work from this point on.
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# Create a new branch forked to the given commit
git checkout -b <branch name>
git reflog
You can always use the reflog
as well.
git reflog
will display any change which updated the HEAD
and checking out the desired reflog entry will set the HEAD
back to this commit.
Every time the HEAD is modified there will be a new entry in the reflog
git reflog
git checkout HEAD@{...}
This will get you back to your desired commit
git reset --hard <commit_id>
"Move" your HEAD back to the desired commit.
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
git rebase --no-autostash
as well.git revert <sha-1>
"Undo" the given commit or commit range.
The reset command will "undo" any changes made in the given commit.
A new commit with the undo patch will be committed while the original commit will remain in the history as well.
# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>
This schema illustrates which command does what.
As you can see there, reset && checkout
modify the HEAD
.
git reset --hard <commit_id>
, removing HEAD
worked! +1 for graphical representation!!. –
Thecla git reflog <branchname>
can be quite useful, since you see the changes of just one branch. –
Wolter git cherry-pick <dangling-commit-hash>
on the branch where I want it to come to put it there. –
Abash Another way to get to the deleted commit is with the git fsck
command.
git fsck --lost-found
This will output something like at the last line:
dangling commit xyz
We can check that it is the same commit using reflog
as suggested in other answers. Now we can do a git merge
git merge xyz
Note:
We cannot get the commit back with fsck
if we have already run a git gc
command which will remove the reference to the dangling commit.
This happened to me just today, so I am writing what came out as a lifesaver for me. My answer is very similar to @Amber 's answer.
First, I did
git reflog
to search for that particular commit's hash, then just copied that hash and did
git cherry-pick <hash>
from that branch. This brought all the change from that lost commit to my current branch, and restored my faith on GIT.
Have a nice day!
git reflog
; I did not see it at all in git logs
. –
Masera First of all use git reflog to list all your commits even the lost commit
git reflog
Then use git log HEAD@{your_commit_number} to find the commit you are looking for. e.g
git log HEAD@{17}
Checkout into the commit after you find it with git checkout HEAD@{your_commit_number} e.g
git checkout HEAD@{17}
You may need to add and commit your changes
git add .
git commit -m"quuck_fix"
Then, you will have to create a temporary branch to restore the commit back to your branch
git branch temp
Finally, you will checkout into your existing branch and then merge the temporary branch.
#git checkout <your_existing_branch> e.g
git checkout main
git merge temp
Try this, This will show all commits recorded in git for a period of time
git reflog
Find the commit you want with
git log HEAD@{3}
or
git log -p HEAD@{3}
Then check it out if it's the right one:
git checkout HEAD@{3}
This will create a detached head for that commit. Add and commit any changes if needed
git status
git add
git commit -m "temp_work"
Now if want to restore commit back to a branch lets say master you will need to name this branch switch to master then merge to master.
git branch temp
git checkout master
git merge temp
Here's also a link specifically for reflog on a Git tutorial site: Atlassian Git Tutorial
If you cannot find your commit with git reflog
and it happen that you were using IntelliJ IDE you can right click on your project root folder -> Local History -> Show History
and revert your changes from there.
I messed up doing git rebase
with git push -f
and this truly saved me since the commit was dropped from the local and remote repositories.
Hope that saves someone's day
Cheers
The safest way is to
git reflog
or git reflog | grep your_phrase
git checkout -b your-branch HEAD@{<your-number>}
I use this quite often and it's dumbfounding me how uncomfortable this still is in git GUI apps such as SourceTree, so I wrote a little utility to help me visualize the history and hidden commits directly in the UI.
These scripts will create (and delete) tags for n
last items from the reflog.
# Create "HEAD-n" tags for n reflog items
./reflog-tag.sh 5
# With prompt
./reflog-tag.sh
# Delete all "HEAD-*" tags
./reflog-untag.sh
On Windows put the scripts somewhere in PATH, then in the repo
sh reflog-tag.sh [<number>]
sh reflot-untag.sh
And the result looks like this:
reflog-tag.sh
#!/bin/bash
# Usage:
# ./reflog-tag.sh <number>
# Usage with prompt:
# ./reflog-tag.sh
# Please provide the number of last commits you want to tag: <number>
# on Windows
# sh reflog-tag.sh <number>
# Number of last commits to tag
n=$1
if [ -z "$n" ]
then
read -p "Please provide the number of last commits you want to tag: " n
fi
# Get the last n commit hashes along with their reflog selectors
entries=$(git reflog --pretty=format:'%h %gd' -n $n)
while read -r entry
do
# Split the entry into commit hash and reflog selector
commit=$(echo $entry | cut -d' ' -f1)
ref=$(echo $entry | cut -d' ' -f2)
# Extract the number from the reflog selector
number=$(echo $ref | grep -o -E '[0-9]+')
# Create a tag for the commit
git tag "HEAD-$number" $commit
done < <(echo "$entries")
echo "Tags created successfully."
reflog-untag.sh
#!/bin/bash
# Get all tags that start with 'HEAD-'
tags=$(git tag -l 'HEAD-*')
for tag in $tags
do
# Delete the tag
git tag -d $tag
done
echo "Tags deleted successfully."
Right click in the graph view to view and execute Custom Actions.
Open Tools > Options > Custom Actions > [Add]
/c sh reflog-tag.sh
/c sh reflog-untag.sh
© 2022 - 2024 — McMap. All rights reserved.