четвъртък, 1 ноември 2012 г.

git goodness (pt. 1)

This post is intended for people, who know the basics of git and want to go to the next level ;-)

Branches

A git branch is just a reference to a commit. You can easily change the reference by git reset --hard <refspec>. The first time you do this you are most probably going to lose some commits :) To see the commits you were on recently, try git reflog, and then another git reset --hard <refspec> to point your branch to the right commit.

Remote branches

These are branches on remote repositories. They are read only -- that is, you can not change where they point to by hand. You can update them with git fetch <remote>. Note that git pull does a fetch and a merge/rebase, so it would update them too.

By default you do not have local branches representing all remote branches, just for master. If you have a remote origin with a branch coolbranch, just use git checkout -b coolbranch origin/coolbranch

Deleting remote branches is odd. Use git push origin :branchtodelete and don't ask me why :)

Tracking branches

You can specify that a local branch is linked to a remote branch, so that git pull and git push work without additional arguments.

If you created a new branch on your local machine and want to push it (to say, github.com), you would do git push -u origin newbranch. -u tells git to set newbranch as tracking origin/newbranch.

When you start working on a new branch from origin, if you do git checkout -b branchname it sets local branch branchname as tracking origin/branchname.

gitconfig

User-wide git configuration resides in ~/.gitconfig. Here is mine, I think its good enough to be shared ;-)

[user]
  email = your.name@email.com
  name = Your Name
[alias]
  st = status
  ci = commit
  co = checkout
  l = log --graph --decorate --pretty=oneline --abbrev-commit --all
[color]
  diff = auto
  log = auto
  status = auto
  branch = auto

Aliases are a great way to save you from typing the most common commands. I also had the habit of using st and co from svn and mercurial, so if you're also used to that add them in.

Everybody likes fancy colors nowadays (even me!), they also make the output more readable. To enable colorful output just include the [color] section and list the commands you wish to see in color. You can also specify the colors used, check the docs for more info on that

Read more about the long line in the next section

history

Checking the repo history is a fundamental operation. I'll explain the most useful options here:

git log <commit>

Lists all ancestor commits of the given commit, or HEAD if no commit is given. The commit hash, the author and the full commit message are shown. Use this if you want to search the history for a given commit (sha128) or words you hope to find in the commit message.

git log -p <commit>

The same as above, but also include the diffs of each commit. Very handy if you want to check the history of something in the source code like a function or variable.

git log <path/to/file>

You may also check the history of a particular file.

git log --graph --decorate --pretty=oneline --abbrev-commit --all

I use this recipe to get an ascii graph of all commits in the repo. I use it so often that I aliased it with git l (check gitconfig above). Experiment with a subset of the options to see what they do.

To be continued...

This turned out longer than I expected. I hope I can post more on this topic, more specifically interactive add, rebase, cherry-pick. Stay tuned.

Няма коментари: