DEV Community

Mosimi Akinlabi
Mosimi Akinlabi

Posted on

Git Commands: The Ultimate Cheatsheet

Git is a version control system in software development that mirrors a codebase, including its full history, on every developer's computer.

This is referred to as a distributed version control system. It is very important in software development because it is used to track changes in a set of computer files and to coordinate work among programmers who are working collaboratively on a source code to achieve a specific goal.

This cheat sheet contains commonly used and important Git commands for easy reference.

Prerequisites

You should have git installed on your computer, depending on the operating system of your machine(Windows, Linux or Mac). This should be done either through the command line or Git’s website. For more information on the step-by-step guide for installation, check here. Are you finished? Let’s proceed.

Getting and Creating Projects

Use the following commands to create a new Git repository or a copy of an existing one.

(a) git init
This is the first command you must learn if you want to keep your project under revision control. It's also worth noting that running this command in an existing project or repository is safe and will not overwrite any existing files. When you run this command, it will generate an empty Git repository.

(b) git clone
Cloning a repository is the process by which developers obtain a working copy of a main repository, making it easier to track changes and assign tasks to team members. When you clone a repository, it creates a local copy of the main repository on your machine.

Snapshotting

A snapshot is a copy of your project at a specific point in time. Assume you make backups by copying your entire project, zipping it, and adding a label and timestamp. That is a snapshot. Use the following commands to make a snapshot:

(a) git add[file-name.txt]
Before committing to the main repository, you can use this command to create a snapshot of your local repository. It is important to note that before running the commit command, you must add any new or modified files to the index using the add command.

(b) git add-A
This command adds to the staging area all new and changed files in the entire working tree.

(c) git status
This command should be used in conjunction with git add and git commit to see what will be included in the next snapshot. This command displays the working directory state as well as the staged snapshot.

(d) git diff
You can infer what a command does from its name, as you can with most git commands. Git diff displays the differences between commits, commits, and working trees.

(e) git commit
This command logs changes to the repository. It saves the staged snapshot of the project's history. When combined with git add, it depicts the day-to-day workflow of git users.

(f) git notes
This command is used to add, remove, or read notes attached to objects without having to touch the objects. Git notes are commonly used to supplement a commit message without altering the commit itself.

(g) git restore
This command restores working tree files. Another application for this command is to restore specified paths in the working tree with some content from a restore source. IT IS IMPORTANT TO NOTE THAT THIS COMMAND IS EXPERIMENTAL AND THE BEHAVIOR MAY CHANGE.

(h) git reset
This command returns the current HEAD to its original state. This means that it reverses changes made to files in the working directory. It can be used to clean up or remove changes that have not been pushed to a public repository.

(i) git rm
This command deletes files from the working directory.

(j) git mv
This command is used to move or rename a file or directory.

Branching and Merging

Branching and merging refers to the process of making a separate copy of a main codebase to incorporate a local change to it. Use the following commands to achieve this:

(a) git branch
This command can be used to list branches as well as create and delete them. It is simply a general-purpose branch administration tool. It allows for the creation of isolated development environments within a single repository. This is done to make it easier to track changes to the repository and assign tasks to team members.

(b) git branch -a
This returns a list of all branches, whether local or remote.

(c) git branch [branch name]
This command is used to start a new branch.

(d) git branch -d [branch name]
This is used to remove a branch.

(e) git branch-m [old branch name][new branch name]
This is used to rename a local branch.

(f) git checkout
This command allows you to switch branches or restore working tree files. It can also be used to navigate existing branches and check out old commits and file revisions.

(g) git checkout-b [branch name]
This is used to create and switch to a new branch.

(h) git checkout-b [branch name] origin/[branch name]
This clones and switches to a remote branch.

(i) git checkout [branch name]
This command switches to a branch.

(j) git switch
This command is used to navigate between branches. When this command is executed, the working tree is updated to match the branch. IT IS IMPORTANT TO NOTE THAT THIS COMMAND IS EXPERIMENTAL AND THE BEHAVIOR MAY CHANGE.

(k) git merge
This is used to link two or more development histories together.

(l) git merge tool
To resolve merge conflicts, use this command to run merge conflict resolution tools. It is typically executed following a git merge.

(m) git log
This displays commit logs.

