Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
The objective of this article is to help you learning Git through an example project. In short, this article contains a step-by-step, example project that shows how to create a Git repository from scratch, make the first commit, navigate around the repo, and perform fundamental operations like staging, committing, branching, and merging. All commands are annotated with explanations. You can copy, paste, and adapt this guide as a helper during your Git adventures!.
In this tutorial you’ll learn:
git init
).git add
).git commit
).git log
) and differences (git diff
).By the end of it, you should have a solid grasp of the Git workflow: Make changes → Stage → Commit → (Optionally) Branch → Merge → Push.
For a more traditional tutorial see: Git tutorial: Version control from the ground up.
# 1. Create a new folder for your sample project
mkdir my-sample-project
# 2. Navigate into the folder
cd my-sample-project
# 3. Initialize a Git repository
git init
Explanations:
my-sample-project
..git
directory.
# 1. Create a simple text file (you can use echo, a code editor, or any other method)
echo "Hello, Git!" > hello.txt
# 2. Check the status of your repo (you'll see hello.txt as 'untracked')
git status
# 3. Stage the new file for a commit
git add hello.txt
# 4. Commit your staged changes with a short message
git commit -m "Add hello.txt with a greeting"
Explanations:
hello.txt
containing the text “Hello, Git!”.hello.txt
in the next commit.
# 1. View the commit history (so far, you only have one commit)
git log
# 2. See a one-line-per-commit summary (useful for quick reference)
git log --oneline
# 3. Check the status to confirm there are no uncommitted changes
Explanations:
# 1. Append a line to hello.txt
echo "This is our sample project to learn Git." >> hello.txt
# 2. See what's changed
git diff
# 3. Stage your changes
git add hello.txt
# 4. Commit the changes
git commit -m "Update hello.txt with a second line"
Explanations:
hello.txt
(using >>
).
# 1. Create a new branch for experimenting
git branch feature-experiment
# 2. Switch to that new branch
git switch feature-experiment
# or, for older Git:
# git checkout feature-experiment
# 3. Confirm which branch you're on
git branch
Explanations:
feature-experiment
.
# 1. Modify hello.txt again
echo "This line only exists in the 'feature-experiment' branch." >> hello.txt
# 2. Check status and diff
git status
git diff
# 3. Stage and commit changes
git add hello.txt
git commit -m "Add branch-specific line to hello.txt"
Explanations:
feature-experiment
branch, so any commits you make here won’t appear in master
or main
until you merge.
git switch master
feature-experiment
into master
:
git merge feature-experiment
git log --oneline --graph --decorate
git status
git branch -d feature-experiment
master
.feature-experiment
into master
.
# 1. Create a lightweight tag
git tag v0.1
# 2. Or create an annotated tag with a message
git tag -a v0.1 -m "First demo version"
# 3. List all tags
git tag
Explanations:
# 1. Link your local repo to a remote (e.g., GitHub)
git remote add origin https://github.com/YourUsername/my-sample-project.git
# 2. Push your commits to the remote
git push -u origin master
# 3. Push other branches if desired
git push -u origin feature-experiment
After these steps, your my-sample-project folder might look like this:
my-sample-project/
├── .git/ (Hidden folder containing Git metadata)
├── hello.txt (Our sample file)
If you’re brand new to Git, you now have a solid foundation to keep learning and experimenting!, and you can do just that by either going to:
Happy coding!