Top Git commands for beginners

lzhangstat
2 min readJun 19, 2023

--

Git is a super popular and useful version control tool. It’s almost a must-have skill for anyone who ever need to code. In this article, I will cover the top Git commands I use a lot with real scenarios examples. A more advanced tutorial can be found here.

Imagine that you just start your very first project with your team, how can you contribute to it?

Scenario 1. Your goal is to review the project and prepare for any upcoming tasks.

What you need to do: Create a local copy of the repository to explore its directories and files. Review the specifics of this repository.

git clone <Repository URL>
git remote show origin

Scenario 2. Having gained a basic understanding of the project, you’ve now been assigned a new task: adding feature xxxx.

What you need to do: When you create a branch from the current branch (typically the main branch), any changes you make in the new branch remain isolated. This approach allows you to work on different features simultaneously without them affecting each other. So create a new branch for your new task.

git checkout -b <branch_for_feature_xxx>

Scenario 3.1 You’ve employed a divide-and-conquer strategy for the task, and you’ve successfully completed the initial subtask.

What you need to do: Specify what files in your repository you want to save changes from and makes a permanent snapshot of the current state of them.

git add <file_to_save>
git commit -m"<This commit keeps track of all the changes to complete a subtask for feature xxx.>"

Scenario 3.2 After inadvertently causing chaos in the branch, you’re aiming to reset and start fresh.

What you need to do: Delete the current branch and create a new one. Ensure that you’re in a different branch before deleting the original.

git checkout <main branch>
git branch -D <branch_for_feature_xxx>

Scenario 3.3 You’ve made significant progress toward the goal, but an unexpected interruption has shifted your focus. Now, a teammate will take over your task. To ensure a smooth transition, share the work you’ve completed so far, sparing them from starting over.

What you need to do: push the current branch and set the remote as upstream. In this way, you teammate will be able to git clone from your branch (in remote).

git push - set-upstream origin <branch_for_feature_xxx>

Scenario 4. Great news! Everything has gone smoothly, and you’ve completed your development work. Now you’re all set to share it!

What you need to do: You want to merge the changes in your current branch to the main branch and push it to the remote repository so other people can access it.

git checkout <main branch>
git merge <branch_for_feature_xxx>
git pull # pull new changes from remote
git push # push your changes to remote

Congrats! Kudos for making your first contribution to the project!

--

--

lzhangstat
lzhangstat

Written by lzhangstat

Stat, math, machine learning and much more!

No responses yet