Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push a branch back to a private github repo

Tags:

git

github

I've cloned a private repo in github to my local machine. After I'm done making some changes, I want to push the branch back to github to propose a merge (or pull request, as github calls it).

How do I do that?

I'm absolutely new to github (I normally use bzr/launchpad, and the command was bzr push lp:~<username>/<master-branch>/<name of the new child branch>. What is the equivalence of this in git?).

[Edit: I found that my wording was confusing due to my little understanding of git's terminologies, I am going to re-describe what I did]

(bar is the private repo)

$git clone [email protected]:foo/bar.git
$cd bar
$touch <some file> # ie., make some changes
$git commit ...
$ <<<<< NOW I need to push this back to github here.
like image 916
user1508893 Avatar asked Sep 18 '25 20:09

user1508893


2 Answers

From your comments above and your original question, you want to do a pull request on github but you've modified master locally ... that's a problem. What you wanted to do is branch first, make the changes, commit locally, and push the branch. Then you could create a pull request on github from that branch.

First what you need to do to fix that is:

git branch mytopicbranch
git reset --hard origin/master

You now have a branch and have reset master locally to what it is in the remote repo. You can now push your branch with:

git push origin mytopicbranch

Then on github you'll be able to create a pull request for that branch.

The normal flow would be to do the following:

git clone <remote repo> 
<cd to directory>
git checkout -b mybranch 
<make changes> 
<commit changes>
git push origin mybranch
<create pull request on github>

Edit to add: This is for the reason you asked; wanting to do a "pull request". If you just want to push master you'd simply do:

git push origin master

And your changes will be committed to master remotely.

like image 59
Brian Roach Avatar answered Sep 21 '25 20:09

Brian Roach


The private master repo is NOT owned by me (ie., I don't have the permission to push my repo back to the master repo). I think I can only push my local repo back, and request the owner to merge it to the master

You seem to be confusing a lot things here. First of all, a repository is a single unit. When you clone a repository, you get a complete local copy of it.

If you cloned a repository, that is not owned by yourself and to which you have no write-rights, then you will not be able to push any changes to that repository. If that is the case, you will need to fork it first to your own GitHub account. To do that, click the fork button in the repository view. This will create a copy of the repository in your own account, which you will have push rights for.

So, if you have a local clone of the repository, you can do whatever you want. Nothing affects the remote repository, neither the original one, nor your GitHub fork. You can create as many branches as you want and commit whereever you want. Note here that branches are not some construct in Git. Branches are simple pointers to commits, so creating a branch is not doing much, and will have no impact on your structure unless you actually start committing.

If you have commits you want to push to a remote repository, you can do that by using git push:

git push <remote> <local-branch>:<remote-branch>

So if you want to push your local master branch into a branch test on the remote named origin, you can do git push origin master:test. As you pushed explicitely to the remote branch test (which does not need to exist previously), you will not touch the remote’s master branch. The other variations of git push are just abbreviations of the above command. For example git push <remote> <branch> which is probably the the second to most common way to push things will push your local branch <branch> to the tracking remote branch (usually the othe with the same name). So it’s more or less equivalent to <branch>:<branch>. The most common way would be just git push which will figure out the remote and branch on its own based on your configuration, but to be safe, you can just specify it explicitely.

So, coming back to your use case, I assume that you want to fork someone else’s repository to make a change to it and propose that change to the original author as a pull request. To do that, you should follow these steps:

  • Fork the repository on GitHub
  • Clone your own repository (i.e. the fork)
  • In your now local repository do your changes
  • Push the changes into your fork (any branch is fine, although using a separate one is advised for clarity)
  • When browsing your fork on GitHub, click the pull request button to create one based on your pushed branch
like image 27
poke Avatar answered Sep 21 '25 21:09

poke