DEV Community

Kruti Bhagat
Kruti Bhagat

Posted on

Git Cheatsheet

This cheatsheet lists commands we use to submit a PR (pull request) to a GH (github) repo. It is mainly for my reference, but if you found it helpful, leave a like!

git checkout -b *branch_name* Creates and switches to the new branch

git checkout - switch to the previous branch

git status to check files that have been changed

git diff shows the code difference in the terminal

git add . to stage all files for commit

git commit -m "*commit message goes here*" to commit the changes

You can use git commit and then you’ll be redirected to either nano or vi editors to enter the commit message.

For nano editor: Type message, ^X to exit the file and y to save the file.

For vi editor: i for insert mode, type the message and then ESC. :wq to save text and exit, or :q to simply exit

git push --set-upstream origin *branch_name* to push the branch and changes. git push works too, but you have to mention the branch_name the first time you push changes.

Revert commits before they are pushed to GH repo:

  1. Soft Reset – Changes stay, commit is reverted

    • git log (Shows commit log. We find the previous commit hash with it.)
    • git reset --soft ***previous commit hash***
    • git reset —soft HEAD^1
  2. Hard Reset – Changes and commit both are reverted

    • git reset --hard ***previous commit hash***
    • git reset —hard HEAD^1

Revert commits after they are pushed to the GH repo:

git revert ***commit hash*** to revert the commit that was pushed. This will revert the commit locally. This should be followed by git push so that the changes are reverted on GH repo.

git bisect checks what broke between specific commits and narrows down the search between a commit range.

Reset the local branch to match the remote branch:

  • git fetch origin fetches the latest changes from the remote branch
  • git reset --hard origin/*branch_name* resets local branch
  • git clean -f -d to delete untracked files
  • git pull to pull all the contents from the repo

Top comments (0)