Welcome back to our beginner-friendly Git series! 👋
In EP02, we created a new Git project from scratch using git init
, made our first commit, and pushed it to GitHub.
This time, we’ll take the next step:
Cloning an existing repository and contributing with a new file.
We'll also write a simple Python script (main.py) to get hands-on with tracking changes.
What Is git clone?
git clone
is how you copy a Git project from GitHub to your local machine.
It’s not just a download — it includes:
- The project files
- Entire commit history
- All branches and configuration
Think of it as copying both the book and its full footnotes.
Step-by-Step: Cloning and Committing to a Repo
1. Clone the Repository
Let’s use the repo we created in EP02.
On GitHub, click the green “Code” button and copy the URL.
In your terminal:
git clone <remote-url>
Replace the with the one you copied from GitHub. For example:
git clone https://github.com/Mirrorsan/git-basics.git
It will show something like:
Next, enter the repo that we just clone with:
# List the contents of the current directory
dir
# Change into the cloned repository directory
cd git-basics
You now have a fully functional local copy!
2. Check Available Branches
Let’s see which branches already exist:
git branch -a
This command lists:
- Local branches (e.g. main)
- Remote branches (e.g. remotes/origin/develop)
Some projects use main, others use develop. Always check before switching!
3. Switch to the develop Branch
Since develop
exists remotely, you can switch to it directly:
git checkout develop
Git will automatically:
- Create a local develop branch
- Link it to origin/develop
- And check it out
Once you're on develop, make sure you're up to date:
git pull
It's a good habit to regularly run git pull to ensure your local branch is up to date with the remote.
4. Create a Feature Branch
Now let’s create a dedicated branch for the new feature — a small Python greeting script:
git checkout -b feature/add-greeting-script
This gives you a clean space to work on without affecting the develop branch directly.
5. Create the Python File (main.py
)
Create a file named main.py
, open it in your code editor, and add the following code:
# main.py
def greet(name):
return f"Hello, {name}! Welcome to your Git journey."
if __name__ == "__main__":
user = input("Enter your name: ")
print(greet(user))
This script:
- Asks for a name
- Returns a personalized message
- Is perfect for beginners to test out code and version control
6. Check Git Status (Before Staging)
Let’s see what Git thinks:
git status
You should see:
Red indicates that Git has detected the file, but it’s not being tracked yet.
7. Stage the File
Now let’s tell Git we want to include this file in the next commit:
git add main.py
8. Check Git Status (After Staging)
Now the file will appear in green:
git status
This means it’s staged and ready to be committed.
9. Commit Using Conventional Commits
Let’s commit using the Conventional Commits format:
git commit -m "feat: add greeting script in main.py"
But this time I will use this:
git commit -m "feat(main): add greeting script in main.py"
This makes it easy to understand what the commit does — and helps tools like changelogs and CI systems later.
10. Push Your Feature Branch to GitHub
Push your new branch to GitHub so others (or future you) can review it:
git push -u origin feature/add-greeting-script
The -u flag sets the upstream so you can just type git push next time.
Bonus: Run Your Python Script
Let’s test your new code!
python main.py
You'll see:
Enter your name:
Type your name and then the output:
Hello, Booranasak! Welcome to your Git journey.
Welcome to our Git journey — let's strive forward together!
Top comments (0)