Posts Git undo most recent local commits
Post
Cancel

Git undo most recent local commits

Today I did a local commit ready to be pushed to GitHub.com.

That is when I realised I also added a scratch file that is used for storing my notes.

The steps below show how to resolve the issue.

When working locally without a remote repository

  • To unstage all locally committed files
1
$ git reset
  • To unstage a specific locally committed file
1
$ git reset scratch

To understand the problem properly, let us try to reproduce the scenario again, and then use the above solution to resolve the issue.

When working locally without a remote repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$ mkdir test
$ cd test
$ test git init
$ touch file1
$ echo "add this file to git" > file1 
$ touch scratch
$ echo "do not add this file to git" > scratch
$ git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        file1
        scratch
    
    nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status

    On branch master
    
    No commits yet
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    	new file:   file1
    	new file:   scratch

$ #oops !
$ #I added both the files by mistake, now let us use the abvoe solution to resolve this
$ git reset
$ git status                                                                            
  On branch master
  
  No commits yet
  
  Untracked files:
    (use "git add <file>..." to include in what will be committed)
  
  	file1
  	scratch
  
  nothing added to commit but untracked files present (use "git add" to track)

This post is licensed under CC BY 4.0 by the author.