Developed in 2005 by Linus Torvalds to manage Linux kernel development, Git has since become a popular version control system to manage and track code changes in source and other files. Git allows developers to track code changes, revert code to earlier versions, and collaborate with others on a shared codebase.
All functions of Git are command-based, which means you have to use specific commands to start a new project, set up a user email, or do similar tasks. Below, we present a Git cheat sheet you can use to familiarize yourself with the basics of using Git.
What is a Git cheat sheet? 📝
A Git cheat sheet is a reference guide that contains a list of commonly used commands for Git configurations. The cheat sheet can help developers save time when setting up and using Git. These commands may include:
- Setting up the name
- Setting the default editor
- Making local changes
- Managing files
- Creating branches
- Merging and rebasing
- Pushing to remote repositories
- Making a local copy
- Creating a local repository
What are Git commands?
The most basic command in Git is to start a project. Then, you can make changes to your project, clone a repository, merge different versions, or rebase branches. Here are some common types of commands for Git repositories:
- Clone: Cloning allows you to make a local copy of a remote repository.
- Start a Project: You use this command whenever you need to start a new project. You can make a copy of the server repository or create a local repository.
- Local Changes: Adding a file to the index area is the most common local change you make while using Git. Other changes are recording files permanently in version history and adding new branches.
- Branches: You can create multiple repository versions with the same file content but different histories. Each version is called a branch.
- Merge and Rebase: Merging combines versions of the same files in a repository. Meanwhile, rebasing is used to move a branch onto a new base commit.
Git cheat sheet ⬇️
Here's a cheat sheet that covers all major Git commands.
Git configuration defines a user name, email address, and preferences for committing or merging. Configuration can be done on four levels: system, global, local, and application. Here are the commands:
- Setting the name: $ git config --global user.name "Name"
- Setting the email: $ git config --global user. email "firstname.lastname@example.org"
- Setting the default editor: $ git config --global core.editor Vim
- Checking the settings: $ git config -list
You can also set up an alias for every command. Simply add '$ git config --global alias.co' before the function you want to perform. For example, the alias for committing is $ git config --global alias.ci. Other examples include:
- $ git config --global alias.co checkout
- $ git config --global alias.br branch
- $ git config --global alias.st status
Starting a project 🏁
When you start a project, you have to choose if you want to initialize it or clone an existing repository. Initializing a new project means creating a .git directory and setting up all the configuration files. Cloning will take an existing repository and make a local one.
- Local repository: $ git init
- Git clone: $ git clone
Making local changes 🖥️
Here are the commands for making local changes to your project:
- Adding a file: $ git add Filename
- Adding all files to the staging area: $ git add*
- Recording a file permanently in version history: $ git commit -m " Commit Message"
Tracking changes 🔎
You can also track changes in Git that have not yet been staged by using $ git diff. Here's how to track other changes:
- Changes that haven't been committed: $ git diff --staged
- Changes after a file is committed: $ git diff HEAD
- Changes between two commits: $ git diff
- Changes between two branches: $ git diff < branch 2>
Checking status ✅
These commands let you check the state of your working directory:
- Git status: $ git status
- Show objects: $ git show
Checking commit history 👀
A “commit” in Git is a snapshot of your project. Here are the commands to check its history:
- Displaying recent commits and head status: $ git log
- Displaying modified files: $ git log -stat
- Displaying modified files with location: $ git log -p
- Displaying modifications on each line of a file: $ git blame <file name>
Every branch in Git is a different version of your project. You can use these commands to isolate work in branches, integrate changes, or change context.
- Listing branches: $ git branch
- Creating a new branch: $ git branch [branch-name]
- Switching to another branch: $ git checkout
- Merging a branch's history into the current one: $ git merge [branch]
- Showing all commits in current branch’s history: $ git log
The commands for merging are as follows:
- Merging branches: $ git merge
- Apply commits from a distinct branch to a final commit: $ git rebase
Pulling updates 📥
Here's how to pull changes from a remote repository:
- Pulling data from the server: $ git pull origin master
- Pulling a remote branch: $ git pull
- Fetching all branches: $ git fetch -all
Undoing changes 🔙
Use these commands to undo changes in Git:
- Undoing changes: $ git revert
- Reverting a specific commit: $ git revert
- Resetting changes: $ git reset -hard
Removing files 🗑️
Use $ git rm <file Name> to remove a file from the index and the working tree. But if you want to remove the file from Git and keep it in your local repository, use $ git rm --cached.
Frequently asked questions
Here are some questions related to the Git cheat sheet markdown.
How to use Git step by step?
The process for using Git is fairly simple. First, you'll need to create an account and log in. Then, you can create a project/repository, add files to it, and use commands like git init, git commits, git push, and so on. You can learn more here.
How can one use a Git cheat sheet with an example?
Suppose you want to commit some changes in a file. You may want to see recent commits. Then, you'll have to find their location. Instead of doing this manually, you can use the Git cheat sheet and speed up the process.
What is GitHub vs Git?
Git is the version control system to track and manage your source code history, whereas GitHub is a hosting service where you can manage your Git repositories. If you need a cheat sheet for GitHub commands, you can find it here.
What is GitLab vs Git?
GitLab is a web-based repository manager that brings additional features to Git, such as issue tracking, code review, continuous integration/continuous delivery (CI/CD) pipelines, etc. Like Git, you also use commands to perform tasks on GitLab. Find the cheat sheet here.
Merge your code automatically 🚀
The more processes you can automate, the simpler your development process will get. With Mergify, you can automate code merging by writing specific trigger rules. It helps ensure your team is always on the same page, and that no code gets lost in the shuffle. Plus, the code merging process becomes more frequent and frictionless.