Introduction to Git
Git is a powerful distributed version control system that enables developers to track code changes, collaborate with others, and manage project versions efficiently. Whether you’re a beginner or an experienced coder, mastering Git can significantly improve your productivity. Here is the ultimate Git cheat sheet to help you with everyday Git commands, tips, and workflows.
Getting Started with Git
To begin using Git, you’ll first need to install it. Here’s a quick guide to getting started:
- Install Git: Download Git here and follow the installation instructions for your operating system.
- Configure Git:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
Basic Git Commands
Create a New Repository
git init
Initializes a new Git repository in the current directory.
Clone a Repository
git clone <repository-url>
Check Repository Status
git status
Shows the status of changes as untracked, modified, or staged.
Stage Changes
git add <filename>
Stages a specific file
git add .
Stages all changes.
Commit changes
git commit -m "Your commit message"
Commits the staged changes to the repository with a descriptive message.
View Commit History
git log
Branching and Merging
Create a New Branch
git branch <branch-name>
Switch to a Branch
git checkout <branch-name>
Switches to the specified branch.
Create and Switch to a New Branch
git checkout -b <branch-name>
Creates a new branch and switches to it.
List all Branches
git branch -a
Merge a Branch
git merge <branch-name>
Merges the specified branch into the current branch.
Delete a Branch
git branch -d <branch-name>
Deletes the specified branch.
Remote Repositories
View Remote Repositories
git remote -v
Displays the list of remote repositories.
Add a Remote Repository
git remote add <name> <url>
Adds a new remote repository.
Fetch Changes from Remote
git fetch <remote>
Downloads changes from the remote repository but does not merge them.
Pull Changes from Remote
git pull <remote> <branch>
Fetches and merges changes from the specified branch.
Push Changes to Remote
git push <remote> <branch>
Pushes committed changes to the specified branch on the remote repository.
Delete Remote Branch
git push -d <remote> <branch>
Advanced Git Commands Stash Changes
Stash Changes
git stash
Temporarily saves changes and reverts the working directory to a clean state.
Apply Stashed Changes
git stash apply
Applies the most recent stash.
Revert a Commit
git revert <commit-hash>
Creates a new commit that undoes the changes from the specified commit.
Reset to a Specific Commit
git reset --hard <commit-hash>
Resets the current branch to the specified commit and discards all changes after it.
Git Best Practices
- Write Descriptive Commit Messages: Clearly describe what each commit does for easy navigation of project history.
- Keep Branches Small and Focused: Work on a single feature or bug fix per branch to simplify collaboration.
- Regularly Pull Changes: Keep your local repository up to date with the latest changes from the remote repository.
- Review Changes Before Committing: Use
git diff
to see what has changed.
Conclusion
Mastering Git commands can greatly improve your workflow, collaboration, and version control. Keep this Git cheat sheet handy to quickly reference essential commands.