Skip to content

Instantly share code, notes, and snippets.

@Ajedi32
Created March 16, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ajedi32/435888cb5eea4b4016b2 to your computer and use it in GitHub Desktop.
Save Ajedi32/435888cb5eea4b4016b2 to your computer and use it in GitHub Desktop.
Learn git branching level demonstrating how to push remote changes after a rebase
{
"goalTreeString": "%7B%22branches%22%3A%7B%22master%22%3A%7B%22target%22%3A%22C3%22%2C%22id%22%3A%22master%22%2C%22remoteTrackingBranchID%22%3A%22o/master%22%7D%2C%22o/master%22%3A%7B%22target%22%3A%22C3%22%2C%22id%22%3A%22o/master%22%2C%22remoteTrackingBranchID%22%3Anull%7D%2C%22feature%22%3A%7B%22target%22%3A%22C2%27%22%2C%22id%22%3A%22feature%22%2C%22remoteTrackingBranchID%22%3A%22o/feature%22%7D%2C%22o/feature%22%3A%7B%22target%22%3A%22C2%27%22%2C%22id%22%3A%22o/feature%22%2C%22remoteTrackingBranchID%22%3Anull%7D%7D%2C%22commits%22%3A%7B%22C0%22%3A%7B%22parents%22%3A%5B%5D%2C%22id%22%3A%22C0%22%2C%22rootCommit%22%3Atrue%7D%2C%22C1%22%3A%7B%22parents%22%3A%5B%22C0%22%5D%2C%22id%22%3A%22C1%22%7D%2C%22C2%22%3A%7B%22parents%22%3A%5B%22C1%22%5D%2C%22id%22%3A%22C2%22%7D%2C%22C3%22%3A%7B%22parents%22%3A%5B%22C1%22%5D%2C%22id%22%3A%22C3%22%7D%2C%22C2%27%22%3A%7B%22parents%22%3A%5B%22C3%22%5D%2C%22id%22%3A%22C2%27%22%7D%7D%2C%22tags%22%3A%7B%7D%2C%22HEAD%22%3A%7B%22target%22%3A%22feature%22%2C%22id%22%3A%22HEAD%22%7D%2C%22originTree%22%3A%7B%22branches%22%3A%7B%22master%22%3A%7B%22target%22%3A%22C3%22%2C%22id%22%3A%22master%22%2C%22remoteTrackingBranchID%22%3Anull%7D%2C%22feature%22%3A%7B%22target%22%3A%22C2%27%22%2C%22id%22%3A%22feature%22%2C%22remoteTrackingBranchID%22%3Anull%7D%7D%2C%22commits%22%3A%7B%22C0%22%3A%7B%22parents%22%3A%5B%5D%2C%22id%22%3A%22C0%22%2C%22rootCommit%22%3Atrue%7D%2C%22C1%22%3A%7B%22parents%22%3A%5B%22C0%22%5D%2C%22id%22%3A%22C1%22%7D%2C%22C2%22%3A%7B%22parents%22%3A%5B%22C1%22%5D%2C%22id%22%3A%22C2%22%7D%2C%22C3%22%3A%7B%22parents%22%3A%5B%22C1%22%5D%2C%22id%22%3A%22C3%22%7D%2C%22C2%27%22%3A%7B%22parents%22%3A%5B%22C3%22%5D%2C%22id%22%3A%22C2%27%22%7D%7D%2C%22tags%22%3A%7B%7D%2C%22HEAD%22%3A%7B%22target%22%3A%22master%22%2C%22id%22%3A%22HEAD%22%7D%7D%7D",
"solutionCommand": "git checkout master;git pull;git checkout feature;git rebase master;git push --force",
"startTree": "{\"branches\":{\"master\":{\"target\":\"C1\",\"id\":\"master\",\"remoteTrackingBranchID\":\"o/master\"},\"o/master\":{\"target\":\"C1\",\"id\":\"o/master\",\"remoteTrackingBranchID\":null},\"feature\":{\"target\":\"C2\",\"id\":\"feature\",\"remoteTrackingBranchID\":\"o/feature\"},\"o/feature\":{\"target\":\"C2\",\"id\":\"o/feature\",\"remoteTrackingBranchID\":null}},\"commits\":{\"C0\":{\"parents\":[],\"id\":\"C0\",\"rootCommit\":true},\"C1\":{\"parents\":[\"C0\"],\"id\":\"C1\"},\"C2\":{\"parents\":[\"C1\"],\"id\":\"C2\"}},\"tags\":{},\"HEAD\":{\"target\":\"feature\",\"id\":\"HEAD\"},\"originTree\":{\"branches\":{\"master\":{\"target\":\"C3\",\"id\":\"master\",\"remoteTrackingBranchID\":null},\"feature\":{\"target\":\"C2\",\"id\":\"feature\",\"remoteTrackingBranchID\":null}},\"commits\":{\"C0\":{\"parents\":[],\"id\":\"C0\",\"rootCommit\":true},\"C1\":{\"parents\":[\"C0\"],\"id\":\"C1\"},\"C2\":{\"parents\":[\"C1\"],\"id\":\"C2\"},\"C3\":{\"parents\":[\"C1\"],\"id\":\"C3\"}},\"tags\":{},\"HEAD\":{\"target\":\"master\",\"id\":\"HEAD\"}}}",
"hint": {
"en_US": null
},
"name": {
"en_US": "Pushing after rebasing"
},
"startDialog": {
"en_US": {
"childViews": [
{
"type": "ModalAlert",
"options": {
"markdowns": [
"## Pushing rebased changes",
"",
"In this tutorial, we will go over how to push changes to a remote repository after you have rebased your branch."
]
}
},
{
"type": "GitDemonstrationView",
"options": {
"beforeMarkdowns": [
"First, let's examine what happens when you rebase changes on your local machine that you have already pushed to the remote."
],
"afterMarkdowns": [
"Notice how the actual commit (C2) in your branch is not modified. Instead, a new commit is created, and your branch pointer is moved to point to the new commit."
],
"command": "git rebase master",
"beforeCommand": "git checkout -b feature;\ngit commit;\ngit clone;\ngit fakeTeamwork;\ngit checkout master;\ngit pull;\ngit checkout feature;"
}
},
{
"type": "GitDemonstrationView",
"options": {
"beforeMarkdowns": [
"Now let's see what happens when you try to push to the remote.",
""
],
"afterMarkdowns": [
"...nothing. Git will give you an error along the lines of:",
"",
"",
"To origin",
" ! [rejected] master -> master (non-fast-forward)",
"error: failed to push some refs to 'origin'",
"hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Merge the remote changes (e.g. 'git pull') before pushing again.",
"",
"This is because pushing this branch would require deleting C2 from the remote, so that C2' can be added. Since this is a destructive operation, git rejects it.",
"",
""
],
"command": "git push",
"beforeCommand": "git checkout -b feature;\ngit commit;\ngit clone;\ngit fakeTeamwork;\ngit checkout master;\ngit pull;\ngit checkout feature;\ngit rebase master;"
}
},
{
"type": "GitDemonstrationView",
"options": {
"beforeMarkdowns": [
"Now, let's take the advice git gives us in the error message to \"Merge the remote changes (e.g. 'git pull') before pushing again.\" and see what happens.",
"",
"",
""
],
"afterMarkdowns": [
"Uh oh. That doesn't look right. Now there are two copies of C2 on our local branch (C2 and C2'). What went wrong?",
"",
"Well, `git pull` works by fetching the commits from the remote repository and merging them into your current branch. In this case, since C2 wasn't in your current branch (only C2'), it got merged, resulting in both C2 and C2' being in your branch. If you were to run `git push` now, this error would be pushed up to the remote repository as well.",
""
],
"command": "git pull",
"beforeCommand": "git checkout -b feature;\ngit commit;\ngit clone;\ngit fakeTeamwork;\ngit checkout master;\ngit pull;\ngit checkout feature;\ngit rebase master;"
}
},
{
"type": "GitDemonstrationView",
"options": {
"beforeMarkdowns": [
"So what should we do instead?",
"",
"Well, as long as nobody else has added any commits to the remote feature branch, or made a branch off of C2, deleting C2 on the remote shouldn't cause any problems. So that's what we'll do. Normally git doesn't let you delete stuff on remote repositories, which is why `git push` didn't work earlier. We can override that behavior though by using the `--force` option with `git push`."
],
"afterMarkdowns": [
"Hey it worked!",
"",
"Now go ahead and try it yourself!",
""
],
"command": "git push --force",
"beforeCommand": "git checkout -b feature;\ngit commit;\ngit clone;\ngit fakeTeamwork;\ngit checkout master;\ngit pull;\ngit checkout feature;\ngit rebase master;"
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment