Search This Blog

Labels

adobe (1) android (3) apache (3) aviation (1) bash (2) chrome (2) composer (1) cookery (3) dev (2) dodanperks (1) extensions (1) facebook (2) firefox (1) git (2) grafana (1) guzzle (1) headaches (11) htaccess (1) html5 (2) jquery (2) lamp (1) life hacks (10) linux (28) mysqli (2) native (1) opera (2) php (10) railfanning (1) reactjs (3) reactnative (1) servers (11) sinhala (1) smartphones (2) snap (1) sound (1) tech (22) troubleshoots (4) ubuntu (29) unicode (4) virtualbox (1) wamp (2) web (11) windows (4) wordpress (3) youtube (2) කෑම (3)

Monday, 20 February 2023

Reverse things in Git: Reset Vs Revert Vs Rebase

Git Reset:

Git Reset is a command used to undo changes to a repository by moving the branch pointer to a previous commit. This command can be used to unstage changes, discard changes, or even completely remove commits from a branch. Git Reset is a powerful command that should be used with caution, as it can alter a repository's history.



Git Hard Reset Vs Soft Reset:

Syntax:

  • git reset --hard d69950b completely discards changes and sets the repository to the specified commit. This means that any changes made since the specified commit will be lost permanently. This command is useful for completely undoing changes or rolling back to a previous commit.

    • git reset --hard without specifying a commit hash, it will reset to the last commit and if you’ve made changes after the last commit, they will be reset.

    • git reset --hard HEAD^ reset the HEAD to the previous commit (one commit back). If you check the log, the previous last commit is gone now.

  • git reset --soft d69950b does not discard changes, but it does unstage them. This means that the changes will still be present in the working directory and can be committed again later. This command is useful for preparing changes for a new commit, or for "splitting" a commit into multiple smaller commits.

Git Revert:

Git Revert is a command used to create a new commit that undoes the changes made by a previous commit. This command creates a new commit with the opposite changes of the reverted commit, allowing you to undo changes while keeping a record of the original commit.


Syntax:
git revert 59c86c9 where 59c86c9 is the commit hash. You can find it from git log --oneline

Git Rebase:

Git Rebase is a command used to integrate changes from one branch into another by rewriting the history of the target branch. This command is used to create a linear history, rather than a branching history, and is useful for keeping a clean and organized Git repository. Git Rebase can be used to merge changes from one branch into another or to update a branch with changes from another branch.

Syntax:
git rebase feature this is executed while on the master branch to incorporate commits that were in the feature branch to master and make the timeline linear.


In summary,

  1. Git Reset is used to undo changes to a repository by moving the branch pointer  Don't use this if you don't want to cover the track because this doesn’t leave a mark of what you were doing.

  2. Git Revert is used to create a new commit that undoes changes made by a previous commit USE THIS to revert a commit.

  3. Git Rebase is used to integrate changes from one branch into another by rewriting the history of the target branch


Credits 
Images: geekflare.com

No comments:

Post a Comment