The Basics of Git For First-Time Users

XQ
The Research Nest
Published in
10 min readDec 25, 2022

A quick revision of some useful commands

Photo by Roman Synkevych 🇺🇦 on Unsplash

Things can look confusing for first-time users but trust me, the more you use these commands, the more seamless and easy it gets. It’s just like multiplication tables.

Creating your first repo and the first branch can feel intimidating, and you may wonder what those commands are and how you can remember them. By the time you create your 10th repo, all of it will become very natural and simple. Give it some time.

You can always refer to this article for a quick revision if you forget something. Git is where every aspiring software engineer must start their journey, even before they learn to code correctly. I recommend reading the entire article to get an overview and some tips. However, for quick reference, here’s a TL; DR.

Summary

  1. cd to navigate the file system from the command line (cmd, command prompt, anaconda prompt, PowerShell, and terminal are various forms of it)
  2. mkdir folder-name to create a new folder via the command line.
  3. git init -b main to initialize a new git repo with the main branch in a folder.
  4. git add . to add all files changed into tracked files to commit.
  5. git commit -m "message" to commit the changes made.
  6. gh repo create to create a new repo from your local command line in your GitHub account.
  7. git checkout -b branch-name to create a new branch in your repo.
  8. git push origin branch-name to push the changes into the remote repository on GitHub.
  9. git checkout branch-name to go to an existing branch in your repo.
  10. git pull to pull the latest changes from the remote repo into your local.
  11. git clone url to clone a remote repo creating a copy of it in your local.

Understanding what this is all about

Git is a version control system to track changes people are doing in a project. It is an excellent way to organize everything and help multiple people work and maintain the code asynchronously.

If you want to build a career in tech, knowing version control tools like Git is critical. There is practically no software that doesn’t use it.

Here’s how the ecosystem works.

  • A git repository is created for any project and hosted somewhere (like on GitHub)
  • People are given access to that repository, which they can “clone” (basically create a local copy on their computer) and make changes.
  • Developers create “branches” of the main repository to make changes in their local system.
  • Once they are ready, they push (as in, update/save) those changes to the remote repository (the code hosted on GitHub).
  • After that, a “pull request” (PR) is created to merge these changes from the developer-created branch into the main branch.
  • Other developers review these changes in the open PR; if all is good, they approve it to merge.

Let’s see how to do all of this as an absolute beginner. We need to install a few things to get started.

Once you understand Git and how to use the command prompt/PowerShell, you can make real-world contributions in open source, even as an absolute beginner.

Note that this tutorial covers only Windows systems. Start the tutorial after completing all the installations.

Step 0: Prerequisites

  1. You can use the command prompt (or PowerShell) to confirm that your git installation was successful.
  2. If this is your first time, search cmd in the search bar, and you will find the command prompt (also referred to as the terminal). You can also use PowerShell.
  3. Once you open it, type git -v and press Enter to check the version installed on your system.
  4. You will get an output like this. This tutorial uses the below version. You may use the latest version too. If you successfully get this output, git installation is a success.
    git version 2.38.1.windows.1
  5. Before you proceed further, you need to know about a few common commands used in the command prompt.
    - cd: This helps you navigate your file system in the command line. For example, if you are at C:\users\username\desktop\projects and type, cd .. you will come into the C:\users\username\desktop\ path. You can also chain this command to go up by multiple folders by using cd ../../.. and so forth. To go inside a folder present in the current directory, you can use cd folder-name. To go to a specific path, you can use cd path where you replace the path with the full path of the folder where you want to go.
    - mkdir folder-name: Use this to create a new folder in the current directory/path you are at right from your command line. Replace the folder name with any name you want to create.
  6. Let’s say you open the terminal, and by default, you are at C:\users\username. Do the below steps.
    - cd desktop : to go into the desktop path.
    - mkdir projects: to create a new projects folder inside the desktop folder.
    - cd projects: to go into this newly created folder.
    - mkdir hello-world: to create another folder inside projects where we will code our first GitHub repository.
    - cd hello-world.

Tip: Whenever you use these commands, you can press tab to autocomplete if possible.

Step 1: Create a git repository in your local project folder

  1. Once you are in your new folder (hello-world, in this case), you have to run the command git init -b mainto initialize a new git repository with a “main” branch.
  2. You will get an output like this in the terminal.
Initialized empty Git repository in C:/users/username/desktop/projects/hello-world/.git/

Tip: You can also create an empty git repository on GitHub and clone it into your folder (check below for what and how to clone).

Step 2: Create the files and add code in the git repository

  1. Use git status to check that repo is initialized and the branch name is main.
  2. Keep the terminal aside (keep it open in the background) and open the hello-world project folder in VS Code.
  3. In the explorer pane on the left, you will have the option to create a new file. It will appear once you hover over the project name in explorer. Create a new file named hello.py. You may install any extensions that VS Code suggests to work with python files.
  4. In hello.py, add a single line of code. I am assuming that this is your first line of code ever. Don’t worry much about it right now. Use ctrl + s to save.
