Click here to Skip to main content
15,886,830 members
Articles / All Topics

Notes about Git

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Jan 2011CPOL2 min read 5.9K   3  
My notes about git for use as quick reference

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 their 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 every revision in the svn repository and commits it locally.

git svn clone http://llvm.org/svn/llvm-project/libcxx/trunk libcxx

Updating Your Sources

git svn rebase

Updating Your Sources After Editing

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.

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.

sashan@cyclops clang master $ git svn rebase
        M       docs/UsersManual.html
r121768 = 024394010048e704192f4f34f21836a550f37a9f (refs/remotes/git-svn)

And then apply the stashed changes:

sashan@cyclops clang master 1 $ git stash pop

Committing Changes in the Working Tree Directly to the Repository

This will add all edited 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.

sashan@cyclops clang master $ git commit -a

Selecting the Files to Commit

This stages files for committing adding them to the index.

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.

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.

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.
This article was originally posted at http://www.zenskg.net/wordpress?p=120

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --