![]() The git push command is one you'll be using often, and there are tons of options that can be used with it. ![]() ![]() You won't need to push all branches from your local very often, but if you do you can add the -all flag: (main)$ git branch + 728f0df.8bf04ea some-branch -> my-feature How to push all local branches to the remote To push to a branch of a different name, you just need to specify the branch you want to push and the name of the branch you want to push to separated by a colon ( :).įor example, if you want to push a branch called some-branch to my-feature: (some-branch)$ git push origin some-branch:my-feature You will usually push your local branch to a remote branch of the same name-but not always. How to push to a branch of a different name on Git If you're collaborating with others on your branch, it would be good to either avoid using -force or at least use -force-with-lease to prevent losing changes other collaborators have made. To prevent this scenario, you can use the -force-with-lease option.Īgain from the documentation: -force-with-lease alone, 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.īasically, you're telling Git to force update this branch only if it looks the same as when you last saw it. If someone else contributes to your branch and pushes up their changes to the remote-and you force push over it-you will overwrite their changes. Sometimes you may want to force push-but only if no one else has contributed to the branch. ( Note: you can use -f as a shorthand instead of -force.)Ī force push is a destructive action-only use it when you're certain it's what you want to do. + edb64e2.52f54da my-feature -> my-feature (forced update) You could do a git pull here to merge the differences, but if you really want to overwrite the remote repository you can add the -force flag to your push: (my-feature)$ git push -force origin my-feature ![]() Hint: See the 'Note about fast-forwards' in 'git push -help' for details. ! my-feature -> my-feature (non-fast-forward)Įrror: failed to push some refs to Updates were rejected because the tip of your current branch is behind This means that if you try to push a branch that has been rebased locally-but not on the remote-the remote repository will recognize that the commit history has changed, and it will prevent you from pushing until you settle up the differences: (my-feature)$ git push It's very important to understand that even though the branch looks the same, it's composed of entirely new commits. The second and more common scenario is after an action like a rebase-which changes the commit history: Internally, Git accomplishes by creating new commits and applying them to the specified base. The first reason is to fix a mistake-although it is probably better to just make a new commit reverting the changes. There are a couple reasons you may want to do this. Normally, you will push to a branch and add to its commit history.īut, there are times when you need to forcefully overwrite the history of a branch. To :johnmosesman/burner-repo.gitįrom the output you can see that the local main branch was pushed to the remote main branch: To :johnmosesman/burner-repo.git In the example below, the origin remote is a GitHub repository, and the current branch is main: (main)$ git remote -v If your current branch is main, the command git push will supply the two default parameters-effectively running git push origin main. The general form of the command is this: $ git push īy default, Git chooses origin for the remote and your current branch as the branch to push. If you run the simple command git push, Git will by default choose two more parameters for you: the remote repository to push to and the branch to push. This command has a variety of options and parameters you can pass to it, and in this article you'll learn the ones that you will use the most often. The basic command for pushing a local branch to a remote repository is git push.
0 Comments
Leave a Reply. |