Git Cheatsheet
Source: Dev.to
This cheatsheet lists the Git commands commonly used to submit a PR (pull request) to a GitHub repository. It’s mainly for reference.
Branch Management
git checkout -b # Create and switch to a new branch
git checkout - # Switch back to the previous branch
Checking Status
git status # Show changed files
git diff # Show code differences in the terminal
Staging and Committing
git add . # Stage all files for commit
git commit -m "commit message" # Commit with a message
git commit # Open editor (nano or vi) to enter the commit message
Editor shortcuts
- Nano: type the message,
Ctrl+Xto exit, thenyto save. - Vi: press
ifor insert mode, type the message,Esc, then:wqto save and exit (or:qto quit without saving).
Pushing
git push --set-upstream origin # Push a new branch and set upstream
git push # Subsequent pushes (branch name not needed)
Reverting Commits (before they are pushed)
Soft reset – keep changes, revert the commit
git log # Find the previous commit hash
git reset --soft
git reset --soft HEAD^1
Hard reset – discard changes and the commit
git reset --hard
git reset --hard HEAD^1
Reverting Commits (after they are pushed)
git revert # Create a revert commit locally
git push # Apply the revert on the remote repository
Bisect
git bisect # Binary search to identify which commit introduced a bug
Resetting Local Branch to Match Remote
git fetch origin # Fetch latest changes from remote
git reset --hard origin/ # Reset local branch to remote state
git clean -f -d # Delete untracked files and directories
git pull # Pull all contents from the repository