Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have a git repository that I cloned from an upstream source on ghithub. I made a few changes to to it (that are uncommitted and in the master branch). What I want to do is push my changes onto my github page as a new branch and have github still see it as a fork.

Is that possible? I'm fairly new to git and github. Did my question even make sense?

The easiest way that I can think of (which I'm sure is the most aroundabout way), is to fork the repo on github. Clone it locally to a different directory. Add the upstream origin repo. Create a branch in that new forked repo. Copy my code changes by hand into the new local repo. And then push it back up to my github.

Is this a common use case that there's a simpler way of doing it without duplicating directories?

I guess I'm asking here as opposed to SO since I'm on linux using command line git and the people here give better answers imo =]

share|improve this question
    
I guess this is technically on-topic, but it might do better elsewhere, since it seems to mostly be a github question – Michael Mrozek Jul 15 '11 at 19:52
up vote 13 down vote accepted

You can do it all from your existing repository (no need to clone the fork into a new (local) repository, create your branch, copy your commits/changes, etc.).

  1. Get your commits ready to be published.

    Refine any existing local commits (e.g. with git commit --amend and/or git rebase --interactive).

    Commit any of your uncommitted changes that you want to publish (I am not sure if you meant to imply that you have some commits on your local master and some uncommitted changes, or just some uncommitted changes; incidentally, uncommitted changes are not “on a branch”, they are strictly in your working tree).

    Rename your master branch to give it the name you want for your “new branch”. This is not strictly necessary (you can push from any branch to any other branch), but it will probably reduce confusion in the long run if your local branch and the branch in your GitHub fork have the same name.

    git branch -m master my-feature
    
  2. Fork the upstream GitHub repository
    (e.g.) github.com:UpstreamOwner/repostory_name.git as
    (e.g.) github.com:YourUser/repository_name.git.

    This is done on the GitHub website (or a “client” that uses the GitHub APIs), there are no local Git commands involved.

  3. In your local repository (the one that was originally cloned from the upstream GitHub repository and has your changes in its master), add your fork repository as a remote:

    git remote add -f github github.com:YourUser/repository_name.git
    
  4. Push your branch to your fork repository on GitHub.

    git push github my-feature
    
  5. Optionally, rename the remotes so that your fork is known as “origin” and the upstream as “upstream”.

    git remote rename origin upstream
    git remote rename github origin
    

    One reason to rename the remotes would be because you want to be able to use git push without specifying a repository (it defaults to “origin”).

share|improve this answer
    
Sounds perfect. Thanks. – Falmarri Jul 16 '11 at 18:00
    
Worked flawlessly, but when you create the github remote I think you're missing the "https://" in the Github URL – jesjimher Jan 13 at 13:32
1  
@jesjimher: The “URLs” in this post use the “scp-like syntax” that use the SSH, not HTTP(S). You would also need to change the colon to a slash to use them as HTTP(S) URLs (assuming the server supports both without additional namespace changes, which GitHub does). – Chris Johnsen Jan 13 at 22:13
    
Oops, you're right, Chris, I had mistyped the colon as a slash, and thus it didn't work to me unless I added "https". Sorry for the noise... – jesjimher Jan 15 at 10:15
    
In Step 4, using -u option can make your local branch my-feature tracking the same remote branch origin/my-feature. The command is: git push -u github my-feature – YaOzI 11 hours ago

There is a tool: hub

hub is a command line tool that wraps git in order to extend it with extra features and commands that make working with GitHub easier

Now you can:

# cd to a cloned repo with uncommited changes
git checkout -b feature
git commit -am 'done with feature'
git fork
git push YOUR_USER feature
# git pull-request
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.