Jonyeezs' Git Reference

Your branch and origin have diverged; and can’t pull remote

Few scenarios that this could happen:

  1. Someone has rebased and forced push to remote
  2. You have rebase your branch onto something else
  3. You’ve changed your local history through rebase interactive

Firstly check what is different: git log HEAD..origin/<remotebranch>

go for git pull --rebase

If you have code changes do git pull --rebase=i

last resort:

git fetch origin
git reset --hard origin/<branchatfault>

Force push with love

push --force-with-lease

Here’s an in-depth post on why you shouldn’t use --force: link. It has alot of information (which honestly, I skimmed through at the end).

What we are looking here is doing a force push gracefully, this will reject the force if there has been changes to the remote (branch upstream) in comparison to our local. This ensures we don’t delete anyone’s commit or compromise the history.

I’ve included this as an alias in my .gitconfig

Rebase or Merge?

Another pro-tip: If anyone other than you is working on the same branch, best not to do a rebase.

Merge without the merge commit noise

git checkout <branch>
git pull <remote>/<branch> --ff

During this process you can check the diff before the merge: git diff <branch> <remote>/<branch>

git pull error: ‘ref refs/remote/{branchatfault} is at {hash} but expected {hash}’

Happened when someone forced push and upset the balance. Or someone created a branch with the same name but different letter casing.

git update-ref -d refs/remotes/origin/<branchatfault> then git status and it should tell you what you need to do.

Push with different branch name

git push origin <local-name>:<remote-name>

Your local branch name doesn’t really describe its intention. Push your newly created local branch with a new name.

Then rename it because the names are different it can be confusing for your mental juggle.

git branch -m <remote-name>

Push a commit to another branch

If you’re working in a branch and realise you want to move that commit to another branch, here’s how: git push <remote> <branch with new changes>:<branch you are pushing to>

See http://stackoverflow.com/questions/13897717/push-commits-to-another-branch for more information

Rebase branch off another branch

Only do this if you’re the only one working on this branch. This will cause some bad syncing for others.

Make sure the branch you wish to rebase off is up-to-date.

git checkout <branchYouAreWorkingOn>
git rebase <branchToRebaseOff>

If you’re using git v2.0. There is a shortcut to rebase off the previous branch you were on.

- indicates the previous branch.

git checkout <branchToRebaseOff>
git checkout <branchYouAreWorkingOn>
git rebase -

Sync with a forked branch

This is part sync-ing and part branching information. But to me it’s more of a syncing issue.

So you’ve forked someone’s git repo. Their repo is ahead of yours and you need to sync with them.

  1. You need to add in a remote link to on your local git config: click here for more info
         git remote add upstream <the git url of the original repo>
    
  2. Switch to the branch you wish to sync with
        git checkout master
    
  3. Now you’re ready to sync. Detailed guide here.
        git pull upstream master
    

Understanding Merge/Rebase Conflict

If you have diff3-way setup (if you haven’t look here. i highly recommend it), a merge conflict will usually look like this:

cauliflower
<<<<<<< HEAD
peas
potatoes
||||||| merged common ancestors
peas
=======
>>>>>>> topic
tomatoes

So here’s a relation to the columns of a diff tool i use Meld:

Diff view Meld What it means
<<<<<<< HEAD ... |||||||| LOCAL Your local branch file with your own changes
||||||| ... ======= BASE The file before any of your or the merge changes (common denominator)
======= ... >>>>>>> REMOTE The merged branch file with its changes