How to Undo Git Add
Sometimes, it is necessary to undo something while working in Git. Find your fast solution for undoing git add in this snippet.
At any stage of your work, you may like to undo something. You have that opportunity with Git. Here, we will show you the basic techniques of undoing an unwanted add in Git.
How to undo git add before commit
While working in Git, the files to be committed are gathered in the staging area. Now, suppose that you have added an unnecessary file to the staging area. Below, you will see how to remove that file easily. To remove only a single file from the staging area, you should run the command below:
undo a single file git
git reset <file-name>To remove all the files from the staging area, run the following command:
undo all changes git
git resetNote that git reset without the --hard flag only unstages files and does not discard your changes. To permanently discard changes, you would need git reset --hard, which is an irreversible operation.
As you see, git reset is used for undoing git add. Running only <kbd class="highlighted">git reset</kbd> without specifying the file name is especially useful when you are unable to list all the existing files one by one. Alternatively, in Git 2.23+, you can use git restore --staged <file-name> to unstage a single file.
How to Use git amend to Add Files to the Commit
There are cases when you commit something forgetting to add a file or make some changes. Git provides the following solution to the problems mentioned above. To solve the issue, first of all, you need to add your changes to the staging area with the <kbd class="highlighted">git add</kbd> command and, then, call git commit with the <kbd class="highlighted">--amend</kbd> option as follows:
undo unstaged changes
git commit --amendIn other words, when you commit but then remember that you have not added some changes, you may act as follows:
undo unstaged changes
git commit -m 'initial commit'
git add forgotten_file
git commit --amendNote that git commit --amend rewrites the last commit's history. Avoid using it on commits that have already been pushed to a shared repository.
The git commit Command
The <kbd class="highlighted">git commit</kbd> command can be related to the most important git commands. It is used for saving overall currently staged changes. You can create commits for capturing the current position of your project. Git always asks before changing the committed snapshots, hence they can be considered safe versions of the project. The <kbd class="highlighted">git add</kbd> command is used before <kbd class="highlighted">git commit</kbd> for making changes to the project which is going to be stored in a commit.
The git reset Command
Whenever you need to undo changes, the <kbd class="highlighted">git reset</kbd> command will come to help you. It runs on the so-called “three states of Git”. It involves the commit history (<kbd class="highlighted">HEAD</kbd>), the working directory and the staging index. This tool is useful when you have not pushed your commit to the remote repository yet.