Skip to content

Instantly share code, notes, and snippets.

@in5ikt
Created February 8, 2011 22:44
Show Gist options
  • Save in5ikt/817448 to your computer and use it in GitHub Desktop.
Save in5ikt/817448 to your computer and use it in GitHub Desktop.
Minecraft - Versioning your saves

Minecraft - Versioning your saves

This is a tutorial show how to version your saves using Git.

Q: Why would anyone go through this process?
A: Because if you do, you can undo an otherwise un-undoable action, such as swimming in a pit of lava and accidentally dropping your Notch armpit forged special edition diamond pickaxe and your 35 stacks of cobblestone.

Step by step tutorial

Download and install the appropriate version of Git

  • Linux
    • Ubuntu/Debian
      1. sudo aptitude install git
    • Other dists
      1. Use the source, Luke.
  • Windows
    1. Download msysGit
      I would choose the "Full installer for official Git 1.7.4" -- or in a few years; "Full installer for official Git \d(.\d(.\d)?)?"
    2. Install it!
  • Mac
    • I'd love to provide more details, but I do not have access to a Mac OS X environment.
      Installers are found on the git-osx-installer download page. I wish you good luck even though I am sure it is no more confusing than the Windows installer.

Find your saves directory

  • Windows
    • As described in a post on the Minecraft forum
      1. Open "Run" from the start menu
      2. Enter %appdata%/.minecraft/saves in the box
      3. Press RETURN If this does not take you to a folder looking somewhat like this, check the forum thread for further directions.
        My saves folder
  • Linux
    • I can't confirm this, but I suspect it might be in $HOME/.minecraft/saves
      A StackExchange Gaming post confirms that the save folder is indeed located at ~/.minecraft/saves
  • Mac

The worlds are all logically named after this view in minecraft:
My minecraft saves

Set up the git repository

If any of the things mentioned in this section should spawn questions in your head that you absolutely want answered, try the Git Immersion git tutorial. It will teach you git from the bottom and up.

If the Git Immersion tutorial does not answer your questions, try Google. There is an almost infinite amount of posts, discussions and manuals available to answer your questions. Some of them do it better than the others.

This chapter and the following pages of the Git Immersion tutorial concerns the points mentioned in this tutorial.

The only difference between this case and the cases mentioned in the Git Immersion tutorial is that we are mostly dealing with binary files and not text files.

  1. Fire up your command-line interface.

    • Windows
      • StartProgramsGitGit Bash
    • Mac
      • Spotlight ⇒ Search for "Terminal" — Or something similar to this
    • Linux
      • Well, duh. Hints: "Terminal", xterm etc.
  2. Change the current working directory for your command-line-interface to the appropriate World[1-5] folder in the saves directory

    cd /c/Users/YOURNAME/AppData/Roaming/.minecraft/saves/World1
    

    for World 1.

  3. Initialize the git repository

     git init
    

    git-init

     # Notice the dot
     git add .
     
     # You might add -s after, If you have set up your user.name and user.email values in your git configuration
     git commit -m "Initial commit"
    

    git add . should output nothing but a blank space. git commit [...] should output something like this
    git-commit

You now have a snapshot of your selected minecraft world.

The correct way to do something really stupid and unnecessary

  1. Run

    git add .
    git commit -m "Before stupidly walking into lava, an event which I have not at all planned. This is just in case"
    

    This creates a new save point for you, a savepoint that you can revert back to.

    By running

    git log
    

    you should be able to see the "savepoint" or commit, as git calls it. Mine is called "Befor(e) dying". git-log

  2. Load your world

  3. Do something clumsy, unfortunate or just plain stupid.
    found-lava
    fffffffuuuuuuuuuuuu

  4. ?????!?!?!?!

  5. Run

    git log
    

    this will show you the most recent commits with the most recent commit at the top
    git-log

    You will need the SHA1 value that is associated with the specific commit. In this case 07c1925969e2bd30fa4e940656c7ab8573f1ddb0.

  6. Run

    git reset --hard y0ursha1c0mm1774gv41u3
    # WARNING: This step is to my knowledge irreversible; once you back, you never go back.
    

    this will return

    HEAD is now at l0l0l0l Before stupidly walking into lava, an event which I have not at all planned. This is just in case
    

    git-reset

@equalent
Copy link

Would you recommend this method for distributing the saves on different computers given it wont be played on both computers in the same time?

Git is not really for binary data. Subversion (SVN) or Perforce (P4) are better for saving worlds and server data.
If you really want to use Git, use Git LFS (your life will be easier)

@JonDum
Copy link

JonDum commented Sep 20, 2019

I know this is old as dirt, but

git reset --hard y0ursha1c0mm1774gv41u3
# WARNING: This step is to my knowledge irreversible; once you back, you never go back.

Nah, this is just telling git to "rollback" the branch tip and working tree to that commit. You can "roll forward" back to the previous tip by finding the commit in the reflog. Git tries it's best to never let you fuck up unrecoverably.

@ryantheleach
Copy link

A lot of fun can be had, converting NBT to text for diffing as well. But I think for chunk/block data, a visualizer that renders regions to pictures would be better.

Merging will be a utter cow however.

@TugdualKerjan
Copy link

Fantastic idea!! To think minecraft has changed so much since :)

@Radon8472
Copy link

The most simple way for a diff view is:

[diff "minecraft_mca"]
         textconv = "php.exe -r 'readfile(\"compress.zlib://$argv[1]\");'";

and a .gitattributes file like this

*.mca            diff=minecraft_nbt
*.nbt            diff=minecraft_nbt
level.dat        diff=minecraft_nbt
*.dat            diff=minecraft_nbt

But better would be to use PHP-NBT-Decoder-Encoder for textconv.
I wrote a little nbt-gitdiff.php script, but it is still in experimental state.

Change the gitconfig line above to

[diff "minecraft_mca"]
         textconv = "'d:/YOUR_PATH_TO_SCRIPT/nbt-gitdiff.php'"

if you like to use this

@Radon8472
Copy link

p.s. @in5ikt you should change the downloadlink for git in windows to https://git-scm.com/download/win

@ludvary
Copy link

ludvary commented Oct 26, 2022

holy shit its been 12 years!

@themixhelp
Copy link

awesome ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment