**Introduction to Git
**Git is a powerful distributed version control system that lets you track changes, collaborate seamlessly, and maintain a robust project history. Whether you're new to Git or an experienced developer, this cheatsheet provides quick, essential references for both basic and advanced Git commands to enhance your workflow.
Basic Git Commands
Initialization
Initialize a new Git repository in your current directory:
git initThis command creates a hidden
.git
directory that starts tracking your project.
Cloning a Repository
Clone an existing repository from a remote source:
git cloneThis command downloads the repository to your local machine.
Staging Changes
Add files or directories to the staging area:
git addStage all modified files:
git add .Committing Changes
Commit your staged changes with a message describing the update:
git commit -m "Your commit message"Checking Repository Status
See the current state of your repository, including staged, modified, and untracked files:
git statusViewing Differences
View changes between your working directory and the staging area:
git diffSee differences between the staging area and the last commit:
`
git diff --cached
`
Branching
Manage branches to work on multiple features simultaneously:
Create a new branch:
git branchSwitch to an existing branch:
git checkoutCreate and switch to a new branch in one step:
git checkout -bMerge a branch into the current branch:
git mergePushing and Pulling Changes
Push your commits to a remote repository:
git pushPull updates from the remote repository to your local branch:
git pullViewing Commit History
List the commit history to review past changes:
git logAdvanced Git Commands
Reverting Changes
Undo changes by reverting a specific commit. This creates a new commit that reverses the changes:
git revertResetting Commits
Undo commits with different levels of rollback:
Soft reset (keeps changes staged):
git reset --soft HEAD~1Mixed reset (unstages changes):
git reset --mixed HEAD~1Hard reset (discards all changes):
git reset --hard HEAD~1Rebasing
Reapply your commits on top of another branch:
git rebaseStashing Changes
Temporarily save uncommitted changes to work on something else:
Stash changes:
git stashReapply stashed changes:
git stash applyCherry-Picking
Apply a specific commit from one branch onto the current branch:
git cherry-pickGit Hooks
Customize Git's behavior with hooks. Place executable scripts in the .git/hooks directory (e.g., pre-commit, post-merge) to run custom actions before or after Git events.
Git Aliases
Create shortcuts for commonly used Git commands:
git config --global alias. ''Example:
git config --global alias.co 'checkout'Git Workflows
Adopt one of these common workflows to manage your project:
Centralized Workflow: Single main branch for direct commits.
Feature Branch Workflow: Separate branches for new features or fixes, merged back when complete.
Gitflow Workflow: Structured branching model with dedicated branches for development (develop), releases (release), and features.
Conclusion
Mastering Git is essential for effective version control and collaboration. Use this cheatsheet as your quick reference guide to become more proficient with Git, streamline your workflow, and manage your projects with confidence.
Connect with me on LinkedIn and follow my work on GitHub.
Happy coding!