That will almost work.
When pushing to a non-default branch, you need to specify the source ref and the target ref:
git push origin branch1:branch2
Or
git push <remote> <branch with new changes>:<branch you are pushing to>
Answer from SLaks on Stack OverflowThat will almost work.
When pushing to a non-default branch, you need to specify the source ref and the target ref:
git push origin branch1:branch2
Or
git push <remote> <branch with new changes>:<branch you are pushing to>
Certainly, though it will only work if it's a fast forward of BRANCH2 or if you force it. The correct syntax to do such a thing is
git push <remote> <source branch>:<dest branch>
See the description of a "refspec" on the git push man page for more detail on how it works. Also note that both a force push and a reset are operations that "rewrite history", and shouldn't be attempted by the faint of heart unless you're absolutely sure you know what you're doing with respect to any remote repositories and other people who have forks/clones of the same project.
How to git commit and push to a new remote branch?
git - How can I push a specific commit to a remote, and not previous commits? - Stack Overflow
a question about pushing changes to a branch in a repo (not the master branch)
How do I push a new local branch to a remote Git repository and track it too? - Stack Overflow
Videos
To push up through a given commit, you can write:
git push <remotename> <commit SHA>:<remotebranchname>
provided <remotebranchname> already exists on the remote. (If it doesn't, you can use git push <remotename> <commit SHA>:refs/heads/<remotebranchname> to autocreate it.)
If you want to push a commit without pushing previous commits, you should first use git rebase -i to re-order the commits.
The other answers are lacking on the reordering descriptions.
git push <remotename> <commit SHA>:<remotebranchname>
will push a single commit, but that commit has to be the OLDEST of your local, non-pushed, commits, not to be confused with the top, first, or tip commit, which are all ambiguous descriptions in my opinion. The commit needs to be the oldest of your commits, i.e. the furthest from your most recent commit. If it's not the oldest commit then all commits from your oldest, local, non-pushed SHA to the SHA specified will be pushed. To reorder the commits use:
git rebase -i HEAD~xxx
After reordering the commit you can safely push it to the remote repository.
To summarize, I used
git rebase -i HEAD~<number of commits to SHA>
git push origin <post-rebase SHA>:master
to push a single commit to my remote master branch.
References:
- http://blog.dennisrobinson.name/push-only-one-commit-with-git/
- http://blog.dennisrobinson.name/reorder-commits-with-git/
See also:
- git: Duplicate Commits After Local Rebase Followed by Pull
- git: Pushing Single Commits, Reordering with rebase, Duplicate Commits
when you're working from a branch and just want to save the changes to that branch only, do you still do git push origin master, or what do you do?
*I need to know this because I went back a few commits, made commits in that old version, and need to save and push that, but also need to retain the most recent commit made.
In Git 1.7.0 and later, you can checkout a new branch:
git checkout -b <branch>
Edit files, add and commit. Then push with the -u (short for --set-upstream) option:
git push -u origin <branch>
Git will set up the tracking information during the push.
If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:
git push --all -u
(Not exactly what the OP was asking for, but this one-liner is pretty popular)
If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.