DEV Community

Cover image for Yet Another Git Cheatsheet
Dumb Down Demistifying Dev
Dumb Down Demistifying Dev

Posted on • Updated on

Yet Another Git Cheatsheet

image source https://acodez.in/gitlab-vs-github-vs-bitbucket/

What is Git

Git is the free and open source distributed version control system that is widely used by the world today. It helps in tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

What is Version Control

Version control keeps track of every modification to the code. If a mistake is made, developers can compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

How to use this cheatsheet

If ever comes the time where you'll need to refer to any of the Git commands or if you are unsure of the syntax to use, feel free to pop by this cheatsheet and hit 'Cmd + f' for Mac users or 'Ctl + f' for Windows users to look for any relevant details.

Other useful resource

Start a new Git Repo from scratch
Advance Git CheatSheet
Git
OhShitGit
Git: Cheat Sheet (advanced)

* Take Note: *

This cheatsheet if subjected to futher updates whenever there are new commands that I want to take note of so that I can revisit again in the future.


1. Git Basics

Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.

git init <directory>
Enter fullscreen mode Exit fullscreen mode

Clone repo located at <repo> onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.

git clone <repo>
Enter fullscreen mode Exit fullscreen mode

Stage all changes in <directory> for the next commit. Replace <directory> with a
<file> to change a specific file.

git add <directory>   # to add specific dir
git add .             # to add all that are not staged for the next commit
Enter fullscreen mode Exit fullscreen mode

It's a good practice to commit unstaged snapshots. Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.

git commit -m "<message>"
Enter fullscreen mode Exit fullscreen mode

Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.

git commit --amend
Enter fullscreen mode Exit fullscreen mode

List which files are staged, unstaged, and untracked.

git status
Enter fullscreen mode Exit fullscreen mode

2. Logging

Display the entire commit history using the default format. For customization see additional options.

git log
Enter fullscreen mode Exit fullscreen mode

Limit number of commits by <limit>. E.g. git log -5 will limit to 5 commits.

git log -<limit>
Enter fullscreen mode Exit fullscreen mode

Condense each commit to a single line.

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Display full diff of each commit.

git log -p
Enter fullscreen mode Exit fullscreen mode

Include which files were altered and the relative number of lines that were added or deleted from each of them.

git log -stat
Enter fullscreen mode Exit fullscreen mode

Search commits for a particular author

git log --author="<pattern>"
Enter fullscreen mode Exit fullscreen mode

Search for commits with a commit message that matches <pattern>.

git log --grep="<pattern>"
Enter fullscreen mode Exit fullscreen mode

Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.

git log <since>..<until>
Enter fullscreen mode Exit fullscreen mode

Only display commits that have the specified file.

git log -- <file>
Enter fullscreen mode Exit fullscreen mode

--graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.

git log --graph --decorate
Enter fullscreen mode Exit fullscreen mode

3. Checking Differences

Show unstaged changes between your index and working directory.

git diff
Enter fullscreen mode Exit fullscreen mode

Show difference between working directory and last commit.

git diff HEAD
Enter fullscreen mode Exit fullscreen mode

Show difference between staged changes and last commit.

git diff --cached
Enter fullscreen mode Exit fullscreen mode

4. Undoing Changes or Rewriting History

Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.

git revert <commit>
Enter fullscreen mode Exit fullscreen mode

Reset staging area to match most recent commit, but leave the working directory unchanged.

git reset
Enter fullscreen mode Exit fullscreen mode

Remove <file> from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.

git reset <file>
Enter fullscreen mode Exit fullscreen mode

Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory

git reset --hard
Enter fullscreen mode Exit fullscreen mode

Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone

git reset <commit>
Enter fullscreen mode Exit fullscreen mode

Resets both the staging area AND working directory to match. Deletes uncommitted changes, and all commits after <commit>

git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.

git clean -n
Enter fullscreen mode Exit fullscreen mode

Rebase the current branch onto <base>. <base> can be a commit ID, a branch name, a tag, or a relative reference to HEAD.

Git merge is always a forward moving change record. Alternatively, rebase has powerful history rewriting features.

