Git commands cheatsheets
Authored By : tdnshah
Published On
Tuesday, Dec 13, 2022
Git Reset
Git reset is a command used to revert or undo your commit to a specific commit specified to the command.
Command
Option 1: git reset {commit hash}
(Note: This command is defaulted to --soft)
Option 2: git reset --hard {commit hash}
Option 3: git reset --soft {commit hash}
Git Revert
Git revert basically created a new commit with the changes you want to undo, this helps in maintaining the git histroy.
Command
Option 1: git revert {commit hash}
Git Squash
Git Squash is a terminology and not a command, basically when we want to combined “N” number of continous commits into a single commit we can use squash commits using git rebase.
To know more on git rebase
Command
Option 1: git revert {commit hash}
Git Rebase
Command
Option 1: git rebase {commit hash}
Option 2: git rebase -i {commit hash}
How to create a branch from a pull request or a merge request
Overview
You may have came to a situation where in you want to get the code/changes in a particular PR to local environment, but as PR or a MR is not a branch on your repository how can you pull a PR or MR, the below command can be used to pull the code from a PR and create a new branch in your local env.
Command
git fetch origin pull/$ID/head:$BRANCHNAME
For example: git fetch origin pull/2/head:MASTER
Note: Here ID is the PR number for github users and MR number for gitlab users.
git checkout $BRANCHNAME
Please give us your feedback on our articles and if you find them helpful, click the thumbs up icon to motivate us to write more.