Skip to main content
removed prompt
Source Link
jfs
  • 409.9k
  • 201
  • 1k
  • 1.7k

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.


brian@silver:~/work$ git init
Initialized empty Git repository in .git/
brian@silver:~/work$ echo "testing reset" > file1
brian@silver:~/work$ git add file1
brian@silver:~/work$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1
brian@silver:~/work$ echo "added new file" > file2
brian@silver:~/work$ git add file2
brian@silver:~/work$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2
brian@silver:~/work$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1
brian@silver:~/work$ cat file2
cat: file2: No such file or directory
brian@silver:~/work$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2
brian@silver:~/work$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2
brian@silver:~/work$ cat file2
added new file
$ git init
Initialized empty Git repository in .git/

$ echo "testing reset" > file1
$ git add file1
$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1

$ echo "added new file" > file2
$ git add file2
$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2

$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1

$ cat file2
cat: file2: No such file or directory

$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2

$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2

$ cat file2
added new file

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.


brian@silver:~/work$ git init
Initialized empty Git repository in .git/
brian@silver:~/work$ echo "testing reset" > file1
brian@silver:~/work$ git add file1
brian@silver:~/work$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1
brian@silver:~/work$ echo "added new file" > file2
brian@silver:~/work$ git add file2
brian@silver:~/work$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2
brian@silver:~/work$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1
brian@silver:~/work$ cat file2
cat: file2: No such file or directory
brian@silver:~/work$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2
brian@silver:~/work$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2
brian@silver:~/work$ cat file2
added new file

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.

$ git init
Initialized empty Git repository in .git/

$ echo "testing reset" > file1
$ git add file1
$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1

$ echo "added new file" > file2
$ git add file2
$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2

$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1

$ cat file2
cat: file2: No such file or directory

$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2

$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2

$ cat file2
added new file

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.

Source Link
Brian Riehman
  • 28k
  • 2
  • 25
  • 17

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.

brian@silver:~/work$ git init
Initialized empty Git repository in .git/
brian@silver:~/work$ echo "testing reset" > file1
brian@silver:~/work$ git add file1
brian@silver:~/work$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file1
brian@silver:~/work$ echo "added new file" > file2
brian@silver:~/work$ git add file2
brian@silver:~/work$ git commit -m 'added file2'
Created commit f6e5064: added file2
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file2
brian@silver:~/work$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1
brian@silver:~/work$ cat file2
cat: file2: No such file or directory
brian@silver:~/work$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2
brian@silver:~/work$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2
brian@silver:~/work$ cat file2
added new file

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.