Notes on Git

From Ggl's wiki

Jump to: navigation, search

Contents

Introduction

Git is a distributed source code manager (SCM). It is a set of tools that helps to track and manage modifications to file.

Configuration

Configure git:

git config --global user.email 'me@mydomain'
git config --global user.name 'me'

If the current directory is a git repository and you omit the --global switch, it sets the configuration for the current repository.

You can also set some convenience parameters:

git config color.diff auto
git config alias.st status
git config alias.stat 'diff --stat'
git config alias.mt 'mergetool --tool=vimdiff'
git config alias.co 'checkout'

You don't need to type again all these commands. Save the file ~/.gitconfig and copy it where you want to restore your git preferences.

Initialize the code repository

To initialize a directory as a git repository:

git init

Add a .gitignore file to avoid tracking irrelevant files. Add a README to describe the project. Then add the current directory:

git add .
git commit -m 'first commit'

Add a remote shared repository

To create a shared repository:

git init --bare --shared

Add you public ssh key to access to the remote directory. Check the files permissions.

To add a remote repository:

git remote add origin git+ssh://my.remote.server/home/me/src/gitrepo/

Now the remote 'origin' appears in 'git remote'.

Push the code to the remote repository:

git push origin

When you want to update you local repository with the remote changes:

git pull origin

pull is fetch and merge. If you only want to review changes on the remote repository, use 'git fetch', then 'git diff' or 'git log'.

Basic Workflow

Whenever you want to work on a feature or a task, create a branch:

git checkout -t -b newbranch master

Modify the code, then commit. Try to commit the smallest changes you can. If you want to selectively add modifications to commit use 'git add -p'. If you need to interrupt your work, don't commit a inconsistent modification. Stash the modifications with 'git stash', then switch to a new branch, modify the new branch, commit in the new branch, merge if it's ok, and delete the branch. When you're done with the other task, switch back to the branch you were working on and pop the changes you stashed with 'git stash pop'. You can inspect the stash with 'git stash list' and 'git stash show -p stash@{0}'.

If you need to cancel all the changes since the last commit, 'git reset'.

When you have commited and tested all the changes you wanted, you may group some of them. Use 'git rebase -i' to "squash" commits or edit commit message. You should edit commits before having pushing them.

After you have squashed and edited the commits in the branch you merge the branch into the main development branch:

git checkout master
git merge --no-ff newbranch

Now test everything works as expected. If it's ok, delete the branch:

git branch -d newbranch

When you have finished a milestone, tag the branch with 'git tag'

note: testing is critical to maintain a sane workflow.

Tools

  • gitk: tk git UI, useful to visualize the branches
  • tig: console curses repository browser
  • git email: useful scripts to make mail workflow easier (creating patches from a set of commits, sending patches, applying diffs, etc...)

References

Personal tools