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
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- Create S3 bucket using AWS CLI Command mb - AWS
- How to resolve Certificate Expired WhatsApp Error - WhatsApp
- [fix] Java Spring Boot JPA SQLSyntaxErrorException: Encountered user at line 1 column 14 - Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error - Java
- How to Update Device Drivers on Windows 10/11 Manually - Windows
- Location of eclipse.ini file on Mac OS X - Mac-OS-X
- hibernate.cfg.xml Configuration and Mapping xml Example - Java
- How to delete SNS Topic using AWS CLI - AWS