{"id":22831,"date":"2020-09-17T14:34:26","date_gmt":"2020-09-17T21:34:26","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=22831"},"modified":"2023-12-01T04:00:11","modified_gmt":"2023-12-01T12:00:11","slug":"git-move-commit-to-another-branch","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/","title":{"rendered":"Git: Move Commit to Another Branch"},"content":{"rendered":"\n<p><a href=\"https:\/\/careerkarma.com\/blog\/what-is-git\/\">Code in Git projects is stored in branches<\/a>. Each branch is an independent line of development in which you can make changes to files.<br><\/p>\n\n\n\n<p>You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.<br><\/p>\n\n\n\n<p>In this guide, we discuss how to move a commit to a different branch. We walk through two methods of moving a commit to a different branch: moving to an existing branch, and a new branch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git Branching: A Refresher<\/h2>\n\n\n\n<p><a href=\"https:\/\/careerkarma.com\/blog\/git-branch\/\">Branches<\/a> let you work on two different versions of a project at the same time.<br><\/p>\n\n\n\n<p>This is useful if you want to fix a bug without changing the main version of a project until that bug is properly fixed. Branches are commonly used when implementing features to keep those features away from the main project until they are ready to be released.<br><\/p>\n\n\n\n<p>You can move a commit to another branch using the Git command line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git: Move a Commit to a New Branch<\/h2>\n\n\n\n<p>We\u2019re working on a project and we want to work on a new feature.<br><\/p>\n\n\n\n<p>We want to move a commit we\u2019ve made to the master branch to a new branch called new-feed. This is because the commit will likely be part of a series of commits about a feature and the feature is not ready for a wide release.<br><\/p>\n\n\n\n<p>To move the commit to a new branch, we must first create a new branch:<br><\/p>\n\n\n\n<p><code>git branch new-feed<br><\/code><\/p>\n\n\n\n<p>This command creates a branch containing all the code currently on the \u201cmaster\u201d branch.&nbsp;<br><\/p>\n\n\n\n<p>Now that we have a new branch, we can move our master branch back by one commit so it does not contain the code we\u2019ve written toward the new feature:<br><\/p>\n\n\n\n<p><code>git reset --hard HEAD~1<br><\/code><\/p>\n\n\n\n<p>We are <a href=\"https:\/\/careerkarma.com\/blog\/git-reset\/\">resetting the master branch<\/a> to the state it was in before the last commit. We can change the number 1 to reflect any number of commits we want to move back.<br><\/p>\n\n\n\n<p>Alternatively, you can specify the hash of a commit if you are not sure how many commits you want to move back relative to the current state of the repository.<br><\/p>\n\n\n\n<p>The next step is to checkout to the new branch to see the version of our project with the features we\u2019ve written:<br><\/p>\n\n\n\n<p><code>git checkout new-feed<br><\/code><\/p>\n\n\n\n<p>Our commit has been moved to the new branch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git: Move a Commit to an Existing Branch<\/h2>\n\n\n\n<p>You can move a commit to an existing branch. To start, navigate to the branch to which we want to move our commit using the <a href=\"https:\/\/careerkarma.com\/blog\/git-checkout\/\">git checkout command<\/a>:<br><\/p>\n\n\n\n<p><code>git checkout new-feed-2<br><\/code><\/p>\n\n\n\n<p>We are now viewing the new-feed-2 branch. Next, we\u2019re going to <a href=\"https:\/\/careerkarma.com\/blog\/git-merge\/\">merge all of the changes<\/a> we have made to the master branch into the new-feed-2 branch. This will ensure both branches contain the same code:<br><\/p>\n\n\n\n<p><code>git merge master<br><\/code><\/p>\n\n\n\n<p>Now that the new-feed-2 branch contains our changes, we can move back to master and reset our repository to the commit before the one where we pushed our changes:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>git checkout master\ngit reset --hard HEAD~1\n\n<\/pre><\/div>\n\n\n\n<p>These commands move our master branch back by one commit. This means that our new branch, new-feed-2, contains the code from our changes. The master branch appears in the way it did before we created that commit and pushed it to the master branch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You can move a commit to another branch, whether it\u2019s a new branch or an existing one. This is because branches let you maintain independent versions of a project. A new branch contains all of the history of an existing branch and then can create its own history.<br><\/p>\n\n\n\n<p>Now you\u2019re ready to move commits to another branch in Git like a professional developer.<br><\/p>\n","protected":false},"excerpt":{"rendered":"Code in Git projects is stored in branches. Each branch is an independent line of development in which you can make changes to files. You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes. In&hellip;","protected":false},"author":240,"featured_media":21615,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17286],"tags":[],"class_list":{"0":"post-22831","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-git"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Git","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":[]},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Git: Move Commit to Another Branch | Career Karma<\/title>\n<meta name=\"description\" content=\"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git: Move Commit to Another Branch\" \/>\n<meta property=\"og:description\" content=\"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-17T21:34:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:00:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Git: Move Commit to Another Branch\",\"datePublished\":\"2020-09-17T21:34:26+00:00\",\"dateModified\":\"2023-12-01T12:00:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\"},\"wordCount\":611,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"articleSection\":[\"Git\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\",\"name\":\"Git: Move Commit to Another Branch | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"datePublished\":\"2020-09-17T21:34:26+00:00\",\"dateModified\":\"2023-12-01T12:00:11+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg\",\"width\":1020,\"height\":680,\"caption\":\"A black and silver laptop with code running on a table next to a plant and a yellow mug.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Git\",\"item\":\"https:\/\/careerkarma.com\/blog\/git\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Git: Move Commit to Another Branch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Git: Move Commit to Another Branch | Career Karma","description":"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/","og_locale":"en_US","og_type":"article","og_title":"Git: Move Commit to Another Branch","og_description":"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.","og_url":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-09-17T21:34:26+00:00","article_modified_time":"2023-12-01T12:00:11+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Git: Move Commit to Another Branch","datePublished":"2020-09-17T21:34:26+00:00","dateModified":"2023-12-01T12:00:11+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/"},"wordCount":611,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","articleSection":["Git"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/","url":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/","name":"Git: Move Commit to Another Branch | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","datePublished":"2020-09-17T21:34:26+00:00","dateModified":"2023-12-01T12:00:11+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"On Career Karma, learn how to move a commit from one branch to a new branch or an existing branch in a Git repository.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/clement-h-95YRwf6CNw8-unsplash.jpg","width":1020,"height":680,"caption":"A black and silver laptop with code running on a table next to a plant and a yellow mug."},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/git-move-commit-to-another-branch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Git","item":"https:\/\/careerkarma.com\/blog\/git\/"},{"@type":"ListItem","position":3,"name":"Git: Move Commit to Another Branch"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=22831"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22831\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/21615"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=22831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=22831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=22831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}