RSS
 

git “Getting Started” Tips

21 Jan

git is has become one of the most popular source code control tools on the planet. Even if you’re coming from another source control system, becoming proficient with git can take some time. The best place to start is by running through the git Tutorial. Before that, you might take a look at the GitGuys article to get a quick synopsis of git concepts. Also, though slightly out of date, keeping the following diagram in front of you will help as well. It shows the git command-line commands that “move” file changes from one place to another in the git data-store hierarchy.

Those references should be enough to get up and running quickly, but you might add the following setup features to make yourself productive in a hurry.

Graphical Interfaces

I use the command-line for most of my git interactions, but it is nice to have a graphical view. The gitk graphical tool is readily available,  SourceTree is a much better alternative, free, and available for both Windows and OS X.

Bash Command-line

I often prefer to use the command-line. If you are using Bash (or other Unix-y command processor), then there are some features that can make life easier.

Command-line Autocompletion

The auto-completion of Bash enables the tab-key to complete parameters whose text is partially entered. This is particularly useful when the variety of parameters are hard to remember. There is a great one that can be used by downloading and running git-completion.bash. Invoke this from your .profile, .bash_profile, or other startup script.

Command-line Aliases

I find it useful to define common git command aliases (this can be done in Windows too, but differently):

  • commit — Depending on how you use git, it may be useful to be able to commit your changes without having to “add” them. Furthermore, committing when your branch is “behind” its corresponding remote causes a merge to occur, unless the local branch is updated first. 
  • master — Since the master branch is where most activity usually happens, it is useful to be able to go back there quickly.

[bash light=”true”]
alias commit=’update; git commit -a’
alias revert=’git revert’
alias status=’git status’
alias update=’git pull’
alias push=’git push’
alias master=’git checkout master’
[/bash]

Git Aliases

It’s nice to be able to define your own git “commands” via aliases. Whether it is to simply syntax for commonly used git commands or to mimic other tools that you’re used to, using aliases can help ease the burden on your mental capacity. You can’t use aliases to redefine git commands, but you can add to them.

[bash light=”true”]
git config –global alias.man help
git config –global alias.update pull
git config –global alias.pop ‘stash pop’
[/bash]

Aliases can invoke external commands. I have a script which allows me to add entries to the .gitignore file without having to edit it. So, I can create an “ignore” command which invokes a script named git-ignore:

[bash light=”true”]
git config –global alias.ignore ‘!git-ignore’
[/bash]

Then I can simply say:

[bash light=”true”]
git ignore file1 file "*.tmp"
[/bash]

Which invokes the following script, named “git-ignore”:

[bash]
## Ignore specified filespecs by adding them to the .gitignore file.
##
## Use:
## git config –global alias.ignore ‘!git-ignore’

gitfile=’.gitignore’

## If no parameters, list current ignored files.
if [ $# -eq 0 ]; then
if [ -r $gitfile ]; then
more $gitfile
else
echo No files ignored.
fi
exit
fi

## For each filespec, add it to ignore list.
for spec in "$@"; do
echo Adding "$spec"
echo "$spec" >>$gitfile
done
[/bash]

Mac OS X setup

It is convenient to tie git authentication credentials in with OS X’s keychain. If you installed git using homebrew or installed git as described in my prior post, OS X Command-line Dev Tools—Install Shortcut, you should already have the osxkeychain helper. You can verify this by trying to run it:

[bash light=”true”]
git credential-osxkeychain
[/bash]

If the first command is not found follow the instructions at Git Credential Caching on Mac OS X:

If you do not have the helper, you can download and install it like so:

[bash light=”true”]
curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
sudo mkdir /usr/local
sudo mkdir /usr/local/bin
sudo mv git-credential-osxkeychain /usr/local/bin/
sudo chmod u+x /usr/local/bin/git-credential-osxkeychain
[/bash]

To tell git to use osxkeychain, simply set the global git config:

[bash light=”true”]
git config –global credential.helper osxkeychain
[/bash]

The next time you clone an HTTPS URL that requires a password you will be prompted for your username and password, and to grant access to the OS X keychain. After you’ve done this, the username and password are stored in your keychain and you won’t be required to type them in to git again.

Note: If this doesn’t work, try updating git to a newer version. See this stackoverflow article.

Cygwin

Cygwin provides a set of tools and a command-line Bash environment that understands and integrates well with the Windows environment. With it, you can install a command-line version of git. If you are using SourceTree, then you may want to make sure that the settings for the command line  are consistent with what SourceTree is using.

This setting sets a global ignore pattern file to share with SourceTree system-wide setting Tools|Options|Git|Global Ignore List file entry.

[bash light=”true”]
git config –system core.excludesfile ${USERPROFILE}’\Documents\gitignore_global.txt’
[/bash]

Resources

  • More useful aliases: https://git.wiki.kernel.org/index.php/Aliases
  • http://durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/
 
 

Tags: , , , , , , , ,