Gallantly GIT

08 January 2015 Contribute to this post

So GIT is gallant now? No, I don’t know… Maybe. I like it, but it did take some time to get over the first many hurdles. I hope this page can help someone in the same situation as I was in.

Show and change remote URL

To see the current remote origin URL:

git remote -v

To change the remote origin URL:

git remote set-url origin <url>

Prepare config

The following can also be done locally to your project, just ignore the --global part.

GIT on Windows like to checkout every file with CRLF endings. We need LF endings, so there are two things you need to do (from command prompt or Cygwin):

git config --global core.eol lf
git config --global core.autocrlf false

Your .gitconfig file should now contain this (as well as other relevant information):

    autocrlf = false
    eol = lf

Also, let’s set up our name and email:

git config --global "mikelothar"
git config --global ""

Revert local changes

To reset all local changes, get the same content as remote master:

git fetch --all
git reset --hard origin/master

Staging files

Different ways to stage changed files, and why git add -A is preferred:

# stages new and modified, without deleted
git add .
# stages modified and deleted, without new
git add -u
# stages all
git add -A

To unstage files:

# removes added
git reset

Git config

Show content of .gitconfig:

git config --global --edit 


Remove unwanted commits to origin/master, return state to how it looked like at commit id e3f1e37:

git reset --hard e3f1e37
git push --force origin master

Move your crazy experiment into a seperate branch

Let’s assume you’ve been committing code for a crazy experiment to the master branch for a while, then realize this should have been in its own branch:

Crazy experiment on master

Create a seperate branch containing this experimental code:

git branch crazy-experiment
git push origin crazy-experiment

Crazy experiment on its own branch

Reset master branch:

git reset --hard 8fa7b51
git push --force origin master

The master branch is back to normal

You can verify your experiment branch is 3 commits ahead of master:

Crazy experiment branch 3 commits ahead of master

Now continue working on your experimental branch:

git checkout crazy-experiment

Reset file

Overwrite content in file with content from same file located on origin/master:

git checkout origin/master -- <file>

Branch names

Show a list of branches on origin:

git fetch origin
git branch -v -a

Ignore changes

To ignore local changes to a tracked file:

git update-index --assume-unchanged <file>

To reverse it (if you ever want to commit changes to it), use:

git update-index  --really-refresh --no-assume-unchanged


git config core.safecrlf false


Create a new repository on GitHub from the command line (if you’re on Windows you’ll need to install curl first):

# Replace USER and REPO
curl -u USER -d '{ "name": "REPO" }'

Create a new project and push it to an existing repository on GitHub:

echo "# REPO" >>
git init
git add
git commit -m "first commit"
# Replace USER and REPO
git remote add origin
git push -u origin master


Push’ing from the command prompt requires you to type your login information each time. You can enable credential caching to avoid this with:

git config credential.helper store

Prepare git message with prefix

If your branch were generated with Jira or similar, you get a branch named something like feature/IU-9547-new-foo-functionality. If you like commit messages like IU-9547: updated-bar but get annoyed with writing the prefix IU-9547: on all your commits, then go into your .git/hooks folder and create a file called prepare-commit-msg containing this:

BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && \
    ! [[ $BRANCH_IN_COMMIT -ge 1 ]] && \
    [[ "$BRANCH_NAME" =~ (.*)?IU-[0-9]{1,10}(.*)? ]]
    JIRA_ID=$(echo $BRANCH_NAME | awk -F'IU-' '{print $NF}' | awk -F'-' '{print "IU-"$NR}')
    sed -i.bak -e "1s/^/$JIRA_ID: /" $1

Make the file executable with chmod +x prepare-commit-msg.

Next time you commit something, the prefix will be the branch name stripped from everything except the Jira ID, for example: IU-9547:.

Using .gitmessage for prefixing

Another way is to use a .gitmessage file (create this file in your home directory). Add this to your .gitconfig to let GIT know you want to use it:

	template = ~/.gitmessage

Then, like before, in your .git/hooks folder, use the post-checkout file, add this:


BRANCH_NAME=$(git symbolic-ref --short HEAD)

if [ -n "$BRANCH_NAME" ] && \
	! [[ $BRANCH_IN_COMMIT -ge 1 ]] && \
	[[ "$BRANCH_NAME" =~ (.*)?IU-[0-9]{1,10}(.*)? ]]
	JIRA_ID=$(echo $BRANCH_NAME | awk -F'IU-' '{print $NF}' | awk -F'-' '{print "IU-"$NR}')
	echo $JIRA_ID:  > ~/.gitmessage

Whenever you check out a new branch, your .gitmessage file will be updated with the JIRA id.