*ARGS.TECH | BLOG | A beginner's guide to Git and GitHub: From `git init` to `git push`
Loading...
BLOG

A beginner's guide to Git and GitHub: From `git init` to `git push`

Learn the essential Git commands to create your first repository, push your code, and collaborate.


Introduction


If you're new to software development or system administration, you've heard of Git and GitHub. They are the industry-standard tools for version control (tracking changes to your code) and collaboration (working with others).


  • Git is the tool on your local computer that tracks changes.
  • GitHub is a website that hosts your Git projects (called "repositories") in the cloud, allowing you to back them up and work with a team.


This guide will walk you through the absolute basics. We'll start with an empty project on your computer, create a repository on GitHub, and push your code to it. Then, we'll cover the daily workflow of making and pulling changes.


Prerequisites: Configure Git


Before you do anything else, you must install Git and tell it who you are. This information is attached to every "commit" (snapshot) you make.


  • Install Git: You can download it from the official Git website.
  • Set your name: Open your terminal and type:

xinit@localhost:~$ git config --global user.name "Your Name"


  • Set your email: This must be the same email you use for your GitHub account.

xinit@localhost:~$ git config --global user.email "you@example.com"


Step 1: Create a new repository on GitHub


First, we need a place on GitHub to store our code.

  1. Log in to your GitHub account.
  2. Click the + icon in the top-right corner and select "New repository".
  3. Give your repository a short, memorable name (e.g., my-first-project).
  4. Add a short description (optional).
  5. Important: For this guide, keep the repository Public and do NOT check "Add a README file," "Add .gitignore," or "Choose a license." We want to start with a completely empty repository.
  6. Click "Create repository".


You will now see a page with a URL (like https://github.com/your-username/my-first-project.git) and some command-line instructions. We're about to use those!


Step 2: Push your local project to GitHub


Now, let's go to your local computer. Let's say you have a project folder my-first-project with a few files in it.


Open your terminal and navigate into that folder:

xinit@localhost:~$ cd /path/to/your/my-first-project


Now, we'll follow the steps from the GitHub page (which you showed in your screenshot) to "connect" this local folder to the GitHub repository you just created.


  • Initialize Git: This command creates a hidden .git folder inside your project directory, turning it into a Git repository.

xinit@localhost:~$ git init


  • (Optional) Rename your branch: By default, the main branch might be called master. The modern standard is main. This command ensures you are using main.

xinit@localhost:~$ git branch -M main


  • Add your files: This command "stages" your files, preparing them to be saved. Using . adds all files in the current directory.

xinit@localhost:~$ git add .


  • Create your first "commit": A commit is a snapshot of your project at a specific point in time. The -m flag lets you add a descriptive message.

xinit@localhost:~$ git commit -m "First commit"


  • Connect your local repo to GitHub This is the most important step. It tells your local repo where the "remote" (named origin) is located. Replace the URL with the one from your own GitHub repository page.

xinit@localhost:~$ git remote add origin https://github.com/your-username/my-first-project.git


  • Push your code This command "pushes" (uploads) your committed files from your local main branch to the origin (GitHub). The -u flag sets it as the default, so in the future, you can just type git push.

xinit@localhost:~$ git push -u origin main


That's it! Refresh your GitHub page. You will now see all your files.


Step 3: The daily workflow (Branch, Commit, Push)


You should never make changes directly on the main branch. The correct workflow is to create a new "branch" for every new feature or fix.


  • Create and switch to a new branch: Let's say you want to add a new feature.

xinit@localhost:~$ git checkout -b new-feature

(This single command creates a new branch called new-feature and immediately switches you to it.)


  • Make your changes: Now, you can safely edit your files, add new ones, etc.
  • Add and commit your changes: Just like before, you stage and save your new snapshot.

xinit@localhost:~$ git add .


xinit@localhost:~$ git commit -m "Add new awesome feature"


  • Push your new branch to GitHub: This time, you are pushing the new-feature branch.

xinit@localhost:~$ git push origin new-feature


  • Create a "Pull Request" (PR) to merge your changes: Go to your repository on GitHub. You will see a new yellow banner prompting you to "Compare & pull request".


Click it. This will take you to a new page. A Pull Request is a formal request to "pull" your new-feature branch and merge it into the main branch.


Click "Create pull request", and then on the next page, click "Merge pull request" and "Confirm merge".


Your feature is now part of the main branch!


Step 4: Staying up-to-date (pulling changes)


What if a teammate (or you, on another computer) merges a change to main? Your local copy is now out of date. You need to "pull" those changes.


  • Switch back to your main branch: You should always pull changes into your main branch.

xinit@localhost:~$ git checkout main


  • Pull the latest changes from GitHub: This command fetches all new changes from origin (GitHub) and merges them into your current local branch (main).

xinit@localhost:~$ git pull origin main

Your local main branch is now fully up-to-date with the one on GitHub.


Conclusion


You now know the essential Git workflow! You've learned how to:

  • Configure Git (git config)
  • Initialize a project (git init)
  • Add and commit files (git add, git commit)
  • Push to GitHub (git remote add, git push)
  • Work with branches (git checkout -b, git push origin ...)
  • Get updates (git pull)


This is the foundation for all version control. Keep practicing these commands, and they will become second nature.

Top button