how.wtf

How to unadd or uncommit files in Git

· Thomas Taylor

Occasionally, developers will mistakenly git add or git commit files before pushing to remote. Luckily, there are commands that remediate the situation.

How to unadd a file

Unadding a specific file before a commit:

1git reset <file>

How to unadd all files

Unadding all files before a commit:

1git reset

How to uncommit the last commit

If the last commit needs to be uncommitted:

1git reset --soft HEAD~1

The command reads as “undo the last committed changes and preserve the files”. If the files do not need to be preserved, --hard may be used.

At this point, the files may be unadded if desired.

Aliasing

Aliases allow for ease-of-use in daily workflows:

1git config --global alias.unadd 'reset HEAD --'
2git config --global alias.uncommit 'reset --soft HEAD~1'

Usage:

 1> git add example.txt
 2> git status
 3On branch main
 4Your branch is up to date with 'origin/main'.
 5
 6Changes to be committed:
 7  (use "git restore --staged <file>..." to unstage)
 8	modified:   example.txt
 9
10> git unadd
11Unstaged changes after reset:
12M	example.txt
13> git add example.txt
14> git commit -m "add example.txt"
15[main 1eb1b2f] add example.txt
16 1 file changed, 2 insertions(+), 0 deletions(-)
17 create mode 100644 example.txt
18> git uncommit
19> git unadd
20Unstaged changes after reset:
21M	example.txt

#Git  

Reply to this post by email ↪