Skip to content
Snippets Groups Projects
Verified Commit 69226386 authored by Laurent Heirendt's avatar Laurent Heirendt :airplane:
Browse files

adaptations to reset

parent b25fcf3e
No related branches found
No related tags found
No related merge requests found
# Theory: Git Reset
# Reset a branch
* Enables to reset back to a previous commit HEAD
* Enables to reset back to a previous commit
* Discards ALL commits made after the selected commit HEAD
* This happens often in **practice**:
you pushed to a branch, then realize that you made a mistake in the commit, and want to start over.
# Practical: Git Reset
# Example: Hard reset of a branch (1)
* Start by committing two files:
```bash
$ echo "This file is the one we want to keep" > header.txt
$ git add header.txt
$ git commit -m "Reset here"
```
```bash
$ echo "This file contains faulty code" > undo.txt
$ git add undo.txt
$ git commit -m "Undo this commit"
$ cd attendees
$ echo "# CV of Firstname Lastname" > myCV.md
$ git add myCV.md
$ git commit -m "add cv for Firstname Lastname"
$ echo "# Biography of Firstname Lastname" > myBio.md
$ git add myBio.md
$ git commit -m "add biography for Firstname Lastname"
$ git push origin myBranch
```
* Check the commits again and copy the relevant commit header:
* Check the commits, copy the `SHA1` of the second last commit:
```bash
$ git log
```
# Example: Hard reset of a branch (2)
* Use the `reset --hard` command in order to undo the faulty commit:
```bash
$ git reset --hard <sha1>
$ git reset --hard <SHA1>
```
* Check what happened in the log
* Force push your now solitary commit to develop
\ No newline at end of file
* Force push your branch (overwrite the history)
# Notes
Alternatively, you can also remove the last commit:
```bash
$ git reset --hard HEAD~1
```
With a `--hard` reset, the index and the working tree are reset.
If you omit the `--hard` flag, a mixed reset is made. This resets the index, but not the working tree
```bash
$ git reset HEAD~1
```
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment