All Articles Technical Journal
Git & Version Control

Git & GitHub Commands: The Developer's Cheat Sheet

Adyaprana Pradhan
Git & GitHub Commands: The Developer's Cheat Sheet
Topics
Git GitHub DevOps Version Control CLI Developer Tools
Share Article

Git is a version control system that helps developers track changes, manage source code, collaborate with others and maintain different versions of a project. GitHub provides an online platform for hosting Git repositories.

This cheat sheet is designed as a quick practical reference for everyday development. Find the operation you need, copy the commands and replace the example filenames, paths and repository URLs with your own.

Quick Navigation


01. First-Time Git Setup

Configure Git with your name and email address. You normally need to do this only once on each computer.

git --version
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Set Visual Studio Code as your default Git editor.

git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --list

Use an email associated with your GitHub account or your GitHub-provided private noreply email.


02. Upload a New Project to GitHub

Use this workflow when you have created a new project on your computer and want to upload it to GitHub for the first time.

Step 1 — Open your project directory

Windows Command Prompt:

cd /d "C:/path/to/your-project"

Linux, macOS or Git Bash:

cd /path/to/your-project

Step 2 — Initialize the repository

git init -b main
git status

Step 3 — Stage and commit your files

Create your .gitignore file first and make sure no passwords, API keys or private files are included.

git add .
git status
git commit -m "Initial commit"

Step 4 — Create a repository on GitHub

  • Open GitHub and select New Repository.
  • Enter your repository name.
  • Choose Public or Private.
  • Leave the repository empty if uploading an existing local project.
  • Copy the repository HTTPS URL.

Create a new GitHub repository

Step 5 — Connect and upload

git remote add origin https://github.com/username/repo-name.git
git push -u origin main

Your local project is now connected to GitHub after a successful push.

Complete upload commands

Run these from your project directory after creating an empty GitHub repository and reviewing your files.

git init -b main
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/username/repo-name.git
git push -u origin main

03. Update an Existing GitHub Project

This is the most commonly used workflow. Whenever you modify an existing project, commit the changes and push them to GitHub.

git status
git add .
git commit -m "Updated project"
git push origin main

If the upstream branch is already configured, you can simply use:

git push

You do not need to run git init or add the remote again for an existing repository.


04. Clone a GitHub Repository

Cloning downloads an existing repository, including its files and available commit history.

git clone https://github.com/username/repo-name.git
cd repo-name

Clone into a custom folder

git clone https://github.com/username/repo-name.git my-project

Clone a specific branch

git clone -b development --single-branch https://github.com/username/repo-name.git

Clone only the latest history

git clone --depth 1 https://github.com/username/repo-name.git

A shallow clone is useful when you do not need the complete commit history of a large repository.


05. Pull and Synchronize Changes

Download changes and integrate them

git pull origin main

Download changes without merging

git fetch origin
git status

Git fetch updates remote-tracking information without directly modifying your working files.

Pull using rebase

Use this when your local commits have not been published and you want to place them after the latest remote commits.

git pull --rebase origin main

After resolving any conflicts, push your changes:

git push origin main

Always commit or stash your local changes before rebasing. Avoid rebasing shared commits without coordinating with your team.


06. Staging and Commits

Check repository status

git status
git status --short

Stage all changes

git add .

Stage a specific file

git add app.py

Review staged changes

git diff --staged

Create a commit

git commit -m "Added new feature"

Stage modified tracked files and commit

git commit -am "Updated existing files"

The -am option does not automatically include newly created, untracked files.

Correct the latest local commit message

git commit --amend -m "Corrected commit message"

Amending rewrites the latest commit. Prefer using it before pushing that commit.


07. Branch Management

Branches allow you to develop new features without directly changing the main branch.

List branches

git branch
git branch -a

Create a branch

git branch development

Switch to a branch

git switch development

Create and switch immediately

git switch -c feature-login

Push a new branch to GitHub

git push -u origin feature-login

Switch back to main

