Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
courses
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nene Barry
courses
Commits
69226386
Verified
Commit
69226386
authored
5 years ago
by
Laurent Heirendt
Browse files
Options
Downloads
Patches
Plain Diff
adaptations to reset
parent
b25fcf3e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
2019/2019-09-24_advancedGitTraining/slides/reset.md
+39
-14
39 additions, 14 deletions
2019/2019-09-24_advancedGitTraining/slides/reset.md
with
39 additions
and
14 deletions
2019/2019-09-24_advancedGitTraining/slides/reset.md
+
39
−
14
View file @
69226386
#
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
<
sha
1>
$
git reset
--hard
<
SHA
1>
```
*
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment