Here’s a Git cheat sheet of some commonly used Git commands. These commands help you manage your version control workflow. Remember to replace placeholders like
<file_name> with your actual branch or file names.
What is Git?
Git is a system that stores your project history on your own machine and lets you work without needing an internet connection. Git creates snapshots of your project called commits, which have unique code and messages. You can also create branches, which are like copies of your project that you can work on separately. You can also connect to online copies of your project called remote repositories, which you can share with others or back up your work.
Git Cheat Sheet
To use Git, you need to install it on your machine and set up some basic information. You also need to learn some commands that you can type in your terminal or command prompt to talk to Git. Here are some of the most common and useful commands:
Before you start using Git, you need to tell it who you are and how you want it to work.
$ git config: Get and set configuration variables that control all facets of how Git looks and operates.
$ git config --global user.name "User name": Set the name
$ git config --global user.email "[email protected]": Set the email
$ git config --global core.editor "your-editor": Set the default editor
$ git config --global color.ui auto: Turns on color output for git commands
$ git config --list: see all your settings
To start a new project with Git, you need to make a folder for your project and turn it into a Git repository.
$ git init: Create a local repository
$ git clone: Clone remote repository
To make changes to your project files locally, you need to use some commands to track, stage, and commit them.
$ git add file-path: Adds a file to the staging area, which means that it is ready to be committed
$ git add .: Add all files to the staging area
$ git commit -m " Commit Message": Commit the file in the version history with a message
To keep track of your project history and see what changes were made by whom and when, you need to use some commands to view and navigate through your commits.
$ git diff: Shows the differences between your repository and the stage
$ git diff --staged: Shows the differences between the stage and the last commit
$ git diff HEAD: Track the changes after committing a file
$ git diff < branch 2>: Track the changes between two commits
$ git status: Display the state of the working directory and the staging area
$ git show: Show the newest commit on a branch
$ git diff -- file-path: Show Particular File changes
$ git diff branch-1 branch-2 --name-only: Show the difference between the two branch
$ git diff branch-1 branch-2 -- file-path: Show the differences between two branches for a specific file
To modify or manipulate your commit history, you need to use some commands that allow you to rewrite or rearrange your commits.
$ git log: Display the most recent commits and the status of the head
$ git log -oneline: Display the output as one commit per line
$ git log -stat: Displays the files that have been modified
$ git log -p: Display the modified files with the location
Specify intentionally untracked files that Git should ignore. Create .gitignore:
$ touch .gitignore
List the ignored files:
# Ignore all .txt files in the current directory
# Ignore all .log files in any subdirectory
# Ignore all files in the data directory except data.csv
# Ignore all files that start with temp or backup
A collection of useful .gitignore templates for different languages or frameworks: gitignore templates
To create parallel versions of your project that you can work on separately, you need to use some commands to create, switch, and delete branches.
$ git branch: Show a list of all branches in your local repository
$ git branch -a: Show all branches in both local and remote repositories
$ git branch -r: Show only remote branches
$ git branch name: Creates a new branch called
namebased on the current commit
$ git branch name hash: Create a new branch based on a specific commit identified by its hash
$ git checkout branch-name: Switch between branches in a repository
$ git checkout -b branch-name: Create a new branch and switch to it
Switch branches without committing to the current branch.
$ git stash: Stash current work
$ git stash save "your message": Saving stashes with a message
$ git stash list: Check the stored stashes
$ git stash apply: Re-apply the changes that you just stashed
$ git stash show: Track the stashes and their changes
$ git stash pop: Re-apply the previous commits
$ git stash drop: Delete a most recent stash from the queue
$ git stash clear: Delete all the available stashes at once
$ git stash branch: Stash work on a separate branch
To combine the changes from different branches into one, you need to use some commands to merge them.
$ git merge name: Merges branch called name into the current branch, by creating a new commit that incorporates both changes
$ git merge --abort: Aborts the merge process and restores the original state of your project, if there are any conflicts or errors during the merge
$ git rebase: command used in Git to apply a sequence of commits from one branch to another
$ git rebase -continue: to continue the rebasing process after you have resolved conflicts manually
$ git rebase --skip: to skip a commit when rebasing
To connect your local repository with an online copy of your project, you need to use some commands to manage your remote repositories.
$ git remote: This shows a list of all remote repositories that are associated with your local repository, and their names and URLs
$ git remote -v: Check the configuration of the remote server
$ git remote show name: Show information about a specific remote repository called name
$ git remote add origin repo-url: Add a remote for the repository
$ git remote rm: Remove a remote connection from the repository
$ git remote rename: Rename remote server
$ git remote show: Show additional information about a particular remote
$ git remote set-url name url: Changes the URL of a remote repository called name to url
To send your local changes to a remote repository, you need to use some commands to push them.
$ git push origin master: Push data to the remote repository
$ git push --all: Push all branches to the default remote repository
$ git push --all origin: Specify the remote repository explicitly
$ git push -f: Force push data to the remote repository
To receive changes from a remote repository to your local repository, you need to use some commands to pull them.
$ git pull: Pull the changes from the default remote repository and branch, usually origin and master respectively
$ git pull origin master: Specify the remote repository and branch explicitly
Download branches and tags from one or more repositories.
$ git fetch <repository Url>: Fetch the remote repository
$ git fetch: Fetch a specific branch
$ git fetch -all: Fetch all the branches simultaneously
$ git fetch origin: Synchronize the local repository
To undo or revert some changes in your project, you need to use some commands that allow you to reset, revert, or restore your files or commits.
$ git revert HEAD: Undo the latest commit
$ git revert hash: Creates a new commit that undoes the changes made by a specific commit identified by its hash
$ git revert branch: Undo all commits on a branch
$ git reset --soft hash: Resets your current branch to a specific commit identified by its hash, but keeps your working directory and staging area unchanged, which means that you can still commit or modify your changes later
$ git reset --mixed hash: Resets your current branch to a specific commit identified by its hash, and resets your staging area to match it, but keeps your working directory unchanged, which means that you can still modify or add your changes later
$ git reset --hard hash: Resets your current branch to a specific commit identified by its hash, and resets your working directory and staging area to match it, which means that you lose any changes you made since then.
To remove some files from your project, you need to use some commands that allow you to delete them from your working directory, staging area, or history.
$ git rm file: Delete a file from both your working directory and staging area, and stage the deletion for the next commit.
$ git rm --cached file: Delete a file only from the staging area but not from the working directory.
This Git cheat sheet covers some of the most commonly used Git commands, but Git offers many more features and options. You can use
git --help followed by a command name to get more information about a specific Git command.