DotNetSlackers: ASP.NET News for lazy Developers

Friday, January 7, 2011

Notes about Git


These are my notes about git for use as quick reference.

Fundamentals

Typically a version control system has a repository where it stores the history of all changes. Then there’s your local development directory where you checkout a copy of the repositories contents, do some edits and then commit them back to the repository. Git breaks this model and adds an intermediate stage called the index that sits between your working directory and the git repository. Changes make there way from the working tree to the index then to the repository.

Checking out a svn hosted repository

You can use git with a svn repository. Note that this step can take a few hours because git checkouts out every revision in the svn repository and commits it locally.
 Collapse
git svn clone http://llvm.org/svn/llvm-project/libcxx/trunk libcxx

Updating your sources

 Collapse
git svn rebase

Updating your sources after editing

 Collapse
sashan@cyclops clang master $ git svn rebase
include/clang/AST/Stmt.h: needs update
lib/AST/Stmt.cpp: needs update
update-index --refresh: command returned error: 1
Oops … git’s complaining that you have edited files in the working tree. Use git stash to save the changes locally.
 Collapse
sashan@cyclops clang master 1 $ git stash
Saved working directory and index state WIP on master: 3923c27 Fix diagnostic reporting of previous default statements.
HEAD is now at 3923c27 Fix diagnostic reporting of previous default statements.
Now you can rebase.
 Collapse
sashan@cyclops clang master $ git svn rebase
        M       docs/UsersManual.html
r121768 = 024394010048e704192f4f34f21836a550f37a9f (refs/remotes/git-svn)
And then apply the stashed changes
 Collapse
sashan@cyclops clang master 1 $ git stash pop

Committing changes in the working tree directly to the repository

This will add all editted files to index and then to repository. It will also prompt you to enter a commit log message. The -a parameter tells git to add all edited files.
 Collapse
sashan@cyclops clang master $ git commit -a

Selecting the files to commit

This stages files for committing adding them to the index.
 Collapse
sashan@cyclops clang master $ git add <file name>
After this you should run git commit to commit the files.

Generating a patch for email submission

This will generate a patch file of the most recent commit that you can email as an attachment.
 Collapse
sashan@cyclops clang master $ git format-patch -1 -p
0001-Fix-diagnostic-reporting-of-previous-default-stateme.patch

Adding edited files to the index

Sometimes you want to add changes in the working tree to the index without committing to the repository at the same time. In this case doing a git commit -a won’t help. You need to use git add. The example below shows a session using git add -ui. The -u is for update and means that only modified paths in the working tree will be added to the index. New paths won’t. The -i is for interactive and therefore git will prompt you for a response. In the session below I enter interactive mode, choose to update the index, and then select a range of paths, 1 to 7, to update the index with. Importantly, after finishing the update I hit enter to exit the interactive update mode.
 Collapse
sashan@cyclops clang use_std_set_for_switch_cases $ git add -ui
           staged     unstaged path
  1:    unchanged      +51/-31 include/clang/AST/Stmt.h
  2:    unchanged        +6/-1 lib/AST/Stmt.cpp
  3:    unchanged        +4/-4 lib/Sema/JumpDiagnostics.cpp
  4:    unchanged        +5/-3 lib/Sema/SemaCodeComplete.cpp
  5:    unchanged       +14/-6 lib/Sema/SemaStmt.cpp
  6:    unchanged        +8/-2 lib/Serialization/ASTReaderStmt.cpp
  7:    unchanged        +3/-3 lib/Serialization/ASTWriterStmt.cpp

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 2
           staged     unstaged path
  1:    unchanged      +51/-31 include/clang/AST/Stmt.h
  2:    unchanged        +6/-1 lib/AST/Stmt.cpp
  3:    unchanged        +4/-4 lib/Sema/JumpDiagnostics.cpp
  4:    unchanged        +5/-3 lib/Sema/SemaCodeComplete.cpp
  5:    unchanged       +14/-6 lib/Sema/SemaStmt.cpp
  6:    unchanged        +8/-2 lib/Serialization/ASTReaderStmt.cpp
  7:    unchanged        +3/-3 lib/Serialization/ASTWriterStmt.cpp
Update>> 1-7
           staged     unstaged path
* 1:    unchanged      +51/-31 include/clang/AST/Stmt.h
* 2:    unchanged        +6/-1 lib/AST/Stmt.cpp
* 3:    unchanged        +4/-4 lib/Sema/JumpDiagnostics.cpp
* 4:    unchanged        +5/-3 lib/Sema/SemaCodeComplete.cpp
* 5:    unchanged       +14/-6 lib/Sema/SemaStmt.cpp
* 6:    unchanged        +8/-2 lib/Serialization/ASTReaderStmt.cpp
* 7:    unchanged        +3/-3 lib/Serialization/ASTWriterStmt.cpp
Update>>
updated 7 paths

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 7
Bye.

No comments:

Post a Comment