Your Git Cheat-sheet — Commands to Remember

Rochelle Abeywickrama
6 min readMay 29, 2023

These are most common commands used nowadays but yet we forget. Here’s a cheat-sheet you can simply bookmark :) Hope it will be useful.

Setup:

Setting up the user configuration that will be utilized by all local repositories.

# display default configuration
$ git config --list


# set a name that is identifiable for credit when review version history
$ git config --global user.name "username"


# set an email address that will be associated with each history marker
$ git config --global user.email "emailaddress@xyz.com"


# check configured username and email
$ git config user.name
$ git config user.email


# set automatic command line coloring for Git for easy reviewing
$ git config --global color.ui auto

Initiate a repository:

# initialize an existing directory as a Git repository
$ git init


# retrieve an entire repository from a hosted location via URL
$ git clone [url]

Note: To get the repository URL, go to the git repo → click on ‘Code’ button → copy the clone link as highlighted below.

Stage your files:

Utilizing the Git staging area and snapshots

# Show modified files in working directory, staged for your next commit
git status


# Add a file as it looks now to your next commit (stage)
git add [file path]


# If you need to add ALL the modified files at once
git add .


# Unstage a file while retaining the changes in working directory
$ git reset [file]


# Difference of what is changed but not staged
$ git diff


# Difference of what is staged but not yet commited
$ git diff --staged


# Commit your staged content as a new commit snapshot
$ git commit -m "descriptive message"


# Add files and Commit your staged content as a new commit snapshot
$ git commit -a

Manage branch & merge:

Branching out work, changing the context, and incorporating changes

# list your branches. a * will appear next to the currently active branch
$ git branch


# create a new branch at the current commit
$ git branch [branch-name]


# switch to another branch and check it out into your working directory
$ git checkout


# One line command to checkout a new branch
$ git checkout -b [branch-name]


# merge the specified branch’s history into the current one
$ git merge [branch]


# show all commits in the current branch’s history
$ git log


# Git branch rename
$ git branch -m <new_branch_name>


# Delete branch
$ git branch -d [branch name]

Inspect branch & compare

Reviewing logs, diffs, and object data

# Show the commit history for the currently active branch
$ git log


# Show the commits on branchA that are not on branchB
$ git log branchB..branchA


# Show the commits that changed file, even across renames
$ git log --follow [file]


# Show the diff of what is in branchA that is not in branchB
$ git diff branchB...branchA


# Show any object in Git in human-readable format
$ git show [SHA]
$ git show [commit]

# used to give tags to the specified commit.
$ git tag [commitID]

Share & Update:

Updating local repositories and obtaining updates from a different repository

# add a git URL as an alias
$ git remote add [alias] [url]


# fetch down all the branches from that Git remote
$ git fetch [alias]


# merge a remote branch into your current branch to bring it up to date
$ git merge [alias]/[branch]


# Transmit local branch commits to the remote repository branch
$ git push [alias] [branch]


# Push commits to all branches in your repository
$ git push –all [variable name]


# fetch and merge any commits from the tracking remote branch
$ git pull

Tracking path changes

Removing files and changing paths in versions

# delete the file from project and stage the removal for commit
$ git rm [file]


# change an existing file path and stage the move
$ git mv [existing-path] [new-path]


# show all commit logs with indication of any paths that moved TEMPO
$ git log --stat -M

Rewrite history

Modifying commits, rewriting branches, and erasing history

# apply any commits of current branch ahead of specified one
$ git rebase [branch]


# clear staging area, rewrite working tree from specified commit
$ git reset --hard [commit]

Temporary Commits

Store modified, tracked files in a temporary location before switching branches.

# Save modified and staged changes
$ git stash


# list stack-order of stashed file changes
$ git stash list


# write working from top of stash stack
$ git stash pop


# discard the changes from top of stash stack
$ git stash drop

Ignoring patterns

Avoiding inadvertent file staging or committing

# system wide ignore patern for all local repositories
$ git config --global core.excludesfile [file]

Generating a new SSH key and adding it to the ssh-agent

  1. Open Git Bash.
  2. Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

3. This creates a new ssh key, using the provided email as a label.

> Generating public/private rsa key pair.

4. When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]

5. At the prompt, type a secure passphrase.

> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]

> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]

Adding your SSH key to the SSH-agent

  1. Type the following in Git Bash.
# start the ssh-agent in the background 
$ eval $(ssh-agent -s)
> Agent pid 59566

2. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

$ ssh-add ~/.ssh/id_rsa

Adding the SSH key to Github account

  1. Copy the SSH key to your clipboard.
$ clip < ~/.ssh/id_rsa.pub 
# Copies the contents of the id_rsa.pub file to your clipboard

Tip: If clip isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor and copy it to your clipboard.

2. In the upper-right corner of any page, click your profile photo, then click Settings.

3. In the user settings sidebar, click SSH and GPG keys.

4. Click New SSH key or Add SSH key.

5. In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.

6. Paste your key into the “Key” field.

7. Click Add SSH key.

8. If prompted, confirm your GitHub password.

Fixing issues with gitignore not ignoring files:

Fix to resolve issues with gitignore not ignoring files:

# Make changes in .gitignore file.


# Remove cached records / files
$ git rm -r --cached .

# Add all the changes
$ git add .

# Commit all changes
$ git commit -m "Commit message"

I hope this is helpful and see you all in another useful guide.

Happy Coding!!

--

--

Rochelle Abeywickrama

Associate Lead Quality Engineering at SyscoLABS | MSc in IT | BSc in IT | ISTQB — CTFL | ISTQB — CTAL (ATM) | SAFe Agilist, Certified Scrum Master