While working on Moose, I accidentally deleted the wrong file in git, though code in that file was very small & easy, so I rewrote it and added it to git. But if it would have been a bigger file, then it might not be easy for me to rewrite it, so recovering that file using git is a better option. Here is what I did:
$ git add file1
$ git commit -m "added file1"
$ git rm file1 # I was not supposed to do this
$ git add file2 file3
$ git commit -m "added file2 and file3"
$ git log -p
commit 51f30ce9ce4cbf6573861427f2229c010e6f03a5
Author: Upasana Shukla <me@upasana.me>
Date: Wed Jul 31 18:13:03 2013 +0530
added file2 and file3
diff --git a/file1 b/file1
deleted file mode 100644
index 433eb17..0000000
--- a/file1
+++ /dev/null
@@ -1 +0,0 @@
-this is file1
diff --git a/file2 b/file2
new file mode 100644
index 0000000..f138820
--- /dev/null
+++ b/file2
@@ -0,0 +1 @@
+this is file2
diff --git a/file3 b/file3
new file mode 100644
index 0000000..a309e46
--- /dev/null
+++ b/file3
@@ -0,0 +1 @@
+this is file3
commit 6df7df21ca590a751e1e7ac6a423c426ada1cfac
Author: Upasana Shukla <me@upasana.me>
Date: Wed Jul 31 18:11:22 2013 +0530
added file1
diff --git a/file1 b/file1
new file mode 100644
index 0000000..433eb17
--- /dev/null
+++ b/file1
@@ -0,0 +1 @@
+this is file1
How to recover file1?
Now I deleted file1, but I need it, so I did this to recover it:
- I copied file2 & file3 to somewhere else, other than git repository
$ cp file1 file3 ~/
- Checked out HEAD^.
HEAD points to what I have checked out right now i.e. commit with SHA 51f30ce9ce4cbf6573861427f2229c010e6f03a5
^ points to commit before HEAD. In this case, ^ is the commit in which I had file1 i.e. commit with SHA 6df7df21ca590a751e1e7ac6a423c426ada1cfac
or I can directly checkout the commit with SHA 6df7df21ca590a751e1e7ac6a423c426ada1cfac, like this:
$ git checkout HEAD^
$ git checkout 6df7df21ca590a751e1e7ac6a423c426ada1cfac
Output of git log -p after executing above commands:
$ git log -p commit 6df7df21ca590a751e1e7ac6a423c426ada1cfac Author: Upasana Shukla <me@upasana.me> Date: Wed Jul 31 18:11:22 2013 +0530 added file1 diff --git a/file1 b/file1 new file mode 100644 index 0000000..433eb17 --- /dev/null +++ b/file1 @@ -0,0 +1 @@ +this is file1
- Now I want file2 and file3 in my repository, so I did this to add those two files:
$ mv ~/file2 ~/file3 . #here, '.' is the directory of my git repository
$ git add file2 file3
$ git commit "added file2 & file3"
End result:
$ git log -p commit 646c1b61731c66ca09d63f0a87a2a68785476474 Author: Upasana Shukla <me@upasana.me> Date: Wed Jul 31 18:24:19 2013 +0530 added file2 & file3 diff --git a/file2 b/file2 new file mode 100644 index 0000000..f138820 --- /dev/null +++ b/file2 @@ -0,0 +1 @@ +this is file2 diff --git a/file3 b/file3 new file mode 100644 index 0000000..a309e46 --- /dev/null +++ b/file3 @@ -0,0 +1 @@ +this is file3 commit 6df7df21ca590a751e1e7ac6a423c426ada1cfac Author: Upasana Shukla <me@upasana.me> Date: Wed Jul 31 18:11:22 2013 +0530 added file1 diff --git a/file1 b/file1 new file mode 100644 index 0000000..433eb17 --- /dev/null +++ b/file1 @@ -0,0 +1 @@ +this is file1
Credits:
Thanks to Jesse, for a super nice explanation.
Nice. I hope you understand now, why you use version control in first place 😉
Thank you 🙂
Yep, I understood it 🙂