In this blog post, I have tried to cover some basic Git commands that I used frequently in my work with Git. Git is a powerful tool that helps you manage your code or files over time. With Git, you can create snapshots of your project, compare and merge different versions, collaborate with other developers, and revert mistakes.
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.
Buy Git Commands Cheat Sheet (PDF)
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:
Git configuration
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 "your-email@example.com"
: 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
Create Project
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
Local Changes
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
Track Changes
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 difference between two branch -
$ git diff branch-1 branch-2 -- file-path
: Show the differences between two branches for a specific file
Commit History
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 location
Ignoring files
Specify intentionally untracked files that Git should ignore. Create .gitignore:
$ touch .gitignore
List the ignored files:
# Ignore all .txt files in the current directory
*.txt
# Ignore all .log files in any subdirectory
**/*.log
# Ignore all files in the data directory except data.csv
data/*
!data/data.csv
# Ignore all files that start with temp or backup
temp*
backup*
A collection of useful .gitignore templates for different languages or frameworks: gitignore templates
Branching
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
-
$ 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 calledname
based on the current commit -
$ git branch name hash
: Create a new branch based on a specific commit identified by its hash
Git checkout
-
$ git checkout branch-name
: Switch between branches in a repository -
$ git checkout -b branch-name
: Create a new branch and switch to it
Git stash
Switch branches without committing 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
Merging
To combine the changes from different branches into one, you need to use some commands to merge them.
Git merge
-
$ 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
-
$ 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
Git Rebase vs Git Merge, Which One is the Best Option?
Remote
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
Pushing Updates
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
Pulling updates
Git pull
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
Git fetch
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
Undo changes
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
-
$ 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
-
$ 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.
Removing Files
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 stages the deletion for the next commit. -
$ git rm --cached file
: Delete a file only from the staging area but not from the working directory.
For a table of all commands, please see below. You can also start or fork this GitHub gist if you like.
Reference: Git Docs
If You are using Medium Please support and follow me for interesting articles. Medium Profile
If this guide has been helpful to you and your team please share it with others!
Top comments (2)
I made some aliases for things I do often in Git as well, like (Windows only but this can be done in bash on Linux for example as well):
So with something like this I can just do
gr
and start with a clean master state when programming.And these can be chained with other custom commands as well.
Thank you for your comment! Having shortcuts like these is wonderful because they make our work processes more efficient and smooth.