Git Crash Course for Beginners: Commands, Workflow & Terminology
If you’re new to Git, you’re about to discover one of the most powerful tools in a developer’s toolkit. Git allows you to track changes, collaborate with others, and maintain different versions of your project. This comprehensive guide will take you through everything you need to know to get started with Git on Windows.
Setting Up Git
If you’ve already downloaded Git (which you mentioned you have), you’re one step ahead! Now let’s configure your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Run these commands once when you first install Git. They’ll associate your name and email with every commit you make.
Starting a Project
You have two options when starting with Git:
Create a New Repository
mkdir my-project
cd my-project
git init
This creates a new folder and initializes it as a Git repository.
Clone an Existing Repository
git clone https://github.com/username/repository-name.git
cd repository-name
This downloads an existing project and its entire version history.
The Daily Git Workflow
Here’s what a typical Git workflow looks like:
1. Check Status (Run This Frequently)
git status
This shows what files have changed, what’s staged, and what’s not. Consider this your dashboard—check it often to understand what’s happening in your repository.
2. Working with Changes
Adding Files to the Staging Area
git add filename.txt # Add a specific file
git add folder/ # Add an entire folder
git add . # Add all changes
Committing Your Changes
git commit -m "Clear description of your changes"
Quick Commit for Already Tracked Files
git commit -am "Description of changes"
3. Working with Remote Repositories
Push Your Changes to GitHub/GitLab/etc.
git push origin main
Get the Latest Changes
git pull origin main
See Remote Repository Information
git remote -v
Add a Remote Repository
git remote add origin https://github.com/username/repository-name.git
Branch Management
Branches let you work on different features or fixes simultaneously.
Create and Switch to a New Branch
git checkout -b feature-name
List All Branches
git branch # Local branches
git branch -a # All branches including remote
Merge a Branch
git checkout main # Switch to the target branch
git merge feature-name
Delete a Branch
git branch -d feature-name # Safe delete
git branch -D feature-name # Force delete
File Operations
Discard Changes to a File
git checkout -- filename.txt
Unstage a File
git restore --staged filename.txt
Delete a File and Stage the Removal
git rm filename.txt
Rename a File
git mv oldname.txt newname.txt
Viewing History
View Commit History
git log # Full history
git log --oneline # Compact history
git log --graph --oneline # Visual history
View Changes in a Specific Commit
git show commit-hash
See Changes Between Working Directory and Staging
git diff
See Staged Changes
git diff --staged
Fixing Mistakes
Amend the Last Commit
git commit --amend -m "New commit message"
Undo the Last Commit but Keep Changes
git reset HEAD~1
Completely Undo Last Commit and Changes
git reset --hard HEAD~1
Stash Changes Temporarily
git stash # Stash changes
git stash pop # Retrieve stashed changes
git stash list # List stashes
Working with VS Code
VS Code has excellent Git integration through its Source Control panel (Ctrl+Shift+G). You can:
- See modified files
- Stage changes with a click
- Commit with a message
- Push/pull with buttons
- View diffs visually
- Resolve merge conflicts
Real-World Workflows
Adding New Code
git status # See what's changed
git add . # Stage all changes
git commit -m "Add login feature" # Commit changes
git push origin main # Push to remote
Fixing a Bug
git checkout -b bugfix # Create a branch for your bugfix
# Make your changes
git add . # Stage changes
git commit -m "Fix login validation bug" # Commit
git checkout main # Switch back to main
git pull origin main # Get latest changes
git merge bugfix # Merge your bugfix
git push origin main # Push to remote
git branch -d bugfix # Delete the branch
Deleting Files
git rm obsolete-file.txt # Remove and stage deletion
git commit -m "Remove obsolete file" # Commit deletion
git push origin main # Push to remote
Handling Merge Conflicts
If a merge creates conflicts:
- Open conflicted files in VS Code
- Look for conflict markers (
<<<<<<
,=======
,>>>>>>>
) - Edit to resolve conflicts
- Save files
- Run:
git add . # Stage resolved files
git commit # Commit the merge
Git Terminology Glossary
Understanding Git’s terminology is crucial for mastering the tool. Here’s a comprehensive glossary:
Core Concepts
Repository (Repo) - A storage location for your project that contains all files, folders, and the complete history of changes.
Working Directory - The files that you’re currently working with on your local machine.
Staging Area (Index) - A middle ground between your working directory and repository where changes are prepared before committing.
Commit - A snapshot of your repository at a specific point in time, including all tracked changes since the last commit.
Branch - An independent line of development that allows you to work on features or fixes without affecting the main codebase.
Main/Master - The default primary branch in your repository (traditionally called “master”, but “main” is now more common).
HEAD - A pointer to the latest commit in your current branch; essentially “where you are now” in the repository’s history.
Clone - A complete copy of a repository, including all files and history.
Fork - A personal copy of someone else’s repository stored on your GitHub account.
Actions & Operations
Stage - The act of preparing files for a commit by adding them to the staging area.
Commit - The action of saving staged changes to the repository history.
Push - Uploading commits from your local repository to a remote repository.
Pull - Downloading commits from a remote repository to your local repository and merging them.
Fetch - Downloading commits from a remote repository without automatically merging them.
Merge - Combining changes from one branch into another.
Rebase - An alternative to merging that rewrites commit history by moving a branch to a new base commit.
Checkout - Switching between branches or versions of files.
Stash - Temporarily storing uncommitted changes so you can work on something else.
Storage Locations
Local Repository - The Git repository stored on your computer.
Remote Repository - A Git repository stored on a server or service (like GitHub, GitLab, Bitbucket).
Origin - The default name Git gives to the remote repository from which you cloned.
Upstream - The original repository that you forked from (commonly used in open-source projects).
Connection & Synchronization
Remote - A connection to another repository, usually on a server.
Tracking Branch - A local branch that has a direct relationship to a remote branch.
Upstream Branch - The remote branch that your local branch is tracking.
Changes & Differences
Modified - Files that have been changed but not yet staged.
Staged - Files that have been added to the staging area and are ready to be committed.
Untracked - Files in your working directory that Git isn’t monitoring.
Diff - The difference between two versions of a file or sets of files.
Patch - A file containing changes between versions that can be applied to another codebase.
Conflict Resolution
Merge Conflict - Occurs when Git cannot automatically resolve differences between two sets of changes.
Conflict Markers - Special notation (<<<<<<<
, =======
, >>>>>>>
) inserted by Git to show conflicting sections in files.
Resolution - The process of manually editing files to resolve conflicts between different versions.
Version References
Hash/SHA - A unique identifier (e.g., a1b2c3d4...
) for each commit in Git.
Tag - A named pointer to a specific commit, typically used for release versions.
HEAD~n - A relative reference to the nth commit before HEAD.
Advanced Concepts
Detached HEAD - A state where you’re viewing a specific commit rather than a branch.
Cherry-pick - Applying a specific commit from one branch to another.
Submodule - A repository embedded within another repository.
Gitignore - A file that tells Git which files or folders to ignore in a project.
Hook - Scripts that run automatically at certain points in Git’s execution.
Reflog - A log of where your HEAD and branches have been.
Fast-forward - A simplified merge that occurs when the target branch hasn’t changed since the feature branch was created.
Practical Terminology
Pull Request (PR) - On platforms like GitHub, a request to merge changes from one branch to another, usually with code review.
Code Review - The process of examining code changes before they’re merged.
Branch Protection - Rules that prevent certain actions on important branches without proper authorization.
Continuous Integration (CI) - Automated testing that runs whenever code is pushed to a repository.
Continuous Deployment (CD) - Automated deployment of code that passes CI tests.
Conclusion
Git can seem intimidating at first, but with practice, these commands and concepts will become second nature. The key is to use Git regularly, starting with simple operations and gradually incorporating more advanced features into your workflow.
Remember, Git is designed to be a safety net for your code. Don’t be afraid to experiment—you can always recover your work if something goes wrong. Happy coding!