rollback_commits.md 1.32 KB
Newer Older
1 2 3 4
---
comments: false
---

Sean Packham's avatar
Sean Packham committed
5 6 7 8
# Rollback Commits

## Undo Commits

9
- Undo last commit putting everything back into the staging area:
10

11 12 13
  ```sh
  git reset --soft HEAD^
  ```
Sean Packham's avatar
Sean Packham committed
14

15
- Add files and change message with:
16

17 18 19
  ```sh
  git commit --amend -m "New Message"
  ```
Sean Packham's avatar
Sean Packham committed
20

21
- Undo last and remove changes:
22

23 24 25
  ```sh
  git reset --hard HEAD^
  ```
Sean Packham's avatar
Sean Packham committed
26

27
- Same as last one but for two commits back:
28

29 30 31
  ```sh
  git reset --hard HEAD^^
  ```
Sean Packham's avatar
Sean Packham committed
32

33
**Don't reset after pushing**
Sean Packham's avatar
Sean Packham committed
34 35 36 37

## Reset Workflow

1. Edit file again 'edit_this_file.rb'
38 39 40 41 42 43 44 45 46
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
Sean Packham's avatar
Sean Packham committed
47 48 49

## Commands

50
```sh
Sean Packham's avatar
Sean Packham committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
# 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

65 66 67
- 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
Sean Packham's avatar
Sean Packham committed
68

69
```sh
Sean Packham's avatar
Sean Packham committed
70 71 72 73 74 75 76 77 78
# 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)
```