{"action":"created","comment":{"url":"https://api.github.com/repos/Haacked/haacked.com/pulls/comments/33597897","id":33597897,"diff_hunk":"@@ -0,0 +1,152 @@\n+---\n+layout: post\n+title: \"Git Alias To Migrate Commits To A Branch\"\n+date: 2015-06-29 -0800\n+comments: true\n+categories: [github git]\n+---\n+\n+Show of hands if this ever happens to you. After a long day of fighting fires at work, you settle into your favorite chair to unwind and write code. Your fingers fly over the keyboard punctuating your code with semi-colons or parentheses or whatever is appropriate.\n+\n+But after a few commits, it dawns on you that you're in the wrong branch. Yeah? Me too. This happens to me all the time because I lack impulse control. You can put your hands down now.\n+\n+### GitHub Flow\n+\n+As you may know, a key component of the [GitHub Flow](https://guides.github.com/introduction/flow/) lightweight workflow is to do all new feature work in a branch. Fixing a bug? Create a branch! Adding a new feature? Create a branch! Need to climb a tree? Well, you get the picture.\n+\n+So what happens when you run into the situation I just described? Are you stuck? Heavens no! [The thing about Git](http://2ndscale.com/rtomayko/2008/the-thing-about-git) is that its very design supports fixing up mistakes after the fact. It's very forgiving in this regard. For example, a recent blog post on the GitHub blog highlights all the different ways [you can undo mistakes in Git](https://github.com/blog/2019-how-to-undo-almost-anything-with-git).\n+\n+### The Easy Case - Fixing `master`\n+\n+This is the simple case. I made commits on master that were intended for a branch off of master. Lets walk through this scenario step by step with some visual aids.\n+\n+The following diagram shows the state of my repository before I got all itchy trigger finger on it.\n+\n+![Initial state](https://cloud.githubusercontent.com/assets/19977/8390497/bbde5f8e-1c4c-11e5-9760-9e94236423d6.png)\n+\n+As you can see, I have two commits to the `master` branch. HEAD points to the tip of my current branch. You can also see a remote tracking branch named `origin/master` (_this is a special branch that tracks the `master` branch on the remote server_). So at this point, my local `master` matches the `master` on the server.\n+\n+This is the state of my repository when I am struck by inspiration and I start to code.\n+\n+![First](https://cloud.githubusercontent.com/assets/19977/8390499/bbe3174a-1c4c-11e5-87ee-75aacf1a197a.png)\n+\n+I make one commit. Then two.\n+\n+![Second Commit - fixing time](https://cloud.githubusercontent.com/assets/19977/8390498/bbe2c524-1c4c-11e5-98c3-7526ae2277f9.png)\n+\n+Each time I make a commit, the local `master` branch is updated to the new commit. Uh oh! As in the scenario in the opening paragraph, I meant to create these two commits on a new branch creatively named `new-branch`. I better fix this up.\n+\n+The first step is to create the new branch. We can create it and check it out all in one step.\n+\n+`git checkout -b new-branch`\n+\n+![checkout a new branch](https://cloud.githubusercontent.com/assets/19977/8411869/0ba24220-1e3b-11e5-9895-42c486709937.png)\n+\n+At this point, both the `new-branch` and `master` point to the same commit. Now I can force the master branch back to its original position.\n+\n+`git branch --force master origin/master`\n+\n+![force branch master](https://cloud.githubusercontent.com/assets/19977/8411975/b05c941e-1e3b-11e5-8e84-f36535fb7893.png)\n+\n+Here's the set of commands that I ran all together.\n+\n+```bash\n+git checkout -b new-branch\n+git branch --force master origin/master\n+```\n+\n+### Fixing up a non-master branch\n+\n+![The wrong branch](https://cloud.githubusercontent.com/assets/19977/8369613/48019364-1b71-11e5-9a28-b748a2802ed7.png)\n+\n+This case is a bit more complicated. Here I have a branch named `wrong-branch` that is my current branch. But I thought I was working in the `master` branch. I make two commits in this branch by `mistake` which causes this fine mess.","path":"_posts/2015/2015-06-29-git-migrate.markdown","position":62,"original_position":62,"commit_id":"599336f015db16ba3946820c47d6da136901f6fe","original_commit_id":"599336f015db16ba3946820c47d6da136901f6fe","user":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"body":"Ha! This `mistake` was a mistake.","created_at":"2015-06-30T17:14:13Z","updated_at":"2015-06-30T17:14:13Z","html_url":"https://github.com/Haacked/haacked.com/pull/205#discussion_r33597897","pull_request_url":"https://api.github.com/repos/Haacked/haacked.com/pulls/205","_links":{"self":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/comments/33597897"},"html":{"href":"https://github.com/Haacked/haacked.com/pull/205#discussion_r33597897"},"pull_request":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/205"}}},"pull_request":{"url":"https://api.github.com/repos/Haacked/haacked.com/pulls/205","id":38719729,"html_url":"https://github.com/Haacked/haacked.com/pull/205","diff_url":"https://github.com/Haacked/haacked.com/pull/205.diff","patch_url":"https://github.com/Haacked/haacked.com/pull/205.patch","issue_url":"https://api.github.com/repos/Haacked/haacked.com/issues/205","number":205,"state":"closed","locked":false,"title":"Git migrate Blog post draft","user":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"body":"- [ ] Still need to resolve the problem with migrating specific ranges of commits\r\n\r\nThe final scenario I want to make work is when you want to migrate a commit range instead of every _local_ commit.\r\n\r\nHere's the alias I have so far:\r\n\r\n```bash\r\n    migrate = \"!f(){ git branch $1 && git reset ${3-'@{u}'} --hard && git rebase --onto ${2-master} ${3-'@{u}'} $1; }; f\"\r\n```\r\n\r\nNow take a look at this tree.\r\n\r\n![](https://cloud.githubusercontent.com/assets/19977/8369614/4801b83a-1b71-11e5-898a-4c116e83b749.png)\r\n\r\nIf I run `git migrate new-branch` everything works great. That would expand to the following commands:\r\n\r\n```bash\r\ngit branch new-branch\r\ngit reset origin/wrong-branch --hard\r\ngit rebase --onto master origin/wrong-branch new-branch\r\n```\r\n\r\nAnd that seems to result correctly in `wrong-branch` being reset to `origin/wrong-branch` AND commits `E` and `F` (but not `D`) migrated to a branch `new-branch` off of master.\r\n\r\nHowever, if I run `git migrate new-branch master HEAD~1` I would expect that `wrong-branch` is reset to `E` (which it is) and that the commit `F` is migrated to the `new-branch` off of `master`. But what happens is that `E` and `F` are rebased, which is a bit confusing to me.\r\n\r\nSo after talking to @mhagger it looks like I might need to add some complexity to the alias so that if you happen to pass in a 3rd parameter, I reset to that parameter, but I rebase onto that parameter~1.\r\n\r\nMaybe I could set the right \"savepoint\" to a variable\r\n\r\n```bash\r\n    migrate = \"!f(){ savepoint = if test -n \"$3\"; then $3^ else @{u} fi; ...\"\r\n```\r\n\r\nThen I'd `reset` to `savepoint` but `rebase --onto` using `savepoint^`.\r\n","created_at":"2015-06-28T04:01:37Z","updated_at":"2015-06-30T17:14:13Z","closed_at":"2015-06-29T17:34:42Z","merged_at":"2015-06-29T17:34:42Z","merge_commit_sha":"903d596a923657f9a586689fce393da181a04246","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Haacked/haacked.com/pulls/205/commits","review_comments_url":"https://api.github.com/repos/Haacked/haacked.com/pulls/205/comments","review_comment_url":"https://api.github.com/repos/Haacked/haacked.com/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Haacked/haacked.com/issues/205/comments","statuses_url":"https://api.github.com/repos/Haacked/haacked.com/statuses/599336f015db16ba3946820c47d6da136901f6fe","head":{"label":"Haacked:git-migrate","ref":"git-migrate","sha":"599336f015db16ba3946820c47d6da136901f6fe","user":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"repo":{"id":14708619,"name":"haacked.com","full_name":"Haacked/haacked.com","owner":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"private":false,"html_url":"https://github.com/Haacked/haacked.com","description":"You've been haacked and you like it","fork":false,"url":"https://api.github.com/repos/Haacked/haacked.com","forks_url":"https://api.github.com/repos/Haacked/haacked.com/forks","keys_url":"https://api.github.com/repos/Haacked/haacked.com/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Haacked/haacked.com/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Haacked/haacked.com/teams","hooks_url":"https://api.github.com/repos/Haacked/haacked.com/hooks","issue_events_url":"https://api.github.com/repos/Haacked/haacked.com/issues/events{/number}","events_url":"https://api.github.com/repos/Haacked/haacked.com/events","assignees_url":"https://api.github.com/repos/Haacked/haacked.com/assignees{/user}","branches_url":"https://api.github.com/repos/Haacked/haacked.com/branches{/branch}","tags_url":"https://api.github.com/repos/Haacked/haacked.com/tags","blobs_url":"https://api.github.com/repos/Haacked/haacked.com/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Haacked/haacked.com/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Haacked/haacked.com/git/refs{/sha}","trees_url":"https://api.github.com/repos/Haacked/haacked.com/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Haacked/haacked.com/statuses/{sha}","languages_url":"https://api.github.com/repos/Haacked/haacked.com/languages","stargazers_url":"https://api.github.com/repos/Haacked/haacked.com/stargazers","contributors_url":"https://api.github.com/repos/Haacked/haacked.com/contributors","subscribers_url":"https://api.github.com/repos/Haacked/haacked.com/subscribers","subscription_url":"https://api.github.com/repos/Haacked/haacked.com/subscription","commits_url":"https://api.github.com/repos/Haacked/haacked.com/commits{/sha}","git_commits_url":"https://api.github.com/repos/Haacked/haacked.com/git/commits{/sha}","comments_url":"https://api.github.com/repos/Haacked/haacked.com/comments{/number}","issue_comment_url":"https://api.github.com/repos/Haacked/haacked.com/issues/comments{/number}","contents_url":"https://api.github.com/repos/Haacked/haacked.com/contents/{+path}","compare_url":"https://api.github.com/repos/Haacked/haacked.com/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Haacked/haacked.com/merges","archive_url":"https://api.github.com/repos/Haacked/haacked.com/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Haacked/haacked.com/downloads","issues_url":"https://api.github.com/repos/Haacked/haacked.com/issues{/number}","pulls_url":"https://api.github.com/repos/Haacked/haacked.com/pulls{/number}","milestones_url":"https://api.github.com/repos/Haacked/haacked.com/milestones{/number}","notifications_url":"https://api.github.com/repos/Haacked/haacked.com/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Haacked/haacked.com/labels{/name}","releases_url":"https://api.github.com/repos/Haacked/haacked.com/releases{/id}","created_at":"2013-11-26T06:12:53Z","updated_at":"2015-06-30T02:06:45Z","pushed_at":"2015-06-30T17:13:54Z","git_url":"git://github.com/Haacked/haacked.com.git","ssh_url":"git@github.com:Haacked/haacked.com.git","clone_url":"https://github.com/Haacked/haacked.com.git","svn_url":"https://github.com/Haacked/haacked.com","homepage":null,"size":264027,"stargazers_count":67,"watchers_count":67,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":115,"mirror_url":null,"open_issues_count":35,"forks":115,"open_issues":35,"watchers":67,"default_branch":"gh-pages"}},"base":{"label":"Haacked:gh-pages","ref":"gh-pages","sha":"3eedbdd19d53021646e1cd30b3b9365e12ffc5c1","user":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"repo":{"id":14708619,"name":"haacked.com","full_name":"Haacked/haacked.com","owner":{"login":"Haacked","id":19977,"avatar_url":"https://avatars.githubusercontent.com/u/19977?v=3","gravatar_id":"","url":"https://api.github.com/users/Haacked","html_url":"https://github.com/Haacked","followers_url":"https://api.github.com/users/Haacked/followers","following_url":"https://api.github.com/users/Haacked/following{/other_user}","gists_url":"https://api.github.com/users/Haacked/gists{/gist_id}","starred_url":"https://api.github.com/users/Haacked/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haacked/subscriptions","organizations_url":"https://api.github.com/users/Haacked/orgs","repos_url":"https://api.github.com/users/Haacked/repos","events_url":"https://api.github.com/users/Haacked/events{/privacy}","received_events_url":"https://api.github.com/users/Haacked/received_events","type":"User","site_admin":true},"private":false,"html_url":"https://github.com/Haacked/haacked.com","description":"You've been haacked and you like it","fork":false,"url":"https://api.github.com/repos/Haacked/haacked.com","forks_url":"https://api.github.com/repos/Haacked/haacked.com/forks","keys_url":"https://api.github.com/repos/Haacked/haacked.com/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Haacked/haacked.com/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Haacked/haacked.com/teams","hooks_url":"https://api.github.com/repos/Haacked/haacked.com/hooks","issue_events_url":"https://api.github.com/repos/Haacked/haacked.com/issues/events{/number}","events_url":"https://api.github.com/repos/Haacked/haacked.com/events","assignees_url":"https://api.github.com/repos/Haacked/haacked.com/assignees{/user}","branches_url":"https://api.github.com/repos/Haacked/haacked.com/branches{/branch}","tags_url":"https://api.github.com/repos/Haacked/haacked.com/tags","blobs_url":"https://api.github.com/repos/Haacked/haacked.com/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Haacked/haacked.com/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Haacked/haacked.com/git/refs{/sha}","trees_url":"https://api.github.com/repos/Haacked/haacked.com/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Haacked/haacked.com/statuses/{sha}","languages_url":"https://api.github.com/repos/Haacked/haacked.com/languages","stargazers_url":"https://api.github.com/repos/Haacked/haacked.com/stargazers","contributors_url":"https://api.github.com/repos/Haacked/haacked.com/contributors","subscribers_url":"https://api.github.com/repos/Haacked/haacked.com/subscribers","subscription_url":"https://api.github.com/repos/Haacked/haacked.com/subscription","commits_url":"https://api.github.com/repos/Haacked/haacked.com/commits{/sha}","git_commits_url":"https://api.github.com/repos/Haacked/haacked.com/git/commits{/sha}","comments_url":"https://api.github.com/repos/Haacked/haacked.com/comments{/number}","issue_comment_url":"https://api.github.com/repos/Haacked/haacked.com/issues/comments{/number}","contents_url":"https://api.github.com/repos/Haacked/haacked.com/contents/{+path}","compare_url":"https://api.github.com/repos/Haacked/haacked.com/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Haacked/haacked.com/merges","archive_url":"https://api.github.com/repos/Haacked/haacked.com/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Haacked/haacked.com/downloads","issues_url":"https://api.github.com/repos/Haacked/haacked.com/issues{/number}","pulls_url":"https://api.github.com/repos/Haacked/haacked.com/pulls{/number}","milestones_url":"https://api.github.com/repos/Haacked/haacked.com/milestones{/number}","notifications_url":"https://api.github.com/repos/Haacked/haacked.com/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Haacked/haacked.com/labels{/name}","releases_url":"https://api.github.com/repos/Haacked/haacked.com/releases{/id}","created_at":"2013-11-26T06:12:53Z","updated_at":"2015-06-30T02:06:45Z","pushed_at":"2015-06-30T17:13:54Z","git_url":"git://github.com/Haacked/haacked.com.git","ssh_url":"git@github.com:Haacked/haacked.com.git","clone_url":"https://github.com/Haacked/haacked.com.git","svn_url":"https://github.com/Haacked/haacked.com","homepage":null,"size":264027,"stargazers_count":67,"watchers_count":67,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":115,"mirror_url":null,"open_issues_count":35,"forks":115,"open_issues":35,"watchers":67,"default_branch":"gh-pages"}},"_links":{"self":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/205"},"html":{"href":"https://github.com/Haacked/haacked.com/pull/205"},"issue":{"href":"https://api.github.com/repos/Haacked/haacked.com/issues/205"},"comments":{"href":"https://api.github.com/repos/Haacked/haacked.com/issues/205/comments"},"review_comments":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/205/comments"},"review_comment":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Haacked/haacked.com/pulls/205/commits"},"statuses":{"href":"https://api.github.com/repos/Haacked/haacked.com/statuses/599336f015db16ba3946820c47d6da136901f6fe"}}}}