Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Whether you’re a software developer, a technical writer, or just collaborating on projects that involve text files, Git is an essential tool. In this guide, we’ll look at what Git is, why it matters, and how to get started using it. By the end, you’ll have a solid understanding of Git basics and an established workflow you can apply to any project.
The guide starts from the very beginning, what’s Git, how to install and configure it, and then it goes through all the Git core concepts, commands and usage.
Git is a distributed version control system (VCS) that tracks changes in files (usually source code) and helps coordinate work on those files among multiple people. It lets you:
brew install git
Verify installation by running:
git --version
Use your distro’s fave package manager.
# Debian/Ubuntu:
sudo apt-get update
sudo apt-get install git
# Fedora:
sudo dnf install git
# Arch:
sudo pacman -S git
Once installed, you’ll want to configure your identity and a few helpful settings. Note: it’s very important that you do this, otherwise you’ll not be able to commit your code and perform a lot of other equally important operations with Git.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
These commands tell Git who you are. The --global
flag makes them universal across your system.
git config --global color.ui auto
Enables color-coded Git output in the terminal.
A Git repository is a database that tracks file changes. It lives in a hidden folder named .git
at your project’s root.
This is the actual folder you see with your files. When you edit a file, you’re modifying the working directory.
The staging area (index) is where you place changes you want to include in your next commit.
A commit is a snapshot of changes in your repository. It includes the changes you staged plus a message describing them.
A remote is a copy of your repository hosted on a server (GitHub, GitLab, or any self-hosted solution). You push commits to and pull commits from the remote.
mkdir my_project
cd my_project
git init
git init
creates a .git
folder and makes this directory a repo.
git remote add origin https://github.com/username/my_project.git
Push your local commits to the remote:
git push -u origin master
The -u
sets origin master
as the default remote tracking branch.
git status
Shows which files are modified, untracked, or staged.
git add file1.txt
git add .
Use git add .
to stage all changes.
git commit -m "Add initial project setup"
Commits everything in the staging area with a message.
git push origin master
Sends local commits to the remote repository named “origin,” on the branch “master.”
git pull origin master
Brings down remote changes and merges them into your local branch.
git clone https://github.com/username/my_project.git
Makes a local copy of an existing remote repository.
Git’s branching system lets you develop features independently of the main codebase.
git branch new-feature
Creates a new branch called new-feature
.
Either command updates your working directory to reflect the new branch.
Older Git versions (before 2.23) use git checkout
to switch branches:
git checkout new-feature
Newer Git versions introduce git switch
:
git switch new-feature
git checkout master
Merge the new-feature
branch into master
:
git merge new-feature
Optionally delete the feature branch:
git branch -d new-feature
Sometimes Git can’t automatically merge changes to the same parts of a file. You’ll see conflict markers in the file(s) that need to be manually resolved:
<<<<<<< HEAD
Your changes here
=======
Changes from new-feature
>>>>>>> new-feature
After editing the file to resolve conflicts, do:
git add .
git commit
The merge is then complete.
git log
Shows the commit history. Press q to quit. For a concise view:
git log --oneline --graph --decorate
Displays differences between your working directory and the latest commit:
# Compare working directory changes to last commit
git diff
# Compare staging area changes to last commit
git diff --staged
Now that you have a basic grasp of Git, you can put your knowledge to the test by following an example project: From the very start of the repo → Make changes → Stage → Commit → (Optionally) Branch → Merge → Push.
You can access the example following this link: Learning Git from the start with an example project.
You can check our guide of Advanced Git Topics, these include: Stashing changes with git stash, Tagging releases with git tag, Cherry-picking with git cherry-pick, Undoing commits with git revert and git reset, following this link: Advanced Git topics and commands.
git diff
before committing to confirm your changes.Git can seem intimidating, but with practice it becomes second nature. Its ability to track changes, foster collaboration, and manage multiple developmetn paths is invaluable. By following this guide, you now know how to:
As you grow more comfortable, explore advanced concepts like submodules and continuous integration. Until then, keep practicing the essentials and enjoy the benefits of version control.
Feel free to use this tutorial as a springboard to help someone get started with Git. If you get stuck, refer back to these notes or check Git’s excellent official documentation at git-scm.com. Good luck, and have fun version-controlling your projects!