1

I had cloned a repo after cutting a branch from the mainline and have now made changes to it. Now I want to push back my changes to the already existing remote branch I had cut. But on running 'git push' the code gets pushed to the master or the main branch and not the remote branch I had cut and cloned initially. How do I push my local code to a branch that already exists on git.

  • Possible duplicate of [How git push other branch to remote/origin?](https://stackoverflow.com/questions/16166713/how-git-push-other-branch-to-remote-origin) – vx3r Aug 27 '19 at 05:21
  • Please clarify the question. You cannot "push a repo to a branch", but you can do that with a specific branch in your repo. Next, there is a huge difference if the branch with the same name exists in your remote, and many other details are important. – Dmitry Kuzminov Aug 27 '19 at 05:25
  • Made few changes to the question. Hope it's better and clearer now. – iliyas hussain Aug 27 '19 at 05:35
  • What have you tried? Did you git push ? Where you in the correct branch on your local machine? If you just did git push, have you set up the upstreams correctly? – mfnx Aug 27 '19 at 06:19
  • This might be a good read for you: https://stackoverflow.com/help/minimal-reproducible-example – mfnx Aug 27 '19 at 06:24

4 Answers4

1

Try following,

Fetch branches git fetch --all

See all available branches, git branch

Checkout to the existing branch git checkout BRANCH_NAME

Then try pushing

See this, https://www.atlassian.com/git/tutorials/using-branches/git-checkout

Tony
  • 152
  • 2
  • 15
0

Try to specify the branch name explicitly:

git push origin name_of_your_branch:name_of_your_branch

That means that you specify the name of your local branch to be pushed, and the name of the remote branch where to push:

git push origin local-name:remote-name
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
0

It is better to merge with the latest master from staging by :

git fetch origin master:branch_to_push

Checkout to it :

git checkout branch_to_push

Merge the working code:

git merge local_working_branch

Then, push this new branch over the one in staging:

git push origin branch_to_push:branch_present_on_staging

Hope this helps!

Grv97
  • 81
  • 1
  • 1
0

You can refer below commands :

git checkout -b <branch-name>  -- locally create new branch
git push -u origin <branch-name> -- create/update branch on server
git checkout <branch-name> -- move head to branch so that changes can be done on that branch
git status -- check branch name & changes done
git branch -- show branch present in your local (fetched from server in your local)