Skip to content

Commit 4236139

Browse files
committed
added: New Blog - Git Undo and revert
1 parent 6199a6f commit 4236139

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: Git Undo and Revert
3+
author: alkaison
4+
date: 2023-02-12 16:00:00 +0530
5+
categories: [Blogging, Git & GitHub]
6+
tags: [git, github, git undo, git revert]
7+
---
8+
9+
- 'revert’ is the command we use when we want to take a previous commit and add it as a new commit, keeping the log intact.
10+
11+
- First thing, we need to find the point we want to return to. To do that, we need to go through the log. To avoid the very long log list, use the --oneline option which gives just one line per commit showing.
12+
13+
1. The first seven characters of the commit hash.
14+
2. The commit message.
15+
16+
```terminal
17+
git log --oneline
18+
```
19+
20+
### Git Revert HEAD
21+
22+
- We can revert the latest commit using ‘git revert HEAD’ (revert the latest change, and then commit). By adding the option --no-edit, we can skip the commit message editor (getting the default revert message).
23+
24+
```terminal
25+
git revert HEAD --no-edit
26+
```
27+
28+
### Git Revert to any commit
29+
30+
- To revert to earlier commits, use ‘git revert HEAD~x’ (x being a number. 1 going back one more, 2 going back two more, etc.)
31+
32+
- Command to go back by one commit
33+
34+
```terminal
35+
git revert HEAD~1
36+
```
37+
38+
### Git Reset
39+
40+
- ‘reset’ is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit.
41+
42+
- First, get the seven characters of the commit hash from the log for the commit that you want to go back for.
43+
44+
- Then we reset our repository back to that specific commit using ‘git reset commithash’ (commithash being the first 7 characters of the commit hash we found in the log).
45+
46+
```terminal
47+
git reset 6199a6f
48+
```
49+
50+
### Git Undo Reset
51+
52+
- Even though the commits are no longer showing up in the log, it is not removed from Git. If we know the commit hash, we can reset to it using
53+
54+
```terminal
55+
git reset <commithash>
56+
```
57+
58+
### Git Amend
59+
60+
- 'commit --amend' is used to modify the most recent commit. It combines changes in the staging environment with the latest commit, and creates a new commit. This new commit replaces the latest commit entirely.
61+
62+
- One of the simplest things you can do with --amend is to change a commit message
63+
64+
```terminal
65+
git commit --amend -m "Commit message here"
66+
```
67+
68+
- Using this, the previous is replaced with our amended one.
69+
70+
#### Git Amend Files
71+
72+
- Adding files with '--amend' works the same way as above. Just add them to the staging environment before committing.

0 commit comments

Comments
 (0)