A list of useful Git commands
These are some snippets I’ve saved over a couple of projects using Git. They include common commands I use regularly.
A few things to know about Git
Before diving into commands, here are some basic points about how Git works:
.git
is just a folder: When you rungit init
, Git creates a hidden.git
folder in your project. This folder holds all the metadata and history of your repository. Deleting it removes your version history but not your files.- Git as a tree structure: Git manages your project like a tree, where branches represent different paths of development. The main (or master) branch is often the starting point.
- Commits save the entire project: When you commit, Git takes a snapshot of your entire project, not just the changes. Git optimises storage to avoid redundancy, making version history lightweight.
- Git is not GitHub: Git is the version control system, while GitHub is just a website that lets you host your repositories online. Other alternatives to GitHub include GitLab or Bitbucket. You can use Git locally without ever needing a hosting platform.
If you want to get deeper into Git and understand how it actually works you should check out this course by The Primeagen.
Git essential snippets
Initialising and configuring
git --version
git -v
Check if Git is installed and display the current version.
git init
Initialise a new Git repository in the current directory. Run this in your project root to start tracking changes.
Note: If you’re creating projects using React (vite
), Rails (rails new
), or similar frameworks, the Git repo is often initialised automatically. Running git init
again is not necessary in such cases.
git config
git config user.name "username"
git config user.email "email@mail.com"
Set your username and email (needed for commits). Add --global
to apply these settings globally for all repositories.
touch .gitignore
Create a .gitignore
file to exclude specific files or directories. Add file patterns (like node_modules/
, *.log
, or *.env
) inside .gitignore
to prevent them from being tracked.
Tracking changes
git status
Show the current state of the working directory (staged, unstaged, untracked files).
git add .
git add <file>
Stage all changes in the current directory for the next commit. Use this before committing, or specify individual files and directories to add.
git restore --staged <file>
Unstage a file after using git add .
.
git reset
Unstage all files after git add .
.
Committing changes
git commit -m "message"
Commit staged changes with a descriptive message. Write meaningful commit messages please.
git reset --soft HEAD~1
Undo the last commit but keep changes staged. Use this to rewrite history without losing work.
git log
View commit history in your repository.
Connecting to a remote repository
git remote add origin https://url-of-repository/
Link your local repository to a remote repository (in GitLab for example).
git remote -v
Verify the linked remote repository. Check the URLs of remote connections.
git push -u origin main
git push
Push local commits to the remote repository. Use -u
to set the upstream branch the first time.
Cloning and pulling projects
git clone https://url-of-repository/
git clone https://url-of-repository/ .
git clone https://url-of-repository/ <folder-name>
Create a local copy of an existing remote repository. Clone a project to your local system. Use .
to clone to the current folder or folder-name
to change the name.
git pull origin main
Fetch and merge changes from the remote repository. Keep your local repository up-to-date, similar to doing git fetch
+ git merge
.
Branch management
git branch
List local branches.
git branch -r
List remote branches.
git checkout <branch_name>
Switch to an existing branch.
git checkout -b <new_branch>
Create a new branch and switch to it.
git branch -d <branch_name>
Delete a local branch. Use -D
to force delete.
git remote prune origin
git fetch --prune
Clean up deleted remote branches from your local repository.
Merging and resetting changes
git merge branch-name
Merge another branch into the current branch.
git merge --no-ff branch-name -m "Merge branch 'branch-name'"
Merge with a commit message, avoiding a fast-forward merge.
git reset --hard <commit>
Reset the current branch to a specific commit, discarding changes.
git rm --cached <file>
git rm -r --cached <folder>
Remove files or directories from the staging area without deleting them locally.
Getting help and documentation
git man <command>
git help
Access the Git manual or detailed documentation for any Git command.
git help
shows an overview of Git commands.git man <command>
displays detailed information about a specific command.
Example: git man commit
explains the git commit
command thoroughly.