github synchronization - upstream, origin and the local repository

810 views Asked by At

I'm a noob at TortoiseGit/github and it's driving me crazy..

Basically I created a local branch, say myBranch, did something I want to keep, then kinda feeling that I had messed up the other branches existing. I also did some incorrect push to my origin so some branches in there were messed up, too. What I want now is to only keep the work I did in my local myBranch, and keep everything else updated according to upstream so that both my local repository and origin are 'clean'.

Firstly, I found that the only way to update origin according to upstream is by updating my local repository first, then pushing it to origin?

Secondly, I fetched upstream. But when I checkout to upstream/master, or to my local master, or tried to merge upstream/master and my local master, it always has this error 'The following untracked working tree files would be overwritten by checkout/merging'. What could be the reason?

Thirdly, in github I can see that the upstream repository has 2 branches: master and ASecondBranch, therefore origin too has these 2 branches to be updated. But after I have fetched, there's only upstream/master (can see that using 'git branch -r') while I can't find upstream/ASecondBranch. Then how do I update my local and the origin ASecondBranch according to upstream?

Thanks in advance!

1

There are 1 answers

1
Ash Wilson On

First: Yes, the only way to keep origin up to date with an upstream repository is by fetching from upstream into a local clone first, and then pushing changes out to origin.

Usually, this ends up not being a big deal; if you're doing work with a fork to contribute to someone else's project, you should be keeping your changes in your own branch (which it sounds like you already are), and submitting a pull request to the upstream project from that branch. It won't matter if master is behind upstream/master, say, because the pull request only cares about the branch you specify.

If you've made commits on master that you don't want to keep, you'll want to reset --hard to upstream/master. To do this in Tortoise, checkout master, fetch from upstream with the sync dialog, open the log view, right-click the commit at upstream/master, choose "Reset master to this" and select "hard". Note that the next time you push, you'll need to select "force overwrite". You can repeat with your other branches as necessary. Double-check your log before you do this and be certain that you're okay with getting rid of the work you've done locally!

Second: It sounds like you have changes in your working copy that you haven't staged and committed. When you're working with git, it's much easier if you get into the habit of committing often and keeping your working copy clean. Either commit your changes to the branch you're on, stash them, or clean them out. The untracked files should appear in Tortoise's commit dialog.

Third: This one is strange; a git fetch should create remote tracking branches for new branches in the remote you fetch from. Did you set something up in the Sync dialog to only fetch master by mistake? Try specifying the remote branch explicitly by name.