A Comprehensive Git Cheat Sheet for Software Developers

As a software engineer, one of the essential tools you’ll use throughout your career is Git. Git is a distributed version control system that allows you to track changes in your codebase, collaborate with team members, and manage your project’s history efficiently. To help you navigate Git’s vast array of commands and options, we’ve put together a comprehensive Git cheat sheet. Whether you’re just starting with Git or looking to sharpen your skills, this cheat sheet will serve as a handy reference.
Git Configuration
01 Git Configuration
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global color.ui auto
This command sets the name that will be attached to your commits and tags.
git init [project name]
This command creates a new local repository in the current directory. If you provide a project name, Git will create a new directory with that name and initialize a repository inside it.
git clone <project url>
Downloads a project with the entire history from the remote repository.
git rm [file]
Remove a file from the working directory and staging area.
Storing Your Work
04 Storing Your Work
git stash
Puts your current changes in the working directory into a stash for later use.
git stash pop
Applies the stored stash content into the working directory and clears the stash.
git stash drop
Deletes a specific stash from all your previous stashes.
Git Branching Model
05 Git Branching Model
git branch [-a]
Lists all local branches in the repository. With the -a
option, it shows all branches (including remote).
git branch [branch_name]
Creates a new branch, referencing the current HEAD.
git rebase [branch_name]
Applies commits from the current working branch onto the specified [branch]
to make the branch history more linear.
git checkout [-b] [branch_name]
Switches the working directory to the specified branch. With the -b
option, Git will create the specified branch if it does not exist.
git merge [branch_name]
Joins the specified [branch_name]
branch into your current branch.
git branch -d [branch_name]
Removes a selected branch if it is already merged into another branch. Use -D
instead of -d
to force deletion.
Day-to-Day Work
03 Day-to-Day Work
git status
Displays the status of your working directory, including new, staged, and modified files. It also shows the branch name, current commit identifier, and pending changes for the next commit.
git add [file]
Adds a file to the staging area. Use .
in place of the full file path to add all changed files from the current directory down into the directory tree.
git diff [file]
Shows changes between the working directory and the staging area.
git diff --staged [file]
Shows changes between the staging area and the repository.
git checkout -- [file]
Discards changes in the working directory. Be cautious, as this operation is unrecoverable.
git reset <path>...
Reverts some paths in the index (or the whole index) to their state in HEAD
.
git commit
Creates a new commit from changes added to the staging area. Ensure you provide a commit message!
Ignoring Files
11 Ignoring Files
cat <<EOF > .gitignore
/logs
*
!/logs/.gitkeep
/tmp
*.swp
EOF
To ignore files, create a .gitignore
file in your repository with a line for each pattern. File ignoring will work for the current and subdirectories where the .gitignore
file is placed. In this example, all files are ignored in the logs
directory (excluding the .gitkeep
file), the entire tmp
directory, and all files with the .swp
extension.
Git Installation
10 Git Installation
For GNU/Linux distributions, Git should be available in the standard system repository. For example, in Debian/Ubuntu, you can install Git using the following command:
sudo apt-get install git
If you need to install Git from source, you can download it from git-scm.com/downloads.
Synchronizing Repositories
09 Synchronizing Repositories
git fetch [remote]
Fetches changes from the remote repository but does not update tracking branches.
git fetch --prune [remote]
Deletes remote references that were removed from the remote repository.
git pull [remote]
Fetches changes from the remote and merges the current branch with its upstream.
git push [--tags] [remote]
Pushes local changes to the remote repository. Use --tags
to push tags.
git push -u [remote] [branch]
Pushes a local branch to the remote repository and sets it as an upstream branch.
Inspect History
06 Inspect History
git log [-n count]
Lists the commit history of the current branch. Use -n count
to limit the list to the last n
commits.
git log --oneline --graph --decorate
Provides an overview with reference labels and a history graph, displaying one commit per line.
git log ref..
Lists commits that are present on the current branch but not merged into ref
. ref
can be a branch name or a tag name.
git log ..ref
Lists commits that are present on ref
but not merged into the current branch.
git reflog
Lists operations (e.g., checkouts or commits) made on the local repository.
Tagging Commits
07 Tagging Commits
git tag
Lists all tags.
git tag [name] [commit sha]
Creates a tag reference named name
for the current commit. You can specify a commit sha to tag a specific commit instead of the current one.
git tag -a [name] [commit sha]
Creates a tag object named name
for the current commit.
git tag -d [name]
Removes a tag from the local repository.
Reverting Changes
08 Reverting Changes
git reset [--hard] [target reference]
Switches the current branch to the target reference, leaving differences as uncommitted changes. When --hard
is used, all changes are discarded, so be careful not to lose uncommitted changes.
git revert [commit sha]
Creates a new commit that reverts changes from the specified commit. It generates an inversion of changes.
In conclusion, Git is a powerful tool for software developers, and mastering its commands is crucial for effective version control and collaboration. This Git cheat sheet should serve as a handy reference as you navigate your way through your software development projects. Feel free to bookmark it and refer back to it whenever you need a quick reminder of Git’s essential commands and workflows. Happy coding!