The Git Push command is used to upload commits from your local repository to a remote repository (e.g., GitHub, GitLab, Bitbucket). Once pushed, other developers can access these changes and contribute by pulling them into their local repositories.
Before pushing, you must commit your changes locally using:
git commit -m "Commit message"
In the above diagram, we can observe If our local main branch is way back when compared to the central main repository after that git push origin main will publish the changes. git push is essential as same as the git merge.
git push <remote> <branch>- The <remote> The alias of the remote repository (origin)
- The <branch> The branch you want to push (main , develop)
Example:
git push origin mainSetting Up a Remote Repository
If no remote is linked, add one using:
git remote add <remote-name> <remote-URL>Example:
git remote add origin https://github.com/username/repo.gitNow you can push your changes:
git push -u origin main-u sets origin main as the default upstream branch for future pushes.
Useful Variations of Git Push
Push all branches
git push --all <remote>Force push (use with caution)
git push --force <remote> <branch>Overwrites remote history with your local history. Use only in special cases (e.g., rewriting commits).
Force push with safety
git push --force-with-lease <remote> <branch>Safer than --force because it prevents overwriting changes you don’t know about.
Git Push and Synchronization
When multiple developers are working:
Commit changes locally
git commit -m "message"Push changes to remote
git push origin branch-namePull latest changes before pushing again
git pullResolve conflicts if needed
Merge conflicts may occur if multiple people edited the same files.Re-push after resolving conflicts
git pushPushing To Bare Repositories
A bare repository contains only Git metadata and objects, without a working directory. It’s commonly used as a central repository.
Following are the steps to push into the bare repository:
Step 1: Create a bare repository.
git init --bare <directory>Step 2: Clone the repository.
Clone the bare repository to the local machine. By using the following command.
git cloneStep 3: Make changes and Commit the changes.
git commit -m "Commit message"Step 4: Push the changes.
After committing the changes to your local machine. Now it is time to push them to the bare repository by using the following command.
git push <Alias name of remote repository> <Branch name>By pushing the local repository to the central repository you are making it available for other developers who need to work on it.
Amended Force Push
If you want to change your last commit and update it on the remote:
Step 1: Amend commit:
git commit --amend -m "New commit message"Step 2: Push with lease:
git push --force-with-lease <Alias name> <Branch name> using "--force-with-lease" will force push only if the remote branch's current commit matches the expected commit. It will prevent accidental overwriting of others' changes.
Git Push Usage
After developing a feature and committing it locally, you can upload changes to a remote repository so others can access them:
git push <remote> <branch>Common Variations
git push <remote> --force: This command forces the push even if it results in a non-fast-forward merge.
git push <remote> --forcegit push <remote> --all: This command pushes all the local branches to the specified remote repo.
git push <remote> --allSet upstream branch:
git push -u origin master-u sets the default upstream branch so future pushes can be done with just git push.
If you see an error like “origin does not appear to be a git repository”, it means no remote is set. Add one first:
git remote add origin <repo-URL>
To add this repo project to your GitHub account, use the git remote add command on the terminal, in the directory where your repository is currently stored at.
git remote add <remote-name><remote URL>This command takes two arguments:
- A remote name, let’s say for example, “Origin”
- A remote URL, which is your repo address as shown below.

Now you can run the git remote add command and git push command to push your code work to your repository.

We used crio here in place of origin which is the repo name coz we assign an origin to another repo and if you assign an origin to a certain repo, you cannot use it again.

Now, the code is successfully pushed to the repository.