git rebase <base>
Enter fullscreen mode Exit fullscreen mode

Interactively rebase current branch onto . Launches editor to entercommands for how each commit will be transferred to the new base

git rebase -i <base>
Enter fullscreen mode Exit fullscreen mode

Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.

git reflog
Enter fullscreen mode Exit fullscreen mode

4. Git branches

List all of the branches in your repo. Add a <branch> argument to create a new branch with the name <branch>

git branch
Enter fullscreen mode Exit fullscreen mode

Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch.

got checkout -b <branch>
Enter fullscreen mode Exit fullscreen mode

Merge <branch> into the current branch.

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

5. Remote Repositories

Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.

git remote add <name> <url>
Enter fullscreen mode Exit fullscreen mode

Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs.

git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.

git pull <remote>
Enter fullscreen mode Exit fullscreen mode

Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.

The rebase command integrates changes from one branch into another. It is an alternative to the better known "merge" command.
Most visibly, rebase differs from merge by rewriting the commit history in order to produce a straight, linear succession of commits.

git pull --rebase <remote>
Enter fullscreen mode Exit fullscreen mode

Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.

git push <remote> --force
Enter fullscreen mode Exit fullscreen mode

Push all of your local branches to the specified remote

git push <remote> --all
Enter fullscreen mode Exit fullscreen mode

Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo.

git push <remote> --tags
Enter fullscreen mode Exit fullscreen mode

6. Git Configurations

Define author name to be used for all commits in current repo. Developerss commonly use --global flag to set config options for current user.

git config user.name <name>
Enter fullscreen mode Exit fullscreen mode

Define the author name to be used for all commits by the current user

git config --global user.name <name>
Enter fullscreen mode Exit fullscreen mode

Define the author email to be used for all commits by the current user.

git config --global user.email <email>
Enter fullscreen mode Exit fullscreen mode

Create shortcut for a Git command.

git config --global alias. <alias-name> <git-command>
Enter fullscreen mode Exit fullscreen mode

Set text editor used by commands for all users on the machine. <editor> arg should be the command that launches the desired editor (e.g. vi).

git config --system core.editor <editor>
Enter fullscreen mode Exit fullscreen mode

Open the global configuration file in a text editor for manual editing.

git confg --global --edit
Enter fullscreen mode Exit fullscreen mode

7. Temporarily store modified, tracked files in order to change branches

Save modified and staged changes.

git stash
Enter fullscreen mode Exit fullscreen mode

list stack-order of stashed file changes.

git stash list
Enter fullscreen mode Exit fullscreen mode

Write working from top of stash stack.

git stash pop
Enter fullscreen mode Exit fullscreen mode

Discard the changes from top of stash stack

git stash drop
Enter fullscreen mode Exit fullscreen mode

8. Other Useful Git Commands

Show Git remote URL, or if your are not connected to a network that can reach the remote repo.

git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode

Show Git remote URL if you are on a network that can reach the remote repo where the origin resides.

git remote show origin
Enter fullscreen mode Exit fullscreen mode

Git push existing repo to a new and different remote repo server.

# Create a new repo at github.
# Copy the new Git repo URL.
git remote rename origin upstream
git remote add origin <NEW_GIT_REPO_URL>
git push origin master
Enter fullscreen mode Exit fullscreen mode

Top comments (7)

Collapse
 
xxzozaxx profile image
Ahmed Khaled

another useful tips
ohshitgit.com/

Collapse
 
henryong92 profile image
Dumb Down Demistifying Dev • Edited

Thank you!

Collapse
 
waylonwalker profile image
Waylon Walker

Yagc

Collapse
 
henryong92 profile image
Dumb Down Demistifying Dev

HAHA! Nice one 😂

Collapse
 
mindmadetech profile image
MindMade

Hey ,

Thanks dude for bringing out the good posts article.

Collapse
 
dsipvtltd profile image
Dreamsoft Infotech

I think it’s a very informative post for all the readers. Thank you for sharing your knowledge & work. We have another resource dreamsoftinfotech.com/

Collapse
 
ashcript profile image
As Manjaka Josvah

Very helpful 😋