Git: CVS equivalent operations

From genomewiki
Revision as of 17:45, 14 June 2010 by Ann (talk | contribs) (from Galt's handy email)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

1. 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


2. 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.


3. 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.

4. 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)


5. show who changed what: cvs ann

git blame somefile

git blame is reasonably fast and very powerful.


6. 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.


7. local branches and tags and merging and change branches

more on this in future


8. remotes and remote branches and tags and tracking branches: configuration and use with pushing and pulling.

more on this in future


9. 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.


10. Oops, I just want to totally throw away all staged changes, but keep my working dir as it is.

git reset


11. How to unstage just one file

git rm --cached somefile

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


12. How to reset just one file

git reset --hard 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.

13. 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.