print("Hello World")
This is how your VS Code might look like

Step 3: Make the first commit

  1. Use git status in the terminal, and you should see hello.py as an untracked file.
  2. Every change in git is tracked via something called a “commit.” It is like a unique identifier that can pinpoint a set of changes you made.
  3. Use the command git add . to track all the current changes in the repo to make them ready to commit. You can do git add file-name to add specific files instead of all (this is a good practice to ensure that only intended changes are pushed in the commit). Now check git status to see that hello.py is tracked.
  4. To create a “commit” of this change, type git commit -m "first commit" (in the terminal). Here, the -m parameter can be followed by any message in double quotes that describes the changes made.

Step 4: Connect to GitHub

  1. It is time to use gh (GitHub command line tools) to push our repo.
  2. Use gh auth login to connect to your GitHub account. It will ask you questions, and you can select the options accordingly.
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

Step 5: Push your repository to GitHub

After successfully connecting to your GitHub account, you can create a new repository on GitHub from your local repo with the below command and then push the changes you have committed.

gh repo create

You can follow the questions and select the options accordingly. For default values, you can press Enter for the corresponding question.

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository (.)

? Path to local repository .
? Repository name hello-world
? Repository owner aditya-xq
? Description (hello-world)

? Description hello-world
? Visibility Public
✓ Created repository aditya-xq/hello-world on GitHub
? Add a remote? Yes
? What should the new remote be called? (origin)
? What should the new remote be called? origin
? Would you like to push commits from the current branch to "origin"? Yes

After this, go to your repositories page on Github and check that your repo got created successfully.

Step 6: Create a new branch

  1. Use git checkout -b test to create a new branch from main called “test.” You can add any name after -b. Generally, developers create a feature branch from the main branch to write code for their specific feature. Good naming practices include using the feature number (if any) to create the branch.
  2. If you do git status now, you should see that you are on the “test” branch. You can notice the same at the bottom left corner of your VS Code, where you can see the branch name.
  3. Let us add some new code in this branch and create a commit. Just add this new line of code in hello.py.
print("Welcome to learning in public!")

Follow the below steps in your terminal to add, commit and push this change.

  • git add hello.py
  • git commit -m "second commit"
  • git push origin test

These are some of the most frequent steps you will use while programming. You can check your GitHub account to see if a new test branch and the change you added are present.

Step 7: Raising a pull request (PR)

A PR is the term we use to describe a request to merge the changes we made into another branch. After you pushed your test branch, you would have noticed an option to raise a PR.

You can create a PR from test to main following the options on GitHub as seen above or in the “Pull requests” tab.

Merge the PR to the base branch (main) on GitHub. Now, switch to the main branch in your local VS Code by clicking on the branch name in the bottom left corner. Alternatively, you can switch in the terminal by using git checkout main. In your local setup, you will see that the new code is not there yet. Use git pull in the terminal to pull the latest merged changes from the remote GitHub repo.

This is precisely how software teams write code, track changes, build feature branches, review the changes with a PR, merge them, and other developers pull the latest changes into their local repo to stay in sync.

Whenever you build any projects, it is good to use Git and create repositories of those projects. This will help you stay organized, collaborate better, practice git and it also acts as proof of work for your portfolio.

Next Steps

With Git, GitHub account, and VS Code successfully set up; you can now think of forking and cloning a public GitHub repository.

“Forking” is a way to create a copy of a repository on GitHub under your account so that you use and makes changes to it as per your interests.

“Cloning” refers to copying a repository into your local system. In general, if you want to contribute to any open-source repository on GitHub, you can fork and clone that repository into your system, add your changes, commit them and raise a PR to the main branch/fork. The people who maintain that repo will review it and merge them.

To clone any repo, you can get the HTTPS URL of that repo from the “code” option and use git clone url to clone into the folder with the name of the repo.

Take an example of the above repo. If you want to clone it, you can run this command in the terminal while in your projects folder. It will create a new project subfolder, “Python,” and copy the contents.

git clone https://github.com/TheAlgorithms/Python.git

Do these steps if you want to follow my guidelines to learn and get into the programming world.

  1. Follow this tutorial. Set up Git, Github, and VS Code, as mentioned, and create the hello-world repo to understand the end-to-end flow.
  2. After successful completion, fork and then clone the Python algorithms repository. Make sure that the URL you use to clone is from your fork. In my case, it would be:
git clone https://github.com/aditya-xq/Python.git

Once you clone it, examine the code and the structure of the repo. Don’t worry if you can’t make any sense of it yet. I will explain everything in a future article and how we can contribute to this repository. #StayTuned and subscribe to me for updates.

👉 Follow me on GitHub.

👉 Tweet about this article with feedback or for help with any step in the tutorial.

If you made it this far into the article, hi-fi!

Note: Alternatively, you can use GitHub desktop to make everything we discussed intuitive with a user interface. However, I prefer and recommend staying in the command line and quickly iterating using these commands.

--

--

XQ
The Research Nest

Exploring tech, life, and careers through content.