The git push command is used to send local commits from your Git repository to a remote repository such as GitHub. It is one of the most commonly used commands when working with remote repositories.
git push, make sure
your local repository has a configured remote and that your changes
have been committed.
git push transfers local commits to a remote repository.
Local Repository
↓
git push
↓
Remote Repository
For example, you can push your local commits to GitHub.
Git push is used when you want to send your local commits to a remote repository.
The basic syntax is:
git push <remote> <branch>
Example:
git push origin main
Here origin is the remote name and
main is the branch name.
origin is a commonly used name for the primary remote
repository.
For example:
git push origin main
This means push the main branch to the remote named
origin.
The branch specifies which local branch should be pushed.
git push origin maingit push origin feature-loginThe first command pushes
main, while the second pushes thefeature-loginbranch.
Before pushing, you can check your current branch:
git branch
The current branch is normally marked with an asterisk.
* main
feature-login
Use git status before pushing to understand the state of
your working directory and staging area.
git status
Remember that git push sends commits, not simply every
modified file in your working directory.
A common workflow is to stage and commit your changes before pushing:
git add .
git commit -m "Update project"
git push origin main
The push sends the committed changes to the remote repository.
For the first push of a local branch, you can use:
git push -u origin main
The -u option sets the upstream tracking relationship
between the local branch and the remote branch.
The -u option is short for
--set-upstream.
git push -u origin main
It records the remote tracking relationship so later commands can often use:
git push
After upstream tracking has been configured, you can usually use:
git push
Git uses the configured upstream branch for the push.
Suppose you have a branch called feature-login.
You can push it with:
git push -u origin feature-login
This publishes the branch to the remote and sets its upstream relationship.
A complete example is:
git switch -c feature-dashboard
git add .
git commit -m "Add dashboard"
git push -u origin feature-dashboard
The new branch is pushed to the remote repository.
Each branch can be pushed separately.
git push origin main
git push origin develop
git push origin feature-login
This allows different branches to be published to the remote repository.
Git can push all local branches using:
git push --all origin
Use this carefully because it can publish multiple local branches to the remote.
A specific tag can be pushed using:
git push origin v1.0.0
You can also push all local tags:
git push origin --tags
A repository can have multiple remotes. You can specify which remote should receive your branch.
git push upstream main
Here upstream is the remote name.
Before pushing, it can be useful to check the configured remote:
git remote -v
Example:
origin https://github.com/username/project.git (fetch)
origin https://github.com/username/project.git (push)
Sometimes a push is rejected because the remote branch contains changes that are not present in your local branch.
A common next step is to retrieve the remote changes and integrate them appropriately before pushing again.
git pull
The exact resolution depends on the repository history and the reason for the rejection.
A non-fast-forward rejection can happen when the remote branch has commits that your local branch does not contain.
One common approach is:
git pull --rebasegit pushReview the changes and resolve any conflicts if necessary.
Git provides force-push options for cases where you intentionally need to replace the remote branch history.
git push --force
Force pushing can overwrite remote history, so it should be used carefully, especially on shared branches.
A safer alternative in many workflows is:
git push --force-with-lease
It checks whether the remote reference is still at the expected state before replacing it.
Even this option should be used carefully on shared branches.
For a new branch, the commonly used command is:
git push -u origin feature-login
After this, you can usually use:
git pushgit pull
# Check changes
git status
# Stage changes
git add .
# Commit changes
git commit -m "Update login page"
# Push commit
git push
This is a common daily workflow after upstream tracking has already been configured.
When pushing to GitHub, Git must authenticate the operation according to your configured remote and authentication method.
Depending on the setup, this may involve HTTPS credentials, credential management, or SSH keys.
You can inspect branch tracking information with:
git branch -vv
Example:
* main 1a2b3c4 [origin/main] Update project
This indicates that the local main branch is tracking
origin/main.
git status
git add .
git commit -m "Add student dashboard"
git remote -v
git push -u origin main
After the first upstream push, future changes can normally use:
git add .
git commit -m "Update dashboard"
git push
The git push command sends local commits to a remote
repository. It is an essential command for publishing local changes
to GitHub and collaborating with other developers.
git add .
git commit -m "Update project"
git push
For the first push of a branch, you can use:
git push -u origin main
git push sends local commits to a remote repository.git push <remote> <branch>.origin is a common remote name.git push origin main pushes the main branch to origin.git push -u origin main sets upstream tracking.git push can usually be used.git push -u origin branch-name.git push origin tag-name.git push --all origin can push all local branches.--force-with-lease provides an additional safety check for force pushes.Question: Which command pushes the local main branch to the remote named origin and sets the upstream branch?