Table of Contents
- What is Git?
- Basic Git Setup
- Core Git Commands
- Branching in Git
- Working with Remotes
- Handling Merge Conflicts
- Git Log & History
- Stashing Changes
- Rebasing & Reset
- Working with Tags
- Git Ignore & Clean
- Managing Multiple GitHub Accounts
- Automation with Git Scripts
- Markdown Cheatsheet
- Git Pro Tips
What is Git?
Git is a distributed version control system that allows developers to track changes in source code over time. It enables collaborative work by allowing multiple developers to work on the same project without interfering with each other’s progress.
Basic Git Setup
Before using Git, you should configure your Git environment. Here's how:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
-
git config --global user.name
: Set your name. -
git config --global user.email
: Set your email address. -
git config --global init.defaultBranch main
: Set default branch name asmain
. -
git config --global core.editor "code --wait"
: Set default editor to Visual Studio Code.
Core Git Commands
Here are some essential Git commands for everyday use:
git init # Initialize a new Git repository
git clone # Clone a repository
git status # Show the current status of your working directory
git add . # Stage all changes
git commit -m "Message" # Commit changes
git push # Push to remote repository
git pull # Pull from remote repository
git fetch # Fetch changes from the remote
git log # Show commit history
Branching in Git
Branching allows you to work on separate features without interfering with the main project. Here are some basic branch commands:
git branch # List all branches
git branch # Create a new branch
git checkout # Switch to a branch
git checkout -b # Create and switch to a new branch
git merge # Merge another branch into the current branch
Working with Remotes
Git allows you to manage different versions of your project across multiple locations (remotes). Here's how to work with remotes:
git remote -v # List the remote repositories
git remote add origin # Add a remote repository
git push origin main # Push changes to the origin remote
git push --set-upstream origin feature # Track a branch with remote
Handling Merge Conflicts
Merge conflicts happen when two developers make changes to the same part of a file. To resolve:
- Pull the latest changes:
git pull
- Git will mark conflicts in the files. Edit the conflicted files, then:
git add .
git commit -m "Resolved merge conflicts"
Git Log & History
Use git log
to view your commit history. You can view detailed logs and make them easier to read:
git log # Show commit history
git log --oneline # Show commits as one line per commit
git log --graph --all # Visualize the commit history with a graph
Stashing Changes
If you need to switch branches but aren’t ready to commit your changes, use git stash
:
git stash # Save uncommitted changes
git stash pop # Apply the last stashed changes
git stash list # View stashed changes
git stash drop 0 # Drop the first stash
Rebasing & Reset
Rebase and reset commands help you modify your commit history:
git rebase main # Rebase your current branch onto the main branch
git reset --hard HEAD~1 # Remove the last commit (hard reset)
git reset --soft HEAD~1 # Remove the last commit but keep changes (soft reset)
Working with Tags
Tags are useful for marking specific points in your project’s history:
git tag v1.0 # Create a tag
git tag # List all tags
git push origin v1.0 # Push a tag to the remote repository
git push origin --tags # Push all tags
Git Ignore & Clean
To avoid tracking unnecessary files, use .gitignore
:
Example .gitignore
:
node_modules/
.env
*.log
To clean up untracked files:
git clean -fd
Managing Multiple GitHub Accounts
1. Generate SSH Keys
Generate unique SSH keys for different accounts:
ssh-keygen -t rsa -C "email1@example.com" -f ~/.ssh/id_rsa_personal
ssh-keygen -t rsa -C "email2@example.com" -f ~/.ssh/id_rsa_work
2. Edit ~/.ssh/config
Configure SSH to manage multiple accounts:
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
3. Add Remotes for Different Accounts
git remote add personal git@github.com-personal:your/repo.git
git remote add work git@github.com-work:your/repo.git
4. Push to Either or Both
You can now push to different remotes:
git push personal main
git push work main
Create an alias to push to both:
git config alias.pushall '!git push personal && git push work'
git pushall
Automation with Git Scripts
You can automate common tasks with shell scripts. Here’s an example script to push to all remotes:
#!/bin/bash
branches=$(git branch --show-current)
git push personal $branches
git push work $branches
git push origin $branches
Make it executable:
chmod +x git-multi-push.sh
./git-multi-push.sh
Markdown Cheatsheet
Here’s a quick reference for Markdown formatting:
# Heading 1
## Heading 2
**Bold** *Italic* `inline`
- List item
[Link](https://example.com)
Git Pro Tips
Here are some Git pro tips for optimizing your workflow:
- Use
git reflog
to recover lost commits. - Use
git bisect
for debugging bad commits. - Configure
.gitattributes
for handling cross-platform line endings. - Set up pre-commit hooks to automate tasks like linting.
Final Thoughts
By mastering Git, you can significantly improve your workflow and collaboration with other developers. This tutorial covers everything from basic Git commands to advanced configuration and automation, helping you become more efficient with version control.
Happy coding!