(n) git stash
When you want to record the current state of the working directory but return to a clean working directory, use this command. That is, it saves your local changes while reverting the working directory to the main commit.

(o) git worktree
This command is used to manage multiple working trees attached to the same repository. Some of them include:

  • git worktree add
    This command associates a new working tree with the repository and adds extra metadata to it. A work tree is distinguished from others in the same repository in this manner.

  • git worktree list
    This command displays information about each worktree. The main worktree appears first, followed by any linked worktrees.

  • git worktree lock
    This command prevents files from being pruned, moved, or deleted automatically.

  • git worktree move
    This command is used to relocate a worktree.

  • git worktree prune
    This command prunes worktree data.

  • git worktree remove
    This command is used to delete clean worktrees that contain no untracked files and no modified tracked files. Unclean worktrees should be removed with --force. It should be noted that the main worktree cannot be removed.

  • git worktree repair
    This command repairs worktree files that have been moved or corrupted as a result of external factors.

  • git worktree unlock
    This command allows you to prune, move, or delete a worktree.

Sharing and Updating Projects

Use the following commands to share a local change and update it in the main codebase:

(a) git fetch
This command is used to retrieve or download objects and references from a different repository. _git fetch _can retrieve files from a single repository or a URL.

(b) git pull
This command fetches from the main repository and integrates it with another repository or a local branch. It merges changes from your local machine repository into the current branch.

(c) git push
This command uses local refs to update remote or main refs while sending objects required to complete the local ref.

(d) git remote
You can manage the set of repositories whose branches you track with this command.

(e) git submodule
This command allows you to inspect, update, or initialise submodules. Submodules are repositories that are contained within another repository. Although such submodules have a history, the repository in which they are embedded is referred to as a super project.

Inspection and Comparison

Use the following command to inspect a codebase's workflow and compare it to other developers' code:

(a) git show
This command displays one or more objects (blobs, trees, tags, and commits) in a single repository.

(b) git log
This command displays commit logs.

(c) git difftool
This git command allows you to edit and compare them between revisions using common diff tools. Git difftool is a GUI for git diff. It accepts the same options and arguments.

(d) git range-diff
This git command compares two branches and displays the differences.

(e) git shortlog
This git command summarises git log output so that it can be included in release notes and announcements.

(f) git describe
This command assigns a human-readable name to an object based on the available ref.

Patching

Patching is used to distribute changes to others without pushing them to the repository's main branch to make necessary corrections. Use the following commands to achieve this:

(a) git apply
This command applies a patch (diff output supplied) to files or the index.

(b) git cherry-pick
This command applies the changes made by previous commits. This command requires a clean working tree.

(c) git rebase
This git command allows you to move branches around to avoid unnecessary merge commits. It generates a linear history that is simple to comprehend and explore.

(d) git revert
This command undoes some previous commits. Simply put, it restores a working tree to its previous state before a commit as if it never happened.

Debugging

Debugging allows you to search through your commit history to identify when a “bad commit” occurred. Use the following commands to achieve this:

(a) git bisect
This command comes in handy when you're looking for a commit that broke a feature in your project. To find the commit, it employs a binary search algorithm.

(b) git blame
This command displays which author and revision modified each line of a file the most recently.

Top comments (11)

Collapse
 
osborne profile image
Dayo

this is so helpful

Collapse
 
kanani_nirav profile image
Kanani Nirav

Thanks for sharing! Here are more git commands: Git Cheat Sheet: Essential Commands for Effective Code Management

Collapse
 
mosimidev profile image
Mosimi Akinlabi

Thank you

Collapse
 
codesolutionshub profile image
CodeSolutionsHub

Good one!

Collapse
 
mosimidev profile image
Mosimi Akinlabi

Thank you

Collapse
 
pizofreude profile image
Pizofreude

Nice sharing, add few more in my git notes.

Collapse
 
mosimidev profile image
Mosimi Akinlabi

Thank you.

Collapse
 
samuel_kelv profile image
Samuel Kelv

Nice one

Collapse
 
mosimidev profile image
Mosimi Akinlabi

Thank you Samuel

Collapse
 
nathanmcl profile image
Nathan McLaughlin

Thank you for this post. I have been searching for a good git command list.

Collapse
 
mosimidev profile image
Mosimi Akinlabi

You are welcome