Github

Git Cheatsheet

Git is a distributed version control system for tracking changes in code. Master these commands for efficient collaboration and version management.

Git is a distributed version control system for tracking changes in source code. It enables collaboration, version history, branching, and merging — essential tools for modern software development.

Initialization

Start tracking a directory with Git.

git init                    # Initialize a new repository
git clone <url>             # Clone an existing repository
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Basic Workflow

Stage, commit, and check status.

git status                   # Check repository status
git add <file>              # Stage a file
git add .                    # Stage all changes
git commit -m "Message"     # Commit with message
git log                      # View commit history

Branching

Create and manage branches.

git branch                   # List branches
git branch <name>            # Create new branch
git checkout <branch>        # Switch to branch
git checkout -b <name>       # Create and switch
git branch -d <name>         # Delete branch

Merging

Combine branches together.

git merge <branch>           # Merge branch into current
git merge --no-ff <branch>   # Force merge commit
git merge --abort            # Cancel merge in progress

Remote Repositories

Work with remote repositories.

git remote add origin <url>  # Add remote
git remote -v                # List remotes
git push origin <branch>     # Push to remote
git pull origin <branch>     # Pull from remote
git fetch origin             # Fetch without merging

Undoing Changes

Revert changes at different stages.

git restore <file>           # Discard working directory changes
git restore --staged <file>  # Unstage file
git reset HEAD~1             # Undo last commit (keep changes)
git reset --hard HEAD~1      # Undo last commit (discard changes)
git revert <commit>         # Create new commit that undoes changes

Viewing History

Inspect commit history and changes.

git log                      # View commits
git log --oneline            # Compact view
git log --graph              # Show branch graph
git show <commit>            # Show commit details
git diff                     # Show unstaged changes
git diff --staged            # Show staged changes
git diff <commit1> <commit2> # Compare commits

Stashing

Temporarily save changes without committing.

git stash                    # Save changes temporarily
git stash list               # List stashes
git stash pop                # Apply and remove stash
git stash apply              # Apply stash (keep it)
git stash drop               # Delete stash

Tagging

Mark important points in history.

git tag <name>               # Create lightweight tag
git tag -a <name> -m "Msg"  # Create annotated tag
git tag                      # List tags
git push origin <tag>        # Push tag to remote
git push --tags              # Push all tags

Rebasing

Reapply commits on top of another base.

git rebase <branch>          # Rebase current branch
git rebase -i HEAD~3         # Interactive rebase (last 3 commits)
git rebase --abort            # Cancel rebase
git rebase --continue        # Continue after resolving conflicts

Submodules

Include other Git repositories.

git submodule add <url>      # Add submodule
git submodule update         # Update submodules
git submodule init           # Initialize submodules

Useful Aliases

Create shortcuts for common commands.

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

Ignoring Files

Use .gitignore to exclude files.

# .gitignore
dist/
node_modules/
.env
*.log
.DS_Store

Git is essential for collaboration and version control. Master the basics, then explore advanced features like rebasing and submodules.

For full documentation, see https://git-scm.com/docs

Promote your content

Reach over 400,000 developers and grow your brand.

Join our developer community

Hang out with over 4,500 developers and share your knowledge.