**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 init
This command creates a hidden
.git
directory that starts tracking your project.
Cloning a Repository
Clone an existing repository from a remote source:
git clone <repository_url>
This command downloads the repository to your local machine.
Staging Changes
Add files or directories to the staging area:
git add <file_or_directory>
Stage 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 status
Viewing Differences
View changes between your working directory and the staging area:
git diff
See 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 branch <branch_name>
Switch to an existing branch:
git checkout <branch_name>
Create and switch to a new branch in one step:
git checkout -b <branch_name>
Merge a branch into the current branch:
git merge <branch_name>
Pushing and Pulling Changes
Push your commits to a remote repository:
git push <remote> <branch>
Pull updates from the remote repository to your local branch:
git pull <remote> <branch>
Viewing Commit History
List the commit history to review past changes:
git log
Advanced Git Commands
Reverting Changes
Undo changes by reverting a specific commit. This creates a new commit that reverses the changes:
git revert <commit_hash>
Resetting Commits
Undo commits with different levels of rollback:
Soft reset (keeps changes staged):
git reset --soft HEAD~1
Mixed reset (unstages changes):
git reset --mixed HEAD~1
Hard reset (discards all changes):
git reset --hard HEAD~1
Rebasing
Reapply your commits on top of another branch:
git rebase <branch_name>
Stashing Changes
Temporarily save uncommitted changes to work on something else:
Stash changes:
git stash
Reapply stashed changes:
git stash apply
Cherry-Picking
Apply a specific commit from one branch onto the current branch:
git cherry-pick <commit_hash>
Git 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.<alias_name> '<command>'
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!
Tidak ada komentar:
Posting Komentar