Git: Step-by-Step - How to Push Local Brach to GitHub


Post Banner

In this tutorial, we will try to cover all steps right from creating a local git repository to committing some code and pushing our first repo from local to GitHub remote repository.


Step 1: Check if git is installed on your System

The first step is to check if git is installed on your device. Open Command Line (CMD) if you Windows, or Terminal if you are using Linux/Mac and run the following command,

# git version
git version 2.36.2

You should get the git version back as a response, if you get git: not found, then you need to first install git on your device.

Installing Git:

Windows: https://code2care.org/howto/install-git-on-windows

Ubuntu/Linux: https://code2care.org/linux/install-git-on-ubuntu-linux

Mac: Run command: brew install git


Step 2: Creating local repository

Let us create a directory which we will use as a local git repo

# mkdir my-git-repo
# cd my-git-repo

Step 3: Initialize Local Git Repo

% git init
Initialized empty Git repository in /my-git-repo/.git/

✏️ Make sure that you change the directory to my-git-repo before executing the git init command.


Step 4: Create a file and add to staging area

Create a file index.html
# echo "<h1>Hello World<h1>" > index.html

Add file to staging area
# git add index.html

Step 5: Commit the file to git

# git commit -m "First commit"
[main (root-commit) 11a3895] First Commit
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

Step 6: Create a project on GitHub Account

Now login to your GitHub account and create a public or private repo named the same what we have locally my-git-repo


Step 7: Generate a Personal Access Token

1. Go to github.com,
2. Go to Profile -> Settings,
3. Select Developer Settings,
4. Select Personal access tokens,
5. Now click on Generate new token,
6. Select Expiration and Scope,
7. Click on Generate Token.
8. Copy the token, it should look something like this ghp_129ZVLCiqkYTiWXxYC9ZSXjf20AkhN1InLO

Step 8: Add Remote Repo to Git

#  git remote add origin your-personal-access-token@github.com:your-github-user/my-git-repo.git

Step 9: Push Local Brach to GitHub Remote Branch

# git push origin -u main

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 231 bytes | 115.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/my-git-user/my-git-repo.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

✏️ You might have the default branch name as master instead of main

Step by Step pushing git local branch to GitHub

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Ventura 13 and on Windows 11.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap