Skip to content

Temp Branch on an Earlier Commit

Folds edited this page Jul 5, 2017 · 5 revisions

Git and GitKraken offer three options for resetting a branch to a previous commit. All three of these options change which commit is the highlighted commit in the "Commit Graph" portion of the user interface:

  • Soft - keep all changes
  • Mixed - keep working copy, but reset index
  • Hard - discard all changes

If what you want to do is:

  • keep all existing commits in the repository,
  • change which commit is the highlighted commit in the "Commit Graph" portion of the GitKraken user interface, and
  • swap out the contents of the working directory to match the highlighted commit,

none of these three choices are appropriate.

Instead, create a temporary new branch at the commit you want to highlight. For example, you might want to do this as part of the following scenario:

  1. Suppose you have a folder (where you really do your work) that is separate from the folder that Git thinks is your working directory. When you are ready to check stuff in, you copy the updated files and subfolders from "where you really do your work" to Git's working directory. When you need to get an old copy of a file, you can use this procedure to do so.
  2. Suppose you are working in a branch, such as dev.
  3. Check in a commit (A), in the working branch. By default, dev's head will now point to A, and A will now be the highlighted commit.
  4. Check in another commit (B). By default, dev's head will now point to B, and B will now be the highlighted commit.
  5. Decide that you would rather have A than B, but want to keep B in the dev's mainline. This scenario assumes that you do not have any unsaved work that you might need to stash.
  6. In GitKraken's "Commit Graph" portion of the user interface, right-click on commit A. Choose "Create branch here". Create a temporary branch, such as temp. dev will still exist, and will still point to B. The new temp branch will become the new working branch. The contents of the working directory will be updated to match commit A, and A will now be the highlighted commit.
  7. Copy the relevant files from Git's working directory to the place where you really do your work.
  8. Right-click dev's branch label, and checkout dev. dev's head still points to B, and B is now highlighted. temp still exists, and its head still points to A.
  9. Right-click temp's branch label, and delete temp. GitKraken will warn you that 'This is a destructive operation, are you sure you want to delete "temp"?'. Confirm the delete. Although you will not be able to use "Undo" to re-create temp, it is easy enough to re-create the temp branch manually if you have to. After all, all of its contents are part of the still-extant dev branch.
  10. Copy the relevant files from the place where you really do your work, back to Git's working directory.
  11. Stage, name, optionally describe, and save a commit (C) that has the same contents as commit A. dev's head now points to C, and C is now highlighted.

Voila!