Risan Bagja

snippet··2 min read

Basic Git Commands

A cheatsheet of the Git commands I use day to day, from SSH key setup and branching to tagging, reverting and cherry-picking.

Here are the Git commands that I use most often.

Setup SSH Key

  1. List all available SSH keys on the computer
Terminal window
ls -al ~/.ssh
  1. If none found, you can generate one with (replace the email with your own)
Terminal window
ssh-keygen -t rsa -b 4096 -C "you@example.com"
  1. Run SSH agent on the background
Terminal window
eval "$(ssh-agent -s)"
  1. Add SSH key to SSH agent, so you don’t have to type the passphrase over and over again
Terminal window
ssh-add ~/.ssh/id_rsa
  1. Copy SSH key to the clipboard
Terminal window
pbcopy < ~/.ssh/id_rsa.pub
# Or
cat ~/.ssh/id_rsa.pub

Basic Branching

  1. Create a new branch
Terminal window
git checkout -b branch_name
  1. Make the changes and push to the origin
Terminal window
git push -u origin branch_name
  1. If you want to pull changes from master to branch
Terminal window
git rebase master
  1. Make the changes and switch back to the master
Terminal window
git checkout master
  1. Merge the changes
Terminal window
git merge branch_name
  1. Delete the branch
Terminal window
git branch -d branch_name
  1. Delete the remote branch
Terminal window
git push origin --delete branch_name

Tagging

Terminal window
# Create a tag
git tag -a v1.0 -m "Stable version 1.0"
git tag -a v1.0.0 -m ":bookmark: First stable version 1.0.0"
git tag -a v1.0.1 -m ":bookmark: Tag patch version 1.0.1"
git tag -s v1.0.0 -m ":bookmark: First stable version 1.0.0"
git tag -s v1.0.1 -m ":bookmark: Patch version 1.0.1"
# Push all tags
git push origin --tags
# Push specific tag
git push origin v1.0

Ignore Already Tracked Folders or Files

  1. Add file / folder to the .gitignore file
  2. Then run the following commands:
Terminal window
git rm -r --cached PATH/TO/FOLDER
git add .
git commit -m "Remove untracked files."

Revert to Previous Commit, Delete Any Uncommitted Changes

Run the following command to revert back to the last commit. Where HEAD is the last commit in current branch.

Terminal window
git reset --hard HEAD
git clean -fd

Revert to Previous Commit

We have made our changes and committed them, here’s the command to revert back to the specified commit:

Terminal window
git revert <previous-commit-sha1>

If we want to go back to one or more previous commit without creating any revert commit, just use this command:

Terminal window
git reset --hard <previous-commit-sha1>

Edit Previous Commit Message

Terminal window
git commit --amend -m "New commit message"

Use Emoji with Commit Message

Terminal window
git commit -m ":emoji: your message."

See Commit History

List the commit history

Terminal window
git log

Also see the changes

Terminal window
git log -p

Limit the list to the last two items

Terminal window
git log -p -2

Merge Specific Commit

Use this command to merge specific commit. You can use git log command to get the SHA

Terminal window
git cherry-pick 62ecb3

Commit and Set the Date

Terminal window
GIT_AUTHOR_DATE="Aug 10 08:12 2018 +0200" git commit -m "message"

Update Previous Push Commit Date

Terminal window
git commit --amend --date='Aug 10 08:12 2018 +0200' -C HEAD