Skip to content

๐Ÿš€ Useful Git Commands

A categorized reference of common and essential Git commands for daily use, including explanations of when and why to use them.


๐Ÿ”ง Initial Configuration

git config --global user.name "Your Name"         # Set your name for all repos
git config --global user.email "your.email@example.com"  # Set your email for all commits
git config --global core.editor nano              # Set your preferred text editor
git config --list                                 # Check your current Git configuration
Use these commands to set up your identity and preferences when working with Git.


๐Ÿ“ฆ SSH Configuration

Use these commands to set up your identity and preferences when working with Git, found it in:

Set Up Github SSH Key


๐Ÿ“ Repository Initialization & Cloning

git init                           # Start a new Git repository in the current folder
git clone <REPO_SSH_URL>           # Create a local copy of a remote repository
Use init when starting a new project from scratch. Use clone when collaborating on an existing project.


๐Ÿ“„ Status and Diffs

git status                     # See the current state of the working directory and staging area
git diff                       # View changes in files not yet staged
git diff --staged              # View changes staged for the next commit
These help you review what you've changed before committing.


โž• Staging and Committing Changes

git add file.txt               # Stage a file for the next commit
git add .                      # Stage all modified and new files
git commit -m "Message"        # Save changes with a message describing the update
git commit -am "Message"       # Add and commit changes in tracked files in one step
Use these to selectively or entirely prepare changes to be permanently recorded in the repo.


โช Undoing Changes

git restore file.txt                  # Revert changes in working directory
git restore --staged file.txt         # Unstage a file without changing its contents
git reset --hard HEAD                 # Discard all changes and return to last commit
Use these to correct mistakes or discard unwanted changes.


๐Ÿ” Viewing History

git log                              # View full commit history
git log --oneline                    # View condensed history
git log --graph --oneline --all      # Visualize branches and commits as a graph
Use to inspect the evolution of your project and understand its commit structure.


๐ŸŒฟ Branching

git branch                           # List all local branches
git branch new-branch                # Create a new branch
git checkout new-branch              # Switch to a different branch
git checkout -b new-branch           # Create and switch to a new branch
git switch new-branch                # (Modern) Switch branches
git switch -c new-branch             # (Modern) Create and switch
git merge feature-branch             # Merge a branch into the current one
git branch -d old-branch             # Delete a branch that is no longer needed
Use branches to work on features or fixes in isolation from the main codebase.


๐ŸŒ Working with Remotes

git remote -v                        # Show the remotes for this repository
git remote add origin <REPO_URL>    # Add a remote called origin
git fetch                            # Fetch changes from the remote without merging
git pull                             # Fetch and merge changes from the remote
git push                             # Upload your local changes to the remote
git push -u origin main              # Push current branch and set upstream
These commands are essential for collaborating via a shared remote repository.


๐Ÿงช Stashing (Temporary Saves)

git stash                            # Save your changes temporarily
git stash list                       # View saved stashes
git stash apply                      # Reapply the latest stash
git stash drop                       # Remove a stash
Use when you need to switch context without committing unfinished work.


๐Ÿ“Œ Tags

git tag                              # List tags
git tag v1.0                         # Create a tag for a specific commit
git push origin v1.0                 # Share the tag with others
Tags mark specific points in history, commonly used for releases.


๐Ÿงผ Cleanup

git clean -n                         # Preview which untracked files would be removed
git clean -f                         # Actually remove untracked files
Use to remove clutter from your working directory.


๐Ÿ“ฆ Checkout (Detailed)

git checkout branch-name             # Switch to an existing branch
git checkout -b new-branch           # Create and switch to a new branch
git checkout commit_hash             # View the code at a specific commit (detached HEAD)
git checkout file.txt                # Restore file to last committed version
Use checkout to navigate between versions of your project or recover files.