Wednesday, September 28, 2011

Undo last commit in Git

From Josh's answer on StackOverflow:

git commit --amend

The git commit manual says that --amend, unlike some of the other StackOverflow answers, can be used to amend merge commits.

Fixing merge conflicts in Git

From CoolAJ86's answer on StackOverflow:
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

Tuesday, September 27, 2011

Minecraft server and IPv6

I was having trouble starting a Minecraft server in Ubuntu Natty. I could connect locally, but my friend couldn't connect over the internet.

I used the standard command to start the server.

$ java -Xmx1024M -Xms1024M \
-jar minecraft_server.jar nogui

I noticed that the server was listening on an IPv6 address. My local network and internet connection is IPv4-only, so my friend would obviously be unable to connect.

$ netstat -an | grep 25565
tcp6  0  0  :::25565  :::*  LISTEN

The solution was to pass a property to Java telling it to bind to an IPv4 address.

$ java -Xmx1024M -Xms1024M -Djava.net.preferIPv4Stack=true \
-jar minecraft_server.jar nogui

With that option the server will listen on an IPv4 address.

$ netstat -an | grep 25565
tcp  0  0  0.0.0.0:25565  0.0.0.0:*  LISTEN

Friday, September 23, 2011

Fixing commit authors in Git

I accidentally committed changes with incorrect author information. Brenda Wallace has posted instructions for how rewrite Git's history with new author names.

$ git filter-branch --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "oldname" ];
        then
                GIT_COMMITTER_NAME="Firstname Lastname";
                GIT_AUTHOR_NAME="Firstname Lastname";
                GIT_COMMITTER_EMAIL="your_email@youremail.com";
                GIT_AUTHOR_EMAIL="your_email@youremail.com";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

I also fixed my future commits with the following commands, taken from GitHub's instructions.

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@youremail.com"

Getting git diff of about-to-be-committed changes

I always like to go over the changes I'm committing before I actually commit them. In other version control systems (CVS, Subversion, Mercurial), running cmd diff will show you what you're about to commit. Not so with Git! You need an extra option.

$ git diff --cached