How to Upload Git Cod to Branch
The basic command for pushing a local branch to a remote repository is git push.
This command has a variety of options and parameters you can pass to it, and in this article you lot'll learn the ones that you will use the most often.
How to push a local Git branch to Origin
If you run the simple command git push, Git volition by default choose ii more parameters for you lot: the remote repository to push to and the branch to push.
The general class of the control is this:
$ git push button <remote> <branch> By default, Git chooses origin for the remote and your current branch as the branch to push.
If your current co-operative is master, the command git push button will supply the 2 default parameters—effectively running git push origin main.
In the instance below, the origin remote is a GitHub repository, and the current branch is main:
(chief)$ git remote -5 origin git@github.com:johnmosesman/burner-repo.git (fetch) origin git@github.com:johnmosesman/burner-repo.git (push) (main)$ git push button Enumerating objects: 4, done. Counting objects: 100% (four/four), done. Delta compression using up to 16 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:johnmosesman/burner-repo.git b7f661f..ab77dd6 master -> main From the output you lot tin see that the local primary branch was pushed to the remote main co-operative:
To github.com:johnmosesman/burner-repo.git b7f661f..ab77dd6 main -> main How to strength push a branch in Git
Normally, you will push to a branch and add together to its commit history.
Just, at that place are times when you demand to forcefully overwrite the history of a co-operative.
There are a couple reasons you may desire to practise this.
The first reason is to fix a mistake—although it is probably better to just make a new commit reverting the changes.
The second and more common scenario is later an action like a rebase—which changes the commit history:
Internally, Git accomplishes [a rebase] past creating new commits and applying them to the specified base of operations. It's very of import to understand that even though the co-operative looks the same, it'southward composed of entirely new commits.
A rebase creates entirely new commits.
This means that if you endeavour to push a branch that has been rebased locally—only not on the remote—the remote repository volition recognize that the commit history has changed, and it will prevent you from pushing until y'all settle up the differences:
(my-feature)$ git push To github.com:johnmosesman/burner-repo.git ! [rejected] my-feature -> my-characteristic (non-fast-forward) error: failed to push some refs to 'git@github.com:johnmosesman/burner-repo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote analogue. Integrate the remote changes (east.g. hint: 'git pull ...') before pushing once more. hint: See the 'Note about fast-forwards' in 'git push button --help' for details. Yous could practice a git pull here to merge the differences, simply if you actually want to overwrite the remote repository you can add the --force flag to your push:
(my-characteristic)$ git push --strength origin my-feature Enumerating objects: 1, done. Counting objects: 100% (1/1), done. Writing objects: 100% (ane/1), 184 bytes | 184.00 KiB/s, done. Total i (delta 0), reused 0 (delta 0) To github.com:johnmosesman/burner-repo.git + edb64e2...52f54da my-feature -> my-feature (forced update) (Notation: you lot tin can use -f as a shorthand instead of --force.)
A force push is a destructive action—only use it when you're certain it'southward what you want to practice.
Strength push with charter
Sometimes you may desire to force push button—but only if no one else has contributed to the branch.
If someone else contributes to your branch and pushes up their changes to the remote—and y'all forcefulness push over it—yous will overwrite their changes.
To prevent this scenario, y'all can use the --forcefulness-with-charter option.
Again from the documentation:
--strength-with-lease lonely, without specifying the details, will protect all remote refs that are going to be updated by requiring their current value to be the same as the remote-tracking branch we have for them.
Basically, you're telling Git to force update this co-operative just if it looks the same as when y'all last saw it.
If you're collaborating with others on your branch, information technology would exist expert to either avert using --force or at to the lowest degree use --force-with-lease to forestall losing changes other collaborators have fabricated.
How to push button to a co-operative of a dissimilar name on Git
Y'all will usually button your local co-operative to a remote branch of the same name—but non always.
To button to a branch of a different name, you just need to specify the branch you want to button and the name of the branch y'all want to push button to separated past a colon (:).
For instance, if you want to push a branch called some-branch to my-feature:
(some-branch)$ git push origin some-branch:my-characteristic Full 0 (delta 0), reused 0 (delta 0) To github.com:johnmosesman/burner-repo.git + 728f0df...8bf04ea some-co-operative -> my-feature How to push all local branches to the remote
You won't need to push button all branches from your local very oftentimes, but if you practice you tin can add the --all flag:
(primary)$ git branch * main my-feature (master)$ git button --all ... To github.com:johnmosesman/burner-repo.git b7f661f..6e36148 primary -> primary * [new branch] my-characteristic -> my-feature Conclusion
The git push button command is one yous'll be using often, and at that place are tons of options that can be used with it. I encourage you lot to read the documentation for helpful options and shortcuts.
If yous liked this tutorial, I as well talk about topics like this on Twitter, and write most them on my site.
Learn to lawmaking for costless. freeCodeCamp'southward open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Source: https://www.freecodecamp.org/news/git-push-to-remote-branch-how-to-push-a-local-branch-to-origin/
0 Response to "How to Upload Git Cod to Branch"
Post a Comment