What do you think? Discuss, post comments, or ask questions at the end of this article [More about me]

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

(heart) git.  No, seriously...  I've also used several git clients over my short lifetime, my current favourite is the awesome and beautiful (and beautifully cross-platform) gitkraken.

However, sometimes I need to do stuff that's just easier in git bash.  Below are several of these commands that I always seem to forget (dang you nice git client - making me forget my terminal roots...).

git reset back to previous commit

Sometimes you might want to take a local branch back to a previous commit (or state... like in between several amend commits) without actually reverting commits.

First, have a look at your reflog.  For example, to look at the previous 10 git operations do

git reflog -10

This will show a list of hashes (representing commits) and the actions taken, or commit message.

Now, copy the hash and do

git reset --soft <hash>

Now, this will point HEAD to the selected hash state.

Simply reset and you're back to a previous commit:

git reset --hard HEAD

Create orphan branch

In some instances you might need/want to create a branch which is not based on another branch.  This is known as an orphan branch.  Some git front-ends provide a nice way of doing this but many (even good ones) do not.  No matter, you can always create one from the command line, like so:

git checkout --orphan <name-of-orphan-branch>
<add/remove files to the working directory as desired>
git add .
git commit -m '<message here>'

.gitignore not working

A situation can occur where you've already committed a file to the repo, and then later on try to add the same file to .gitignore.  In this case, it may appear that .gitignore is not working (git is actually working as designed).

You'll need to remove the previously added files (that you now want to ignore) from the repo.  Fortunately, this is quite easy to do by running the following commands in git bash:

git rm . -r --cached
git add .

You'll note that now the files you previously added (but now what to ignore) have been deleted from the working folder.

Now commit and things will be working as expected. 

git can't checkout on windows due to filename too long...

If you're using a git client on windows that uses msysgit as the backend, it might have been compiled to only accept filenames no longer than 260 characters.  If this affects you, you can workaround the issue by opening a git bash window (as admin) and:

git config --system core.longpaths true

Keeping git clean and happy

git generally is great at keeping things clean and in order.  I was on a project using a system that wasn't designed for source code management (but git still worked great!) and we had to commit several binary files.

I found that the following process really helped in keeping the repo in check (especially sizes due having to commit and comment on binary files often).

Get to git bash

In git extensions (for example) go to 'tools' menus, and select 'Git bash'.

Expire your reflog!

git reflog expire --expire=now --all

Prune!

git prune

(use 'git prune -n' to see what commits will be pruned without actually pruning).

Done...

unless you want to garbage collect and recompress everything (only do every so often to save space and house clean).

git gc

References

  1. https://stackoverflow.com/questions/22575662/filename-too-long-in-git-for-windows

  • No labels