How to Undo Git Add
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:
git reset <file-name>
To remove all the files from the staging area, run the following command:
git reset
As you see, git reset is used for undoing git add. Running only git reset without specifying the file name is especially useful when you are unable to list all the existing files one by one.
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 git add command and, then, call git commit with the --amend option as follows::
git commit --amend
In other words, when you commit but then remember that you have not added some changes, you may act as follows:
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
The git commit Command
The git commit 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 git add command is used before git commit for making changes to the project which is going to be stocked in a commit.
The git reset Command
Whenever you need to undo changes, the git reset command will come to help you. It runs on the so-called “ The Three Trees of Git”. It involves the commit history (HEAD), the working directory and the staging index. This tool is useful when you have not pushed your commit to the remote repository yet.