debian-mirror-gitlab/doc/university/training/topics/rollback_commits.md

79 lines
1.3 KiB
Markdown
Raw Normal View History

2018-03-17 18:26:18 +05:30
---
comments: false
---
2017-08-17 22:00:37 +05:30
# Rollback Commits
## Undo Commits
2019-03-02 22:35:43 +05:30
- Undo last commit putting everything back into the staging area:
2018-11-20 20:47:30 +05:30
2019-09-04 21:01:54 +05:30
```sh
2018-11-20 20:47:30 +05:30
git reset --soft HEAD^
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- Add files and change message with:
2018-11-20 20:47:30 +05:30
2019-09-04 21:01:54 +05:30
```sh
2018-11-20 20:47:30 +05:30
git commit --amend -m "New Message"
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- Undo last and remove changes:
2018-11-20 20:47:30 +05:30
2019-09-04 21:01:54 +05:30
```sh
2018-11-20 20:47:30 +05:30
git reset --hard HEAD^
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- Same as last one but for two commits back:
2018-11-20 20:47:30 +05:30
2019-09-04 21:01:54 +05:30
```sh
2018-11-20 20:47:30 +05:30
git reset --hard HEAD^^
```
2017-08-17 22:00:37 +05:30
** Don't reset after pushing **
## Reset Workflow
1. Edit file again 'edit_this_file.rb'
2019-02-15 15:39:39 +05:30
1. Check status
1. Add and commit with wrong message
1. Check log
1. Amend commit
1. Check log
1. Soft reset
1. Check log
1. Pull for updates
1. Push changes
2017-08-17 22:00:37 +05:30
## Commands
2019-09-04 21:01:54 +05:30
```sh
2017-08-17 22:00:37 +05:30
# Change file edit_this_file.rb
git status
git commit -am "kjkfjkg"
git log
git commit --amend -m "New comment added"
git log
git reset --soft HEAD^
git log
git pull origin master
git push origin master
```
## Note
2019-03-02 22:35:43 +05:30
- git revert vs git reset
- Reset removes the commit while revert removes the changes but leaves the commit
- Revert is safer considering we can revert a revert
2017-08-17 22:00:37 +05:30
2019-09-04 21:01:54 +05:30
```sh
2017-08-17 22:00:37 +05:30
# Changed file
git commit -am "bug introduced"
git revert HEAD
# New commit created reverting changes
# Now we want to re apply the reverted commit
git log # take hash from the revert commit
git revert <rev commit hash>
# reverted commit is back (new commit created again)
```