git switch main

Delete a merged local branch

git branch -d feature-login

Delete a remote branch

git push origin --delete feature-login

Rename the current branch to main

git branch -m main

08. Merge and Rebase

Merge a feature branch into main

git switch main
git pull --ff-only origin main
git merge feature-login
git push origin main

If your team uses pull requests, push the feature branch and create a pull request instead of merging directly into main.

Rebase a local feature branch onto main

git switch feature-login
git fetch origin
git rebase origin/main

Resolve a rebase conflict

Open the conflicted files, resolve the conflicting code, save the files and continue the rebase.

git status
git add .
git rebase --continue

Cancel an unfinished rebase

git rebase --abort

Cancel an unfinished merge

git merge --abort

09. Manage Remote Repositories

A remote is a reference to another repository, usually hosted on GitHub.

View connected remotes

git remote -v

Add a remote repository

git remote add origin https://github.com/username/repo-name.git

Change an existing remote URL

git remote set-url origin https://github.com/username/new-repo.git

Remove a remote connection

git remote remove origin

Removing the remote connection does not delete the repository from GitHub.

View remote repository information

git remote show origin

10. Commit History and Differences

View commit history

git log
git log --oneline

View a visual branch history

git log --oneline --graph --all --decorate

View the latest five commits

git log -5 --oneline

View unstaged changes

git diff

View staged changes

git diff --staged

Inspect the latest commit

git show HEAD

Inspect a specific commit

git show abc1234

Replace abc1234 with the actual commit hash obtained from git log.


11. Undo Changes Safely

Unstage a file without deleting its changes

git restore --staged app.py

Unstage all files

git restore --staged .

Undo the latest local commit but keep changes staged

git reset --soft HEAD~1

Undo the latest local commit and unstage changes

git reset --mixed HEAD~1

Reverse a published commit safely

Git revert creates a new commit that reverses the selected commit without rewriting shared history.

git revert HEAD

Discard unstaged changes in a file

Warning: this permanently discards unstaged modifications to the selected tracked file. Back up important changes first.

git restore app.py

12. Temporarily Save Changes

Git stash temporarily stores unfinished changes so you can switch branches or perform other operations.

Save unfinished changes

git stash push -m "Work in progress"

Include untracked files

git stash push -u -m "Temporary backup"

View saved stashes

git stash list

Restore the latest stash without removing it

git stash apply

Restore and remove the latest stash if successful

git stash pop

13. Tags and Releases

Tags identify important versions of your software, such as stable releases and major updates.

Create an annotated version tag

git tag -a v1.0.0 -m "First stable release"

List and inspect tags

git tag
git show v1.0.0

Push a specific tag to GitHub

git push origin v1.0.0

Push all local tags

git push origin --tags

14. Gitignore and Security

A .gitignore file prevents selected untracked files from being included in Git commits. Create it in the root directory of your project.

Example .gitignore file

# Environment variables and secrets
.env
.env.*
!.env.example
*.pem
*.key

# Python
venv/
.venv/
__pycache__/
*.pyc

# JavaScript
node_modules/
.next/
dist/
build/

# Logs
*.log

# Operating system files
.DS_Store
Thumbs.db

Stop tracking an already tracked environment file

git rm --cached .env
git add .gitignore
git commit -m "Stop tracking environment file"

Never upload API keys, passwords, database credentials or private access tokens. If a secret has already been committed, removing the file is not enough. Revoke or rotate the exposed credential and review the repository history.


15. Git Submodules

Submodules allow you to include another Git repository inside your project while maintaining its independent version history.

Add a submodule

git submodule add https://github.com/username/library.git
git commit -m "Added library submodule"

Initialize and download submodules

git submodule update --init --recursive

Clone a project with its submodules

git clone --recurse-submodules https://github.com/username/repo-name.git

16. Useful Git Shortcuts

Git aliases let you create shorter versions of frequently used commands.

git config --global alias.st status
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.sw switch
git config --global alias.lg "log --oneline --graph --all"

