Git Cheat Sheet: Essential Commands for Version Control
Git is a powerful tool for version control, whether you’re working solo or collaborating with a team. This cheat sheet covers essential Git commands for creating snapshots, managing branches, viewing history, and collaborating on code.
Initializing a Repository and Staging Files
Creating a Git Repository
To start tracking a project with Git, initialize a repository:
1git init
Staging Files
Add files to the staging area, preparing them for commit: Stage a single file:
1git add filename.ts
Stage a multiple files:
1git add filename.ts filename.py
Use patterns to stage files:
1git add *.ts
Stage all files in the current directory:
1git add .
Committing Changes
Committing Staged Files
After staging, commit your changes.
Commit with a short message:
1git commit -m "Your message here"
For longer messages:
1git commit
Skipping the Staging Area
For quick commits without staging:
1git commit -am "Message"
Viewing Changes and File Status
Checking the Status of Files
Track the state of files:
1git status
Short status:
1git status -s
Viewing Changes
Show unstaged changes:
1git diff
Show staged changes:
1git diff --staged
Viewing History and Logs
Viewing Commit History
Full history:
1git log
Brief overview:
1git log --oneline
Reverse order, from oldest to newest:
1git log --reverse
Viewing Specific Commits
Show the latest commit:
1git show HEAD
Go back two commits:
1git show HEAD~2
Undoing Changes
Unstaging Files
If you added files by mistake:
1git restore --staged filename.ts
Discarding Local Changes
Revert local changes to match the last commit: Discard changes in a single file:
1git restore filename.ts
Discard changes in the entire directory:
1git restore .
Branching and Merging
Creating and Switching Branches
Create a new branch:
1git branch branch_name
Switch to an existing branch:
1git switch branch_name
Merging Branches
To merge a branch into your current branch:
1git merge branch_name
Stashing Changes
Save changes temporarily with a stash:
1git stash push -m "Stash description"
List all stashes:
1git stash list
To apply a specific stash:
1git stash apply stash@{1}
Working with Remote Repositories
Cloning Repositories
Clone a remote repository:
1git clone [repository-url]
Fetching and Pulling Changes
Fetch updates without merging:
1git fetch origin
Fetch and merge changes:
1git pull
Pushing to Remote Repositories
To push your changes:
1git push origin branch_name
Managing Remote Repositories
Adding and Removing Remotes
Add a new remote:
1git remote add upstream [url]
Remove a remote:
1git remote rm upstream
Advanced Usage
Rebasing
Change the base of the current branch:
1git rebase branch_name
Cherry-picking
Apply changes from a specific commit:
1git cherry-pick commit_hash
Interactive Rebase
Reorder, combine, or edit previous commits:
1git rebase -i HEAD~99
Recovering Lost Commits
To see the history of HEAD:
1git reflog
Conclusion
This cheat sheet highlights core Git commands for version control. By mastering these commands, you’ll be able to confidently create, manage, and collaborate on code projects.