↓ Skip to main content

Git Command Cheat Sheet: Essential Workflow and Operations

Git Command Cheat Sheet: Essential Workflow and Operations

Git is a powerful, free, and open-source distributed version control system used widely for source code management and team collaboration.

This guide provides a clear and structured overview of essential Git concepts and commands to help streamline your development workflow.


🔄 Git Basic Workflow
#

Git operates across four key areas:

  • Working Directory
    Your active workspace where files are created and modified

  • Staging Area (Index)
    A temporary area that tracks changes before committing

  • Local Repository
    Stores commit history; HEAD points to the latest commit

  • Remote Repository
    Hosted platforms like GitHub or GitLab for collaboration

Understanding how changes flow between these layers is fundamental to mastering Git.


🧰 1. Core Git Operations
#

Initialize a Repository
#

Create a new Git repository in the current directory:

git init

Clone an Existing Repository
#

Download a remote repository to your local machine:

git clone <repository-url>

Check Status and Differences
#

Inspect the current state of your project:

git status

View changes between working directory and staging area:

git diff

Add and Commit Changes
#

Stage files:

git add <file>
git add .

Commit changes:

git commit -m "Describe your changes"

View Commit History
#

git log
git log --oneline

🌿 2. Branch Management
#

Action Command
Create branch git branch <branch-name>
Switch branch git checkout <branch-name>
Create & switch git checkout -b <branch-name>
Merge branch git merge <branch-name>
Delete branch git branch -d <branch-name>
Force delete git branch -D <branch-name>

Branches enable parallel development and safe experimentation.


🌐 3. Remote Repository Operations
#

  • View remotes git remote -v

  • Add remote git remote add <name> <url>

  • Push changes git push <remote> <branch>

  • Pull changes (fetch + merge) git pull <remote> <branch>

  • Fetch only (no merge) git fetch <remote>


🔁 4. Undo and Recovery Operations
#

Restore Working Directory
#

Discard local changes to a file:

git checkout -- <file>

Unstage Files
#

Remove a file from the staging area:

git reset HEAD <file>

Reset Commits
#

# Keep changes staged
git reset --soft <commit-hash>

# Discard all changes permanently
git reset --hard <commit-hash>

🏷️ 5. Tag Management
#

  • Create tag git tag <tag-name>

  • Annotated tag git tag -a <tag-name> -m "Description"

  • List tags git tag

  • Push tag git push <remote> <tag-name>

  • Delete tag

git tag -d <tag-name>
git push <remote> --delete <tag-name>

🛠️ 6. Practical Tips and Troubleshooting
#

Ignore Files with .gitignore
#

*.log
/tmp

Exclude unnecessary files from version control.


Use Git Aliases
#

Simplify frequent commands:

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch

Resolve Merge Conflicts
#

  1. Open conflicted files

  2. Edit and remove conflict markers:

    <<<<<<<
    =======
    >>>>>>>
  3. Stage and commit:

git add <file>
git commit

Recover Deleted Branches
#

git reflog
git checkout -b <branch-name> <commit-hash>

Safely Revert Remote Commits
#

git revert <commit-hash>
git push

Creates a new commit that undoes previous changes without rewriting history.


✅ Summary
#

Git is an essential tool for modern development. By mastering:

  • Core workflows
  • Branching strategies
  • Remote synchronization
  • Undo and recovery techniques

you can significantly improve both productivity and collaboration in any project.

Related