Getting Started with Git
Installing Git
Git is available for all major operating systems. Here's how to install it:
1. Windows: Download the Git installer from git-scm.com and follow the installation wizard. During setup, you can configure options like line ending conversions and the default editor.
2. macOS: You can install Git through Homebrew with:
brew install git
Alternatively, Git is included when you install Xcode Command Line Tools:
xcode-select --install
3. Linux: Use your distribution’s package manager:
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install git
Fedora:
sudo dnf install git
After installation, configure Git with your personal details so your commits are properly attributed:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
You can also set a default text editor (e.g., Vim, Nano, VS Code) for Git to use when writing commit messages:
git config --global core.editor "code --wait"
(Replace "code --wait" with your preferred editor.)
To check your current configuration:
git config --list
Setting Up a Basic .gitignore File
A .gitignore file tells Git which files and directories to ignore, helping you avoid tracking unnecessary files like logs, builds, or environment settings.
Create a .gitignore file in the root of your project and add patterns for files you want Git to skip. Example:
# Ignore Python cache
__pycache__/
*.pyc
# Ignore node modules
node_modules/
# Ignore environment variables
.env
You can also find pre-built .gitignore templates for specific languages and frameworks at gitignore.io.
Core Git Command Line Commands
1. Repository Initialization and Setup
git init
touch README.md
git add README.md
git commit -m "Add project README
git clone https://github.com/user/project.git
2. Tracking and Saving Changes
git status: Displays the status of your working directory and staging area, which files are untracked, modified, or staged for commit.
git status
git add index.html
git add .
git commit -m "Add new homepage layout"
git commit --amend: This lets you revise the last commit without creating a new one, keeping your history clean. If you need to modify the most recent commit, such as correcting a message or adding a missed file, you can use;
git commit --amend
3. Branching and Merging
git branch
git checkout feature-login
git checkout -b feature-signup
git merge feature-login
4. Working with Remote Repositories
git remote -v
git push origin main
git pull origin main
5. Undoing and Fixing Mistakes
git reset HEAD index.html
The git reset command has multiple modes:
--soft: Keeps your changes staged.
--mixed: Unstages the changes but keeps them in your working directory (default).
--hard: Discards all changes and resets the working directory to match the last commit.
Example: git reset --hard HEAD~1
This command discards the last commit and all changes; use it with caution.
git revert 4a5e6f7
git checkout -- index.html
6. Viewing Project History
git log
git log --oneline
git diff git diff main feature-login
Git Commands to Boost Productivity
1. git stash and git stash pop
Sometimes you are in the middle of making changes but need to switch tasks quickly without committing incomplete work. Using git stash saves your uncommitted changes and resets your working directory to the last commit, allowing you to focus on something else.
When you are ready to continue, git stash pop reapplies those changes, making it ideal for multitasking or handling urgent fixes. Alternatively, use git stash apply if you want to reapply the stashed changes without removing them from the stash list.
2. git cherry-pick [commit]
When you want to apply a specific commit from one branch onto another without merging the entire branch, you can use git cherry-pick. For example, running git cherry-pick 4a5e6f7 applies that specific commit to your current branch.
This command is especially useful for selectively moving bug fixes or features without including unrelated changes.
3. git rebase -i
Use interactive rebase to clean up commit history, squash commits, or change commit messages before pushing:
git rebase -i HEAD~3
Setting Up Git Aliases
Typing long Git commands repeatedly can slow you down. Git lets you set up aliases to shorten common commands:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
After setting these, you can use git co, git br, git ci, and git st instead of the full commands. This small tweak can save you significant time over hundreds of operations.
To see all your aliases and configurations:
git config --global --list
Best Practices for Using Git Command Line
1. Write Clear and Descriptive Commit Messages
Good commit messages help others and your future self quickly understand the purpose of each change by starting with a summary line of 50 characters or less. If the change is complex, you can add a brief explanation for context.
Using the present tense, such as “Add login feature” instead of “Added login feature,” keeps messages consistent and action-oriented. Clear, well-structured commits improve collaboration and make debugging faster and more efficient.
2. Commit Often but Logically
Frequent commits create a detailed project history that makes it easier to track progress and identify issues as they arise. Instead of committing every minor edit, group related changes into logical commits that represent a single purpose, feature, or fix.
3. Regularly Pull from Remote Before Pushing
Before pushing your work, always pull the latest changes from the remote repository using git pull origin [your-branch]. This keeps you aligned with your team’s updates and helps prevent merge conflicts.
4. Keep Branches Focused and Small
Use branches for specific features, fixes, or experiments rather than combining unrelated changes into one. Small, focused branches make code reviews faster, reduce merge conflicts, and keep the project history clean and easy to follow. Once the work is complete, promptly merge the branch and delete it if it's no longer needed.
From initializing repositories and committing changes to branching, merging, and advanced techniques like rebasing or stashing, Git provides powerful tools for efficient version control. With the right command-line skills, you can streamline development, reduce errors, and collaborate more effectively on any project.