Git and Github
Estimated reading: 5 minutes 7 views

Here’s a Comprehensive List of Git Commands Categorized Based on Their Functions



🧰 Configuration Commands

Setting up your identity

Before anything else, Git needs to know who you are. Run the following to let Git identify your commits:

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

This sets your name and email across all repositories on your system.

Viewing current configurations

Want to check your current Git settings? Use:

git config --list

This gives a quick rundown of all active configurations.

Setting your preferred editor

Don’t like the default editor? Set your favorite one (e.g., VS Code or Nano):

git config --global core.editor "code --wait"

📁 Repository Setup

Starting a new project with Git

Ready to version control your project? Initialize Git with:

git init

This creates a .git folder and starts tracking changes.

Cloning existing projects

To grab a repo from GitHub or another remote:

git clone <repository-url>

This pulls down the entire repository, including all commit history.


✅ Staging and Committing

Checking changes

See what’s been modified or added:

git status

It’s your go-to command before staging or committing.

Adding files to staging

To stage a specific file:

git add filename.txt

Or add everything:

git add .

Committing changes

Commit your staged changes with a descriptive message:

git commit -m "Initial commit"

Combine staging and committing in one line for tracked files:

git commit -am "Quick update"

🔍 Viewing History

Viewing commit logs

Check the project’s history:

git log

Want a cleaner, one-line-per-commit view?

git log --oneline

Checking differences

See what’s changed but not yet staged:

git diff

Or what’s staged and ready to commit:

git diff --staged

🌿 Branching

Creating branches

Start a new feature or fix with:

git branch feature-x

Switching and deleting branches

Move to another branch:

git checkout feature-x

Create and switch in one go:

git checkout -b hotfix-1

Clean up after merging:

git branch -d old-branch

🔁 Merging and Rebasing

Merging changes

Merge one branch into another:

git merge branch-name

Rebasing workflows

Clean history by stacking commits sequentially:

git rebase main

Cherry-picking commits

Apply a specific commit from any branch:

git cherry-pick commit-hash

Perfect for hotfixes or feature reuse.


🔄 Remote Repositories

Managing remotes

Check what remotes you’re connected to:

git remote -v

Add a new remote origin:

git remote add origin https://github.com/user/repo.git

Fetching, pulling, and pushing changes

Download new changes (without applying):

git fetch

Fetch and merge in one:

git pull

Push your commits:

git push

First-time push with upstream tracking:

git push -u origin main

🧽 Undoing Changes

Resetting and reverting

Unstage a file:

git reset filename.txt

Hard reset everything (be careful!):

git reset --hard

Undo a commit with a new one:

git revert commit-hash

Discarding changes safely

To discard changes in a file:

git checkout -- filename.txt

Great for quick reverts without touching commit history.


🔒 Tagging

Creating tags

List all tags:

git tag

Create a new tag:

git tag v1.0

Or an annotated one:

git tag -a v1.0 -m "Release version 1.0"

Pushing tags to remote

git push origin v1.0

Simple and clean.


📌 Advanced Commands

Stashing changes

Save your work without committing:

git stash

Get it back later:

git stash pop

Blaming and inspecting commits

Who changed what and when?

git blame filename.txt

See detailed commit info:

git show commit-hash

📦 Bonus: Daily Git Workflow Tips

  • Always pull before starting work.
  • Use meaningful commit messages.
  • Delete old branches once merged.
  • Make small, frequent commits.
  • Rebase for a clean history; merge when collaboration is key.

🛠️ Troubleshooting Common Git Issues

Resolving merge conflicts

Git marks the conflicts. Open the file, fix it manually, and then:

git add conflict-file.txt
git commit

Fixing detached HEAD state

If you see something like “You are in a detached HEAD state,” don’t panic. Just create a new branch:

git checkout -b new-branch-name

🧠 Pro Tips for Mastering Git Faster

  • Use aliases to shorten commands (e.g., git config --global alias.st status)
  • Practice with dummy projects.
  • Use visual Git tools (like GitKraken or VS Code Git panel)
  • Learn .gitignore for cleaner repos
  • Read commit messages like breadcrumbs through time

📝 Conclusion

Git can feel overwhelming at first—but once you break it down into categories, it’s actually pretty logical and powerful. This list of Git commands serves as your go-to cheat sheet whether you’re a total beginner or a seasoned developer. Bookmark it, refer to it often, and you’ll become a Git ninja in no time.


❓ FAQs

Q1: What is the difference between git pull and git fetch?
git pull fetches new changes and merges them, while git fetch only downloads changes without merging.

Q2: Can I undo a commit in Git?
Yes! You can use git revert to safely undo a commit or git reset to remove it (with caution).

Q3: How do I resolve a merge conflict in Git?
Edit the conflicted files manually, then stage and commit the changes.

Q4: What’s the difference between git merge and git rebase?
merge keeps history as-is with a new merge commit. rebase rewrites history to be linear and cleaner.

Q5: Is it necessary to use git add before git commit?
Yes, unless you’re using git commit -am, which skips add for tracked files only.


Leave a Reply

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

Share this Doc

Git Commands List

Or copy link

CONTENTS
Scroll to Top