Mastering Git - A Beginner’s Guide to Essential Commands
Becoming proficient in Git is crucial for every developer using version control.
Git enables you to track changes, collaborate seamlessly with teams, and efficiently manage your codebase.
Core Git commands form the foundation of an efficient workflow. From git init
to initialize a repository to git merge
for integrating branches, understanding these commands can help you avoid unnecessary headaches.
Enhance your Git expertise and optimize your development workflow with these fundamental commands, all demonstrated within a single project: GitPractice!
Setting Up Your Project
Command: git init
The first step is to create a new project folder and initialize it as a Git repository. A repository (or “repo”) is a storage space where your project’s files and their version history are managed by Git.
mkdir GitPractice # Create the project folder
cd GitPractice # Navigate into it
git init # Initialize an empty Git repository in the project directory
What happens?
- A new hidden
.git
folder is created, and Git begins tracking changes in this directory. - You’ve set up your project for version control.
Command: git status
Check the current status of your repository.
The status of a repository shows which files have been modified, staged, or are yet to be tracked by Git.
git status
- Since the folder is empty, Git will report: “No commits yet” and “nothing to commit.”
Adding and Committing Changes
Command: git add
Create a file and add it to the staging area.
The staging area is a space where you prepare changes before committing them to the repository.
# Option 1: Create a file using echo
echo "Hello, Git!" > readme.md # Create a README file with initial content
# Option 2: Create a new file manually
touch readme.md # Create an empty README file in the directory
# Add unstaged files to git
git add readme.md # Stage the file for committing
What happens?
- The file
readme.md
is now staged, ready to be committed.
Command: git commit
Commit the staged file with a descriptive message.
A commit is a snapshot of your project’s current state. Each commit saves the changes made to your files along with a message describing the updates.
git commit -m "Add readme.md with initial content"
What happens?
- Git creates a snapshot of your project. This commit represents the first version of your repository.
Creating and Managing Branches
Command: git branch
Check your current branches and create a new one. A branch is a separate line of development in your repository. It allows you to work on new features or bug fixes without affecting the main codebase.
git branch # List branches (only 'main' or 'master' branch exists initially)
# Option 1: create and Switch to the new branch in 2 commands
git branch feature-1 # Create a new branch named 'feature-1'
git checkout feature-1 # Switch to the new branch
# Option 2: Create and switch in one step
git checkout -b feature-1
What happens?
- You’ve created a branch to work on a new feature without affecting the main branch.
Command: git merge
Merge changes from the feature-1
branch into main
.
Merging combines changes from one branch into another. It’s often used to integrate feature branches back into the main branch after development is complete.
# Make a change in the 'feature-1' branch
echo "This is a new feature." >> feature.txt # Append feature content to 'feature.txt'
git add feature.txt # Stage the changes
git commit -m "Add feature.txt with feature content" # Commit the changes
# Switch back to the main branch and merge the changes
# To check which branch the repository uses, run the following command:
git branch
git checkout main # Switch to the 'main' branch (commonly the default branch in newer repositories).
# If the repository uses 'master' instead of 'main', use the following command:
git checkout master
git merge feature-1 # Merge the changes from 'feature-1' into the current branch.
What happens?
- Changes from
feature-1
are now integrated intomain
ormaster
branch.
Simulating Collaboration
Command: git remote add origin <URL>
Link your local repository to a remote repository (e.g., on GitHub). A remote repository is a version of your project hosted on a server (e.g., GitHub or GitLab) that can be shared and synced with others.
git remote add origin https://github.com/your-username/GitPractice.git
What happens?
- Your local repository is now connected to a remote repository.
Command: git push
Push your changes to the remote repository.
Pushing uploads your commits from the local repository to a remote one, making them available to collaborators.
git push origin main # Push changes to the 'main' branch on the remote repository (use 'master' if that's the default branch).
What happens?
- The
main
branch is uploaded to GitHub, making it accessible to collaborators.
Command: git pull
Pull changes from the remote repository. Pulling fetches updates from a remote repository and merges them into your local branch.
git pull origin main
What happens?
- Updates from the remote
main
branch are fetched and merged into your local branch.
Exploring Changes
Command: git diff
View the differences between files or commits. The diff command shows the changes between two versions of a file, helping you understand what’s been modified.
# Make a change to readme.md
echo "Git is awesome!" >> readme.md # Append text to the 'readme.md' file
# View the differences
git diff # Show the differences between the current working directory and the last commit
# Example output from 'git diff':
# diff --git a/readme.md b/readme.md
# index 670a245..06c202b 100644
# --- a/readme.md
# +++ b/readme.md
# @@ -1 +1,2 @@
# Hello, Git!
# +Git is awesome!
What happens?
- Git highlights the lines added or modified in your file.
Command: git log
View the commit history. The log command displays a history of commits, showing details like commit hash, author, date, and message.
# View the commit history
git log # Show the commit history
# Example output from 'git log':
# commit 23279ecaa5d38849ffe5a31c8e1d76164e5493c3 (HEAD -> master, feature-1)
# Author: <author-name> <author-email>
# Date: Mon Jan 6 21:31:55 2025 +0100
#
# Add feature.txt with feature content
#
# commit 228087afa3b9f7c45e73d680604c144f72f5ed39
# Author: <author-name> <author-email>
# Date: Mon Jan 6 21:25:58 2025 +0100
#
# Add readme.md with initial content
What happens?
- You’ll see a list of all commits, including their hash, author, date, and message.
Undoing Changes
Command: git reset
Undo changes or unstage files.
The reset command allows you to move a file out of the staging area (i.e., unstage it) or undo commits, depending on the options used. Unstaging means that the changes are no longer marked to be included in the next commit, but the modifications remain in the working directory.
For example, running git reset <file>
will unstage a file, removing it from the staging area while keeping the changes you made to the file.
# Modify the README but don't want the changes
git reset readme.md
What happens?
- The file is removed from the staging area but remains in your working directory.
Command: git stash
Temporarily save changes to work on something else. The stash command saves your changes temporarily without committing them, allowing you to work on something else.
# Modify a file and stash the changes
echo "Temporary changes" >> temp.txt # Append temporary changes to 'temp.txt'
git stash # Stash the changes to save them temporarily
# Example output from 'git log':
# Saved working directory and index state WIP on master: 2379eca Add feature.txt with feature content
# Work on something else, then apply the stashed changes
git stash apply # Apply the stashed changes back to the working directory
What happens?
- Your changes are saved and restored later.
By following this guide, you’ve learned how to use Git commands within a single project, GitPractice, and understand key Git concepts like commits, branches, remotes, and more. Keep practicing, and soon Git will become second nature in your development workflow!