git reset is a command that allows you to undo commits and change the state of your local repository. It can be used to unstage changes, discard commits, or move the branch pointer to a different commit.
There are three main options for the git reset command:
- git reset --mixed (or git reset): This option resets the branch pointer to a specified commit and un-stages any changes in the working directory. The changes will still be present in the working directory and can be recommitted or stashed. This is the default option.
- git reset --soft: This option resets the branch pointer to a specified commit, but keeps the changes in the staging area. This allows you to modify and recommit the changes in a new commit.
- git reset --hard: This option resets the branch pointer to a specified commit and discards all changes in the working directory and staging area. This is a dangerous option and should be used with caution, as it permanently deletes any uncommitted changes.
Here is an example of how to use git reset:
$ git log
commit abcdefg (HEAD -> master)
Author: John Doe
Date: Mon Jan 1 12:00:00 2021
Added new feature
commit hijklmn
Author: John Doe
Date: Sun Dec 31 12:00:00 2020
Fixed bug
$ git reset --mixed hijklmn
The above command will reset the master branch to the commit hijklmn and un-stage any changes. The new feature will not be in the master branch and any changes made to it will be lost.
It's important to note that git reset does not delete commits, it only moves the branch pointer to a different commit, so all commits will still be available in the repository, but they will be unreachable, this is why git reset is often used in conjuction with git push --force if the changes needs to be propagated to a remote repository.
Here is an example of how you might use the git reset --mixed option:
- You have made some changes to your local repository and have added and committed them:
$ git add file1.txt
$ git commit -m "Added new feature"
- After committing, you realize that there is a mistake in the changes and you want to undo the commit:
$ git log
commit abcdefg (HEAD -> master)
Author: John Doe
Date: Mon Jan 1 12:00:00 2021
Added new feature
commit hijklmn
Author: John Doe
Date: Sun Dec 31 12:00:00 2020
Fixed bug
- You can use git reset command to undo the commit and move the branch pointer back to the previous commit:
$ git reset --mixed hijklmn
This will reset the branch pointer to the commit hijklmn and un-stage any changes. The new feature will not be in the master branch and any changes made to it will be lost.
- You can verify that the branch pointer has been reset and the changes have been un-staged:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
- You can make changes again to the files and re-commit the changes with a new message.
It's important to note that git reset --mixed does not delete commits, it only moves the branch pointer to a different commit, so all commits will still be available in the repository, but they will be unreachable, this is why git reset is often used in conjuction with git push --force if the changes needs to be propagated to a remote repository.
git reset --soft is an option for the git reset command that allows you to undo commits while keeping the changes in the staging area. This option is useful when you want to modify and recommit the changes in a new commit.
Here is an example of how you might use the git reset --soft option:
- You have made some changes to your local repository and have added and committed them:
$ git add file1.txt
$ git commit -m "Added new feature"
- After committing, you realize that there is a mistake in the changes, but you want to keep the changes and make modifications before committing again.
$ git log
commit abcdefg (HEAD -> master)
Author: John Doe
Date: Mon Jan 1 12:00:00 2021
Added new feature
commit hijklmn
Author: John Doe
Date: Sun Dec 31 12:00:00 2020
Fixed bug
- You can use git reset command with --soft option to undo the commit and move the branch pointer back to the previous commit, but keeping the changes in the staging area:
$ git reset --soft hijklmn
This will reset the branch pointer to the commit hijklmn and keep the changes in the staging area.
- You can verify that the branch pointer has been reset and the changes are still in the staging area:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file1.txt
- You can now make the necessary modifications and commit the changes again with a new message.
It's important to note that git reset --soft does not delete commits, it only moves the branch pointer to a different commit, so all commits will still be available in the repository, but they will be unreachable, this is why git reset is often used in conjuction with git push --force if the changes needs to be propagated to a remote repository.
git reset --hard is an option for the git reset command that allows you to undo commits and discard any changes that were made in those commits. This option is useful when you want to completely undo changes and start over.
Here is an example of how you might use the git reset --hard option:
- You have made some changes to your local repository and have added and committed them:
$ git add file1.txt
$ git commit -m "Added new feature"
- After committing, you realize that there is a mistake in the changes, and you want to completely undo the changes and start over:
$ git log
commit abcdefg (HEAD -> master)
Author: John Doe
Date: Mon Jan 1 12:00:00 2021
Added new feature
commit hijklmn
Author: John Doe
Date: Sun Dec 31 12:00:00 2020
Fixed bug
- You can use git reset command with --hard option to undo the commit and discard any changes that were made in that commit:
$ git reset --hard hijklmn
This will reset the branch pointer to the commit hijklmn and discard any changes that were made in the commit abcdefg.
- You can verify that the branch pointer has been reset and the changes have been discarded:
$ git status
On branch master
nothing to commit, working tree clean
It's important to note that git reset --hard is a destructive command, it discards commits and changes made to the files, so it's important to be very careful while using this command. It's also important to note that this command will delete any commits that were made after the commit you are resetting to, so be sure you don't want those changes before you run this command. It's also important to note that git reset --hard does not delete commits, it only moves the branch pointer to a different commit, so all commits will still be available in the repository, but they will be unreachable, this is why git reset is often used in conjuction with git push --force if the changes needs to be propagated to a remote repository.