{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rednafi.com/misc/when-to-use-git-pull-rebase/",
  "description": "Fix divergent branch errors with git pull --rebase. Learn when to rebase local commits on top of remote changes for cleaner Git history.",
  "path": "/misc/when-to-use-git-pull-rebase/",
  "publishedAt": "2022-07-14T00:00:00.000Z",
  "site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
  "tags": [
    "Git",
    "TIL",
    "GitHub"
  ],
  "textContent": "Whenever your local branch diverges from the remote branch, you can't directly pull from the\nremote branch and merge it into the local branch. This can happen when, for example:\n\n- You checkout from the main branch to work on a feature in a branch named alice.\n- When you're done, you merge alice into main.\n- After that, if you try to pull the main branch from remote again and the content of the\n  main branch changes by this time, you'll encounter a merge error.\n\nReproduce the issue\n\nCreate a new branch named alice from main. Run:\n\nFrom alice branch, add a line to a newly created file foo.txt:\n\nAdd, commit, and push the branch:\n\nFrom the GitHub UI, send a pull request against the main branch and merge it:\n\n![GitHub pull request UI showing merge from alice branch to main][image_1]\n\nIn your local machine, switch to main and try to pull the latest content merged from the\nalice branch. You'll encounter the following error:\n\nThis means that the history of your local main branch and the remote main branch have\ndiverged and they aren't reconciliable.\n\nSolution\n\nFrom the main branch, you can run:\n\nThis will rebase your local main by adding your local commits on top of the remote\ncommits.\n\nFurther reading\n\n- [When should I use git pull --rebase]\n- [An example repo that reproduces the issue]\n\n\n\n\n[when should i use git pull --rebase]:\n    https://stackoverflow.com/questions/2472254/when-should-i-use-git-pull-rebase\n\n[an example repo that reproduces the issue]:\n    https://github.com/rednafi/_pull-rebase\n\n[image_1]:\n    https://blob.rednafi.com/static/images/when_to_use_git_pull_rebase/img_1.png",
  "title": "When to use 'git pull --rebase'"
}