It happens to everyone: you run git add . and accidentally commit test data, environment files, or proxy settings you didn't mean to include.
If you’ve made a commit with unwanted files but haven't pushed those changes to a remote repository yet, you can easily correct the mistake without losing any of your work locally.
This post will walk you through the safest way to remove an unwanted file from your most recent commit, ensuring only the necessary files are pushed.
The Safest Method: Soft Reset and Selective Recommit
The goal here is to undo the commit action while keeping all the file changes in your local working directory.
Prerequisites
You have not yet pushed the commit to a remote repository (origin, etc.).
You want to keep the changes from the unwanted files locally for later use, but exclude them from the current commit.
Step-by-Step Instructions
Follow these commands in your terminal:
Step 1: Soft reset the last commit.
This command moves your branch pointer back by one commit but keeps all the changes from that commit in your staging area.
bash
git reset --soft HEAD~1
Use code with caution.
Step 2: Unstage the unwanted file(s).
The files are currently staged (ready to be committed). Use git reset (in its three-argument form, which unstages files without changing the working directory) to remove specific files from the staging area.
bash
git reset HEAD path/to/unwanted-file.txt
Use code with caution.
You can repeat this command for multiple files or use wildcards if necessary. The changes for these files are now safely in your working directory, untracked by the next commit.
Step 3: Recommit only the correct files.
Now, stage only the files you intended to commit originally.
bash
git add path/to/correct-file.txt
Use code with caution.
Finally, create your new, clean commit.
bash
git commit -m "Your descriptive commit message"
Use code with caution.
The unwanted files' changes are still present in your working directory, but they are no longer part of your commit history.
Therefore, Quick summary:
bash
# 1. Undo the last commit, keep changes staged
git reset --soft HEAD~1
# 2. Unstage the specific files you didn't want in the commit
git reset HEAD path/to/unwanted-file.txt
# 3. Stage only the correct files
git add path/to/correct-file.txt
# 4. Create the clean commit
git commit -m "The correct commit message"
Type the following command to set Notepad as the default text editor for Git:
GIT command
git config --global core.editor notepad
This will set Notepad as the default text editor for all Git repositories on your system.
Now when you need to edit a commit message or any other text file in Git, Git will open the Notepad editor for you to make changes.
Note:
that Notepad may not be the best choice for a text editor for programming, as it lacks many features that are helpful for coding.
You may want to consider using a more advanced text editor, such as Visual Studio Code, Atom, Sublime Text, or Notepad++, depending on your preferences.
To use a different text editor with Git, just replace "notepad" with the name of your preferred text editor in the command above.