Using Git Reset Without Fear
It’s your worst nightmare. Getting a little too confident, running a few git commands in quick succession, and suddenly… your meticulously coded contributions are gone. For most developers, using Git to add changes is second nature. Make a change, stage, commit, push, sip coffee, done. However, for some developers, using Git to remove changes brings paralyzing terror. It shouldn’t. Git reset allows us to unstage, uncommit, and, if we need to, discard changes forever. Here are five ways to git reset
.
1. Unstage Changes
git reset
If you’ve staged changes (git add
) that have not been committed to your local branch and you want to unstage those changes, this is your command. This command is not destructive — you will not lose any work.
2. Unstage Changes of a File
git reset ./relative-path/file-name.ts
Only want to unstage one file? First, figure out the relative path from the root of your Git repo to the file you’d like to unstage. If you’re using VSCode, right-click the file in the explorer sidebar and click “Copy relative path”. Make sure you replace the root directory of your repo with a period, like our example above. Then run the command from the root directory of your git repo and the changes in the specified file should now be…