Git: CVS equivalent operations

From genomewiki
Revision as of 18:01, 14 June 2010 by Galt (talk | contribs)
Jump to navigationJump to search

Check out a source tree: cvs co kent

cd $HOME mv kent kent-cvs-old # only if you have an existing cvs kent sandbox git clone yourlogin@hgwdev.cse.ucsc.edu:/data/git/kent.git/ kent cd kent


Update a source tree: cvs up

git pull # often this is all you need to do on your master branch

or

git fetch

fetch allows getting updates and comparing without merging. pull = fetch + merge.


Check in a changed file: cvs commit

git add changed-file git status git commit -m 'I fixed something important'

Note that git commit -a is very dangerous since it will grab any change anywhere in your entire directory tree and commit it. Do not use.

Instead, use this to add only tracked files at or below the current directory. This makes it behave like cvs commit which is relative to the current dir:

git add . -u # DO NOT FORGET THE -u or you will be sorry.

At least with this method you can still use git status to check what you have.

git push

 (may need to do another git pull if someone else has pushed more 

changes recently).

This is a fool-hardy alias:

 alias gcpp "git add \!*; git commit; git pull; git push"

!!! Committing and pushing are times of reflection. !!! This is your chance to catch problems before they escalate.

It is much better to group related changes together into a single commit with a well-written commit message. And test the changes before pushing them into shared history.

Review the history of a file: cvs log

git log git log somefile

It lists the changes from newest to oldest.

There are tons of options with this command. It pipes it into more by default. You can pipe it into tig, or you can use -N where N is the number of lines to output before stopping.

There are also ways to filter the output by user. And you can specify a directory and get it and all subdirectories. And you could specify the time-frame. So you could say, show me everything that Hiram changed under kent/src/hg/lib in the last 3 weeks.

cd ~/kent; git log --stat --author=hiram --since="3 weeks ago" src/hg/lib

You can also specify changes in terms of tags and branch-tags, using tree-ish. (See the genomewiki, search for git)


Show who changed what: cvs ann

git blame somefile

git blame is reasonably fast and very powerful.


Rename files (no cvs equivalent)

git mv path newpath git commit -m 'renamed path to newpath for some good reason'

You can add -C to git blame or git log to track renames.

There is a lot of new-found freedom to fix poorly chosen names or do other larger re-arrangements of the directories and files now, but before doing anything major you should check with others.


Local branches and tags and merging and change branches

more on this in future


Remotes and remote branches and tags and tracking branches

configuration and use with pushing and pulling. More on this in future.


Oops, I'm stuck with half-baked changes

I want to just utterly lose all the uncommitted changes in my sandbox and return to my former self.

git reset --hard

This uses the HEAD by default.

Be careful because it resets ALL modified tracked files at every subdirectory level, i.e. the entire directory tree. So if you do a trick like modifying ~/kent/src/inc/common.mk to turn on extra debugging, and you don't check it in because you don't want to make it the default for everybody, then you would lose that tweak after a reset --hard.


Oops, I just want to totally throw away all staged changes

but keep my working dir as it is.

git reset


How to unstage just one file

git rm --cached somefile

This removes it from your stage but not from your working dir.


How to reset just one file

git checkout HEAD somefile

This will nuke any changes regarding somefile from the stage and also reset it in the working directory so it matches your HEAD commit.

You can also recursively reset any edited files at your level and below with this:

git checkout HEAD . # dot refers to current directory


Diffing

git diff has many flexible options. You can use branch-tags, tags, tree-ish. You can specify entire commit states, you can limit it to a specific file or directory.

git diff # diffs working dir to stage git diff --cached # diffs stage to HEAD commit. git diff A B # A and B can be any branches, tags, commit-ids, tree-ish.