Git Cheat Sheet

Git Cheat Sheet

Git Cheat Sheet – Essential Commands & Workflows

Why Use Git?

Git is the most widely used version control system, allowing developers to track changes, collaborate, and manage code efficiently.

Track code changes – Keep a history of modifications.
Work collaboratively – Manage branches and pull requests.
Revert mistakes – Roll back changes when needed.
Optimize workflows – Automate with Git hooks and CI/CD pipelines.

This Git cheat sheet covers essential commands, branching workflows, merging strategies, and troubleshooting tips to streamline your development process.


Basic Git Commands

1️⃣ Configuring Git

Set up your username and email (used for commits):

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

Check your configuration:

git config --list

2️⃣ Initializing & Cloning Repositories

Create a new Git repository:

git init

Clone an existing repository:

git clone <repository-url>

Working with Commits

3️⃣ Staging & Committing Changes

Add files to staging area:

git add <file>

Add all changes:

git add .

Commit changes with a message:

git commit -m "Your commit message"

4️⃣ Viewing Commit History

See commit history:

git log --oneline --graph --all

View changes before committing:

git diff

View changes in staged files:

git diff --staged

Branching & Merging

5️⃣ Creating & Switching Branches

List all branches:

git branch

Create a new branch:

git branch feature-branch

Switch to a branch:

git checkout feature-branch

Create and switch to a new branch:

git checkout -b new-branch

6️⃣ Merging Branches

Merge a branch into the current branch:

git merge feature-branch

Use rebase to keep history clean:

git rebase main

7️⃣ Deleting Branches

Delete a local branch:

git branch -d feature-branch

Delete a remote branch:

git push origin --delete feature-branch

Working with Remotes

8️⃣ Pushing & Pulling Changes

Push changes to the remote repository:

git push origin main

Pull the latest changes from remote:

git pull origin main

Fetch changes without merging:

git fetch origin

9️⃣ Managing Remotes

List remote repositories:

git remote -v

Add a new remote repository:

git remote add origin <repository-url>

Change the URL of a remote:

git remote set-url origin <new-url>

Undoing Changes & Fixing Mistakes

Discard Local Changes

Reset all unstaged changes:

git checkout -- <file>

Reset all staged changes:

git reset HEAD <file>

1️⃣1️⃣ Reverting Commits

Undo the last commit while keeping changes:

git reset --soft HEAD~1

Undo the last commit and remove changes:

git reset --hard HEAD~1

Revert a pushed commit:

git revert <commit-hash>

Advanced Git Commands

1️⃣2️⃣ Stashing Changes

Save work without committing:

git stash

List stashed changes:

git stash list

Apply the last stash:

git stash apply

1️⃣3️⃣ Tagging Releases

Create an annotated tag:

git tag -a v1.0.0 -m "Version 1.0.0"

Push tags to remote:

git push --tags

1️⃣4️⃣ Fixing Merge Conflicts

List conflicted files:

git status

Manually edit the files, then stage the resolved files:

git add <file>

Complete the merge:

git commit -m "Resolved merge conflicts"

Git Best Practices

Commit often with meaningful messages – Helps track changes effectively.
Use branches for features & bug fixes – Keep main stable.
Pull before pushing – Avoid merge conflicts.
Use .gitignore – Prevent unnecessary files from being tracked.
Review code before merging – Ensure quality and avoid bugs.

Need more Git help? Drop a comment and share your favorite Git commands!

That Developer Guy

Website:

Leave a Reply

Your email address will not be published. Required fields are marked *

14 + 3 =