Gallantly GIT

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>

GIT on Windows

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):

[core]
    autocrlf = false
    eol = lf

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 

Rollback

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

Misc

git config core.safecrlf false

GitHub

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 https://api.github.com/user/repos -d '{ "name": "REPO" }'

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

echo "# REPO" >> README.md
git init
git add README.md
git commit -m "first commit"
# Replace USER and REPO
git remote add origin https://github.com/USER/REPO.git
git push -u origin master

Credentials

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