DEV Community

Cover image for πŸ“˜ Mastering Git & GitHub: From Basics to Pro-Level Commands
Swapnil Meshram
Swapnil Meshram

Posted on • Edited on

πŸ“˜ Mastering Git & GitHub: From Basics to Pro-Level Commands

Presented by Swapnil Meshram within mentorship DevSync.in

🏒 Shoutout to DevSync for inspiring this beginner-friendly Git and GitHub series. Perfect for those starting out or looking to build hands-on experience with version control and collaboration. Follow DevSync for more developer-focused learning!

🧩 What are Git & GitHub?

πŸ”Ή Git

A fast, distributed version control system that helps you track changes in your source code and collaborate with others.

πŸ”Ή GitHub

A cloud-based Git repository hosting service. It enables remote collaboration, pull requests, code reviews, and issue tracking.

Here is a complete list of Git commands with syntax from basic to advanced, including setup, workflow, branching, stash, logs, remotes, recovery, and more.


🧰 1. Git Setup (Global Configuration)


git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"  # Optional: VS Code as default editor
git config --global init.defaultBranch main     # Default branch name
git config --list                               # View current config

Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 2. Repository Management


git init                                # Initialize a new Git repo
git clone <repo-url>                    # Clone an existing repo
git remote -v                           # Show remotes
git remote add origin <url>             # Add a remote origin
git remote remove origin                # Remove remote

Enter fullscreen mode Exit fullscreen mode

πŸ“„ 3. File Tracking


git status                              # Check repo status
git add <file>                          # Stage a file
git add .                               # Stage all changes
git add -A                              # Add all (tracked/untracked)
git reset <file>                        # Unstage a file
git reset                               # Unstage everything

Enter fullscreen mode Exit fullscreen mode

πŸ’Ύ 4. Committing


git commit -m "Commit message"          # Commit with message
git commit -a -m "Quick commit"         # Commit all tracked changes
git commit --amend                      # Edit last commit (don’t use after pushing)

Enter fullscreen mode Exit fullscreen mode

🌳 5. Branching & Merging


git branch                              # List branches
git branch <branch>                     # Create new branch
git checkout <branch>                   # Switch branch
git switch <branch>                     # Modern alternative to checkout
git checkout -b <new-branch>            # Create & switch
git merge <branch>                      # Merge into current branch
git branch -d <branch>                  # Delete a local branch
git branch -D <branch>                  # Force delete
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ 6. Pull, Push, and Remotes

git pull                                # Pull and merge changes
git pull origin main                    # Pull specific branch
git push                                # Push current branch
git push origin <branch>                # Push specific branch
git push -u origin <branch>             # Set upstream branch
git fetch                               # Download changes (don’t merge)
git fetch --all                         # Fetch all remotes

Enter fullscreen mode Exit fullscreen mode

πŸ•΅οΈ 7. Viewing History & Logs

git log                                 # View commit log
git log --oneline                       # Condensed log
git log --graph --oneline --all         # Visual graph
git show <commit>                       # Show specific commit
git diff                                # Compare working directory to index
git diff --cached                       # Compare index to last commit
Enter fullscreen mode Exit fullscreen mode

🧳 8. Stashing (Save work temporarily)


git stash                               # Stash changes
git stash list                          # View stashed changes
git stash pop                           # Apply & remove latest stash
git stash apply                         # Apply latest stash (keep it)
git stash drop                          # Delete latest stash
Enter fullscreen mode Exit fullscreen mode

🧼 9. Cleaning (Remove untracked)

git clean -n                            # Show what will be deleted
git clean -f                            # Delete untracked files
git clean -fd                           # Delete untracked files and dirs

Enter fullscreen mode Exit fullscreen mode

♻️ 10. Resetting, Reverting, Recovering


git reset --soft HEAD~1                 # Undo commit (keep changes staged)
git reset --mixed HEAD~1                # Undo commit (unstage changes)
git reset --hard HEAD~1                 # Undo everything (destructive)

git revert <commit-id>                  # Safely undo a commit by creating a new one
git reflog                              # Show history of HEAD changes
git checkout -- <file>                  # Discard local changes to a file

Enter fullscreen mode Exit fullscreen mode

πŸ’ 11. Rebase & Cherry-pick


git rebase <branch>                     # Rebase current branch onto another
git rebase -i HEAD~3                    # Interactive rebase last 3 commits
git cherry-pick <commit-id>            # Apply specific commit to current branch

Enter fullscreen mode Exit fullscreen mode

πŸ”– 12. Tagging

git tag                                 # List tags
git tag <tagname>                       # Create lightweight tag
git tag -a <tagname> -m "msg"           # Annotated tag
git push origin <tagname>               # Push tag to remote
git tag -d <tagname>                    # Delete local tag
git push origin --delete <tagname>      # Delete remote tag

Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 13. Aliases


git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm 'commit -m'
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'

Enter fullscreen mode Exit fullscreen mode

Then you can use


git co main
git br
git cm "Fast commit"

Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 14. Workflow (Example)

mkdir test-repo
cd test-repo
git init
echo "Hello Git" > index.txt
git add .
git commit -m "Initial commit"
git branch dev
git checkout dev
echo "Working on dev" >> index.txt
git commit -am "Update on dev"
git checkout main
git merge dev

Enter fullscreen mode Exit fullscreen mode

🧾 Summary

Command Description
git init Initializes a new Git repository in the current directory.
git config --global user.name "Your Name" Sets your Git username globally.
git config --global user.email "you@example.com" Sets your Git email globally.
git status Shows the status of changes (tracked, untracked, staged, etc.).
git add <file> Stages a specific file for commit.
git add . Stages all modified and new files in the current directory.
git commit -m "message" Commits staged files with a message.
git log Shows the commit history.
git clone <repo-url> Clones a repository from GitHub or other remote.
git remote -v Shows configured remote repositories.
git push Pushes commits to the remote repository.
git pull Fetches and merges updates from the remote.
git diff Shows the difference between working directory and last commit.

Top comments (0)