Featured image of post Git Cheatsheet- Common Operations

Git Cheatsheet- Common Operations

Common GIT Operations


πŸ“Œ Setting Up Git (For the Newbies)

Before diving into the fun stuff, let’s configure Git:

1
2
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

You can check your setup with:

1
git config --list

Boom!

Git now knows who you are.

No more anonymous commits from “Some Guy on the Internet.”


🎬 Starting a New Project (Or Cloning an Existing One)

Initialize a new repo:

1
git init

Clone an existing repo:

1
git clone https://github.com/user/repo.git

Now you have a Git-powered project.

It’s like giving your project superpowers (but with great power comes great responsibility).


πŸ“‚ Tracking Changes

Check the status of your repo:

1
git status

This tells you what’s changed, what’s staged, and whether you accidentally committed debug print statements.

Add files to staging:

1
2
git add file.txt  # Add a specific file
git add .         # Add all changes

Commit your changes:

1
git commit -m "Add a meaningful commit message"

Make sure your commit messages don’t look like:

1
2
3
git commit -m "Fixed stuff"
git commit -m "Final fix"
git commit -m "FINAL final fix"

Future you (and your team) will thank you.


πŸ”„ Working with Branches

Create a new branch:

1
git branch feature-branch

Switch to another branch:

1
git checkout feature-branch

Or, since Git 2.23, use:

1
git switch feature-branch

Create and switch in one go:

1
git checkout -b new-branch

List all branches:

1
git branch

Delete a branch (once you’re done with it):

1
git branch -d old-branch

Use -D (uppercase) if Git refuses to delete it because it’s not merged yet.


πŸ“€ Pushing and Pulling

Push your code to remote:

1
git push origin branch-name

Pull latest changes from remote:

1
git pull origin branch-name

πŸ”„ Merging and Rebasing

Merge branches:

1
2
git checkout main
git merge feature-branch

Rebase (for a cleaner commit history):

1
2
git checkout feature-branch
git rebase main

Abort a bad rebase:

1
git rebase --abort

Think of merge as adding a new chapter, while rebase rewrites history.

Use wisely.


🏠 Reset, Revert, and Stash (a.k.a. “Oops, Undo!”)

Undo the last commit (without losing changes):

1
git reset --soft HEAD~1

Undo the last commit (and erase changes):

1
git reset --hard HEAD~1

Revert a commit (create a new commit that undoes it):

1
git revert <commit-hash>

Stash changes (hide them temporarily):

1
git stash

Apply stashed changes:

1
git stash pop

πŸ•΅οΈβ€β™‚οΈ Finding Stuff

View commit history:

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

See who changed a specific file:

1
git blame file.txt

Search in commits:

1
git grep "search-term"

πŸ’₯ Dealing with Conflicts

When Git screams about merge conflicts:

  1. Open the conflicting file.
  2. Manually fix the differences (keep what you want, delete what you don’t).
  3. Stage the file: git add file.txt
  4. Commit the changes: git commit -m "Fix merge conflict"

Merge conflicts are like unexpected plot twists in a movie.

Sometimes painful, but necessary.


🏁 Wrapping Up

Git is an essential tool, and mastering it will save you a ton of headaches.

This cheatsheet covers the basics, but Git is deep, so don’t stop learning!

If all else fails, remember the golden rule:

1
git reset --hard origin/main

Use it with caution.

It’s the “Ctrl+Z” of Git, but it erases your local changes.


πŸ”‘ Key Ideas

Key ConceptSummary
git initStart a new Git repo
git cloneClone an existing repo
git addStage changes
git commitSave staged changes
git branchManage branches
git mergeMerge branches
git rebaseRebase for a clean history
git resetUndo changes
git stashTemporarily store changes
git logView commit history
git push/pullSync with remote

πŸ”— References