Push Git Branch to Remote
Last Updated :
06 Oct, 2025
Git is a popular version control system that tracks changes in a project over time. It lets you see who made changes, when, and why, and easily revert to earlier versions if needed. This article covers the essential commands for pushing a Git branch to a remote repository and explains their core purpose.
Key Terminologies are as follows:
- init: It is basically a part of a git command with which we initialize git in a non-git repository.
- status: It is also a part of a command with which we can see the current state of any git repository.
- log: It is a record of all the commits done in the repository.
- commit: A commit is a snapshot of the git repository at one point in time.
- commit id: It is a 40 character hexadecimal value and it’s a unique identifier that git generates every time we make a commit to our repository.
So let us begin and see how to push a git branch to a remotely hosted repository as stepwise justified below as follows:
Git init
So before we will start pushing a branch directly, we need to create a local repository in our local system to push to the remotely hosted repository. First of all, we need to initialize git in an existing directory in our local system. For that purpose, we use the below command,
Personal@LAPTOP-SKVEHBA2 MINGW64 ~ (master)
$ cd "E:\git pushing"
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing
$ git status
fatal: not a git repository (or any of the parent directories): .git
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing
$ git init
Initialized empty Git repository in E:/git pushing/.git/
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
- Navigate to the desired directory (e.g., from C: to E: drive) to access the project folder.
- Running
git status in a non-Git folder shows an error. - To fix this, Git must be initialized in the directory first.
git init initializes the folder as a Git repository.- After initialization, Git commands work without errors.
git status
So "git status" command is basically the command to know the current state of any existing git repository. In the previous explanation, you saw that by running the command "git status" we get this information from git:
- We are on the default main branch ("On branch main")
- There are no commits yet to be done
- Finally, no files to add to the staging area.
Now we will create a file named "hello_world.cpp" in the directory and then if we run "git status" we will get something like this
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello_world.cpp
nothing added to commit but untracked files present (use "git add" to track)so basically it is saying that you have a change in the directory that you created a file named "hello_world.cpp" and currently it is untracked.
git add
So to track any change in an existing git repository we have to add it to the staging area and we can commit that change to push all the commits or you can say changes to a remotely hosted repository. For that, we ran the command "git add ."
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git add .
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello_world.cpp now we added the changes to the staging area and let's commit those changes.
git commit
To commit all changes which are there in the staging area we have a command "git commit -m "a commit message".
Note: You can only commit those changes which were already staged means which were already there in the staging area.
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git commit -m "created hello_wprld.cpp"
[main (root-commit) bb198fb] created hello_wprld.cpp
1 file changed, 8 insertions(+)
create mode 100644 hello_world.cpp
Now we have to add a remote origin to tell git that whenever we want to push or pull anything for this current repository you have to do the operations from that remote origin only. So, let's do that
git add origin
To add a remote origin to an existing local repository first of all you have to create a repository in your GitHub account.
Step 1: First of all simply go to the repositories section in your GitHub account and create a new repo by simply clicking on new button
Creating repo in my github accountStep 2: Then Give a nice name to your repo which you just created and create the repo
Create a new repo by clicking on create repository buttonStep 3: Then simply copy the URL to the repo to add as origin in your local directory

Step 4: Run the command "git remote add origin <the URL to the github repo>"
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git remote add origin https://github.com/Subrata-Rajak/Git-pushing
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git remote -v
origin https://github.com/Subrata-Rajak/Git-pushing (fetch)
origin https://github.com/Subrata-Rajak/Git-pushing (push)
After executing the command you can simply check whether your remote origin is defined or not by the "git remote -v" command.
Step 6: Finally Git push
To push the branch or you can say to push the changes in the branch to the Github repo you have to run this command "git push origin <the branch name>" in our case the branch name is "main".
Personal@LAPTOP-SKVEHBA2 MINGW64 /e/git pushing (main)
$ git push origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 327 bytes | 327.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/Subrata-Rajak/Git-pushing
* [new branch] main -> main
After pushing the changes the repo will look like and this is how you can push a branch to a remotely hosted GitHub repository.
The repo after pushing changes
Why do you need to run git init before pushing a branch to a remote repository?
-
To create a backup of the folder
-
To convert the current folder into a Git repository
-
To automatically push all files
-
To reset the existing repository
Explanation:
git init creates a new Git repository by adding a hidden .git folder. Without initializing Git, commands like git add, git commit, or git push will not work, because Git cannot track changes in a non-Git directory.
After creating a new file, git status shows “untracked file: hello_world.cpp.” What does this mean?
-
The file has already been committed
-
Git is ignoring this file
-
The file exists but is not staged for tracking
-
The file has been deleted
Explanation:
An untracked file means Git sees the file in your directory but it’s not yet added to version control. You must run git add hello_world.cpp (or git add .) to stage it before committing.
What is the purpose of the command git remote add origin <repo-url>?
-
To rename the remote repository
-
To delete the existing remote connection
-
To link your local repository to a remote repository
-
To commit changes to the remote repository
Explanation:
git remote add origin <URL> sets the remote location where your code will be pushed or pulled. “origin” becomes the shortcut name that refers to your GitHub repository’s URL.
Which command is used to push your local branch changes to the remote repository?
-
-
-
git push origin <branch-name>
-
Explanation:
To upload commits from the local repository to the remote, use:
This sends your branch’s history to the remote repository under the same branch name.
What happens if you try running git push before adding a remote and setting up a branch?
-
Git automatically creates a remote
-
Git pushes to a default location
-
Git shows an error because no remote is configured
-
Git commits all files automatically
Explanation:
Explanation:
If no remote exists, running git push results in an error like:
You must first add a remote (e.g., origin) and then push a branch.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands