Git: CVS equivalent operations: Difference between revisions

From genomewiki
Jump to navigationJump to search
(added a section on splitting and joining files)
Line 103: Line 103:
others.
others.


==Split or Join files (no cvs equivalent)==
Split example:
  cp file1.c file2.c
  vi file1.c  # remove one half
  vi file2.c  # remove the other half
  git add file1.c
  git add file2.c
  git commit -m 'split file1 into file1 and file2 for some good reason'
Join example:
  cat file2.c >> file1.c  # file1 will have the contents of old file1 and file2
  git rm file2.c
  git add file1.c
  git commit -m 'combined file2 and file1 into just file1 for some good reason'
IT IS VITAL TO HAVE ALL THE PARTS CHANGING
BE ADDED AND COMMITTED TOGETHER IN A SINGLE COMMIT.
If you fail to do this the file's history (git blame) will not be maintained.
You can add -C to git blame or git log to track renames and content movement.
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.
If there's any chance you didn't get it right,
then don't push it.  If you need some time
to work out a complex rearrangment, or to test it,
you may wish to use a branch to develop in
a more controlled fashion.  Only merge and push
when it's right. Use git blame to check your
results are as expected.


==Local branches and tags and merging and change branches==
==Local branches and tags and merging and change branches==

Revision as of 10:20, 18 June 2010

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.

Split or Join files (no cvs equivalent)

Split example:

 cp file1.c file2.c
 vi file1.c  # remove one half
 vi file2.c  # remove the other half
 git add file1.c
 git add file2.c
 git commit -m 'split file1 into file1 and file2 for some good reason'

Join example:

 cat file2.c >> file1.c   # file1 will have the contents of old file1 and file2
 git rm file2.c
 git add file1.c
 git commit -m 'combined file2 and file1 into just file1 for some good reason'

IT IS VITAL TO HAVE ALL THE PARTS CHANGING BE ADDED AND COMMITTED TOGETHER IN A SINGLE COMMIT. If you fail to do this the file's history (git blame) will not be maintained.

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

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.

If there's any chance you didn't get it right, then don't push it. If you need some time to work out a complex rearrangment, or to test it, you may wish to use a branch to develop in a more controlled fashion. Only merge and push when it's right. Use git blame to check your results are as expected.

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 HEAD   #diffs working dir to HEAD
 git diff A B   # A and B can be any branches, tags, commit-ids, tree-ish.