Use the new shortcuts

git st
git br
git lg

17. Common Git Errors and Solutions

Error: remote origin already exists

Your repository already has a remote named origin. Update its URL instead of adding it again.

git remote -v
git remote set-url origin https://github.com/username/repo-name.git

Error: src refspec main does not match any

Usually this means the repository has no commits or the current branch is not named main.

git status
git branch

If this is a new repository with no commits, stage your reviewed files and create the first commit.

git add .
git commit -m "Initial commit"
git branch -m main
git push -u origin main

Error: non-fast-forward push rejected

The remote repository contains commits that your current local branch does not contain. Inspect the changes before integrating them.

git fetch origin
git log --oneline --graph --all

For unpublished local commits on a branch with shared history, use:

git pull --rebase origin main

Resolve any conflicts before pushing.

git push origin main

Error: not a Git repository

Make sure your terminal is inside the correct project directory.

cd /d "C:/path/to/your-project"
git status

Only run git init if the project is intentionally a new repository.

Error: authentication failed

GitHub does not accept your normal account password for HTTPS Git authentication. Use Git Credential Manager, GitHub CLI, a personal access token or an appropriately configured SSH key.

If GitHub CLI is installed:

gh auth login
gh auth setup-git

18. Advanced Git Commands

Force push with lease

Sometimes a previously pushed feature branch needs to be updated after intentionally rewriting its history.

Warning: force pushing rewrites remote history. Use it only on branches you control and after confirming that nobody else's changes will be lost. Avoid force pushing to main.

git push --force-with-lease origin feature-login

Force-with-lease is safer than a normal force push, but it is not risk-free.

Recover previous local commit references

git reflog

Git reflog can help you locate recent commits after an accidental reset or branch change.

Create a recovery branch from a previous commit

git branch recovery-branch abc1234

Apply a specific commit to the current branch

git cherry-pick abc1234

Preview untracked files that could be removed

git clean -nd

The -n option performs a dry run. It previews files without deleting them.

Hard reset — destructive operation

Warning: this moves the current branch back one commit and discards tracked working-tree and staged changes. Do not use this as a normal troubleshooting step. Back up important work first.

git reset --hard HEAD~1

19. Everyday Developer Workflow

Before starting work

From a clean working tree, synchronize your main branch.

git switch main
git status
git pull --ff-only origin main

Start a new feature

git switch -c feature-update

After completing your changes

git status
git diff
git add .
git diff --staged
git commit -m "Implemented new feature"
git push -u origin feature-update

Create a pull request on GitHub, review the changes, and merge the feature after successful testing.

Quick update for a personal project

For your own repository, when working directly on main and the remote branch is already synchronized:

git add .
git commit -m "Updated project"
git push origin main

Essential Commands at a Glance

  • git status — Check repository status.
  • git add . — Stage changes.
  • git commit — Save a version.
  • git push — Upload commits.
  • git pull — Download and integrate changes.
  • git fetch — Download remote information.
  • git clone — Copy a repository.
  • git branch — Manage branches.
  • git switch — Change branches.
  • git merge — Integrate branches.
  • git log — View commit history.
  • git diff — Inspect differences.
  • git stash — Temporarily save changes.
  • git revert — Reverse a commit.
  • git remote -v — View remote URLs.

Best Practices

  • Always review changes using git status and git diff.
  • Write short and meaningful commit messages.
  • Commit small, logical changes regularly.
  • Use feature branches for major development work.
  • Never commit sensitive credentials.
  • Keep your local and remote repositories synchronized.
  • Prefer git revert for undoing published commits.
  • Avoid unnecessary force pushes and hard resets.
  • Test your application before pushing changes.

Official Documentation

Conclusion

Git is one of the most important tools in modern software development. Mastering these commands makes it easier to maintain personal projects, collaborate with teams and manage production applications confidently.

Bookmark this cheat sheet and use it as your everyday reference whenever you work with Git and GitHub.