Saturday 6 June 2020

Git Undo Changes (Reset / Revert / Rebase)



Tracked Files:- are files that have been created within repo's working directory and have been added to the repository using git add command. 

Untracked Files:- are files that have been created within your repo's working directory but have not yet been added to the repository's tracking index using the git add command.

$ echo "tracked" > ./tracked_file
$ echo "untracked" > ./untracked_file
$ git add ./tracked_file
warning: LF will be replaced by CRLF in tracked_file.
The file will have its original line endings in your working directory

$ git status
On branch sprint-2
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   tracked_file

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        untracked_file
$ git log --oneline
138da36 (HEAD -> sprint-2) index3 file added
e8bb55f (origin/master, master) Initial folder structure created.
027265e add readme to initial commit

$ git reset 138da36

$ git log --oneline
138da36 (HEAD -> sprint-2) index3 file added
e8bb55f (origin/master, master) Initial folder structure created.
027265e add readme to initial commit

$ git reset e8bb55f

$ git log --oneline
e8bb55f (HEAD -> sprint-2, origin/master, master) Initial folder structure created.
027265e add readme to initial commit

$ ls
docs/  index3.html  readme.md  src/  test/  tracked_file  untracked_file

$ git status
On branch sprint-2
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index3.html
        tracked_file
        untracked_file

nothing added to commit but untracked files present (use "git add" to track)

Reset

$ git log --oneline
b764644 File with three lines
7c709f0 File with two lines
9ef9173 File with one line

$ git reset 9ef9173 (using an absolute commit SHA1 value 9ef9173)
or
$ git reset current~2 (using a relative value -2 before the "current" tag)

$ git log --oneline
9ef9173 File with one line


git reset --hard (This overwrites any local changes you haven't committed. In effect, it resets (clears out) the staging area and overwrites content in the working directory with the content from the commit you reset to. Before you use the hard option, be sure that's what you really want to do, since the command overwrites any uncommitted changes.)

Revert

The net effect of the git revert command is similar to reset, but its approach is different. Where the reset command moves the branch pointer back in the chain (typically) to "undo" changes, the revert command adds a new commit at the end of the chain to "cancel" changes.
$ git log --oneline
92eeac5 (HEAD -> sprint-2) added tracked_file
506d8ca added index3.html file.
e8bb55f (origin/master, master) Initial folder structure created.
027265e add readme to initial commit

admin@DESKTOP-M6VPONK MINGW64 /f/repo/my-repo (sprint-2)
$ git revert HEAD
Removing tracked_file
[sprint-2 71fa892] Revert "added tracked_file"
 1 file changed, 1 deletion(-)
 delete mode 100644 tracked_file

admin@DESKTOP-M6VPONK MINGW64 /f/repo/my-repo (sprint-2)
$ git log --oneline
71fa892 (HEAD -> sprint-2) Revert "added tracked_file"
92eeac5 added tracked_file
506d8ca added index3.html file.
e8bb55f (origin/master, master) Initial folder structure created.
027265e add readme to initial commit

Revert or reset?

Why would you choose to do a revert over a reset operation? If you have already pushed your chain of commits to the remote repository (where others may have pulled your code and started working with it), a revert is a nicer way to cancel out changes for them. This is because the Git workflow works well for picking up additional commits at the end of a branch, but it can be challenging if a set of commits is no longer seen in the chain when someone resets the branch pointer back.

Rebase

$ git log --oneline master
d0de868 (HEAD -> master) c5
e8bb55f (origin/master) Initial folder structure created.
027265e add readme to initial commit

$ git log --oneline sprint-2
9165990 (HEAD -> sprint-2) c4
df6bf21 c3
0374abc c2
764633c c1
71fa892 Revert "added tracked_file"
92eeac5 added tracked_file
506d8ca added index3.html file.
e8bb55f (origin/master) Initial folder structure created.
027265e add readme to initial commit

admin@DESKTOP-M6VPONK MINGW64 /f/repo/my-repo (sprint-2)
$ git rebase master
Successfully rebased and updated refs/heads/sprint-2.

admin@DESKTOP-M6VPONK MINGW64 /f/repo/my-repo (sprint-2)
$ git log --oneline master
d0de868 (master) c5
e8bb55f (origin/master) Initial folder structure created.
027265e add readme to initial commit

admin@DESKTOP-M6VPONK MINGW64 /f/repo/my-repo (sprint-2)
$ git log --oneline sprint-2
af3a9c7 (HEAD -> sprint-2) c4
88dbaba c3
4854135 c2
7017d9a c1
cc47ff2 Revert "added tracked_file"
8719ecf added tracked_file
5d0fe72 added index3.html file.
d0de868 (master) c5
e8bb55f (origin/master) Initial folder structure created.
027265e add readme to initial commit

No comments:

Post a Comment

Jenkins Startup and Configuration

Steps to setup jenkins on ubuntu:- -After installation. check the jenkins services running on not on the server. sudo service jenk...