Difference between revisions of "Git"

From YobiWiki
Jump to navigation Jump to search
m
Line 17: Line 17:
 
Creating a .git in the current (project) directory:
 
Creating a .git in the current (project) directory:
 
git init
 
git init
 
==Working on a project==
 
===Initial project===
 
Add manually files/directories e.g. with
 
git init
 
git add .
 
git commit
  +
  +
===Copying existing project===
 
Clone an existing Git repository into a to-be-created target directory:
 
git clone /path/to/other/repository target
 
Remote repositories can also be accessed with paths like
 
ssh://login@host/path/to/repository
 
git://git.software.org/trunk
 
http://git.software.org/trunk
 
Later to update the local repository according to the remote repository:
 
git pull
 
Symmetrically the remote repository owner could also get the changes we've done locally if she does:
 
git pull /path/to/our/target
 
Or we could send them ourselves if we've write access on the remote:
 
git push
 
BTW the remote can create a shortcut to us to not have to provide our full path everytime
 
git remote add ourshortcut /path/to/our/target
 
And now use directly
 
git remote show ourshortcut
 
git pull ourshortcut
 
Note that git pull ourshortcut ==
 
git fetch ourshortcut
 
git merge ourshortcut/master
 
===Using a Subversion server===
 
Getting the full project:
 
# git clone =>
 
git-svn clone http://subversion.server.com/project -T trunk -b branches -t tags
 
(git-gc to compress if it took a big room)
 
Updating the local repository according to the subversion server:
  +
# git pull =>
 
git-svn rebase
 
Sending the local changes to the subversion server:
  +
# git push =>
 
git-svn dcommit
 
==Basic usage==
 
==Basic usage==
  +
===Edition===
 
Schedule a file for committing
 
Schedule a file for committing
 
git add <file>
 
git add <file>
Line 27: Line 67:
 
git mv file
 
git mv file
 
git rm file
 
git rm file
  +
===Diff/patch===
 
Diff between working files & repository
 
Diff between working files & repository
 
git diff
 
git diff
Line 35: Line 76:
 
This provides usage patches, including metadata, can be applied with
 
This provides usage patches, including metadata, can be applied with
 
git apply
 
git apply
  +
===Status & revert===
 
Status of local working files
 
Status of local working files
 
git status
 
git status
Line 44: Line 86:
 
git reset HEAD^
 
git reset HEAD^
 
This will not change the working tree.
 
This will not change the working tree.
 
===History===
 
History
 
 
git log
 
git log
 
See from which revision came the lines of a file
 
See from which revision came the lines of a file
Line 57: Line 98:
 
Or a commit with:
 
Or a commit with:
 
git show rev
 
git show rev
  +
===Tags & branches===
 
  +
Create a tag:
==Initial project==
 
  +
git tag -a name
Add manually files/directories e.g. with
 
  +
List tags and show the tag message:
git init
 
git add .
+
git tag -l
git commit
+
git show tag
  +
Create a branch:
 
  +
git branch branch [<rev>]
==Copying existing project==
 
  +
Switch to the branch
Clone an existing Git repository into a to-be-created target directory:
 
  +
git checkout branch
git clone /path/to/other/repository target
 
  +
List branches (current is flagged by a *)
Remote repositories can also be accessed with paths like
 
  +
git branch
ssh://login@host/path/to/repository
 
  +
To move your tree to some older revision, use:
git://git.software.org/trunk
 
  +
git checkout rev
http://git.software.org/trunk
 
  +
git checkout prevbranch
Later to update the local repository according to the remote repository:
 
 
===Ignoring some files===
git pull
 
Symmetrically the remote repository owner could also get the changes we've done locally if she does:
 
git pull /path/to/our/target
 
Or we could send them ourselves if we've write access on the remote:
 
git push
 
BTW the remote can create a shortcut to us to not have to provide our full path everytime
 
git remote add ourshortcut /path/to/our/target
 
And now use directly
 
git remote show ourshortcut
 
git pull ourshortcut
 
Note that git pull ourshortcut ==
 
git fetch ourshortcut
 
git merge ourshortcut/master
 
==Using a Subversion server==
 
Getting the full project:
 
# git clone =>
 
git-svn clone http://subversion.server.com/project -T trunk -b branches -t tags
 
(git-gc to compress if it took a big room)
 
Updating the local repository according to the subversion server:
 
# git pull =>
 
git-svn rebase
 
Sending the local changes to the subversion server:
 
# git push =>
 
git-svn dcommit
 
 
==Ignoring some files==
 
 
cat > .gitignore <<EOF
 
cat > .gitignore <<EOF
 
*.pyc
 
*.pyc

Revision as of 00:04, 17 September 2008

Links

Install

sudo apt-get install git-svn git-doc git-gui tig

Writing to global ~/.gitconfig file:

git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

Creating a .git in the current (project) directory:

git init

Working on a project

Initial project

Add manually files/directories e.g. with

git init
git add .
git commit

Copying existing project

Clone an existing Git repository into a to-be-created target directory:

git clone /path/to/other/repository target

Remote repositories can also be accessed with paths like

ssh://login@host/path/to/repository
git://git.software.org/trunk
http://git.software.org/trunk

Later to update the local repository according to the remote repository:

git pull

Symmetrically the remote repository owner could also get the changes we've done locally if she does:

git pull /path/to/our/target

Or we could send them ourselves if we've write access on the remote:

git push

BTW the remote can create a shortcut to us to not have to provide our full path everytime

git remote add ourshortcut /path/to/our/target

And now use directly

git remote show ourshortcut
git pull ourshortcut

Note that git pull ourshortcut ==

git fetch ourshortcut
git merge ourshortcut/master

Using a Subversion server

Getting the full project:

# git clone =>
git-svn clone http://subversion.server.com/project -T trunk -b branches -t tags
(git-gc to compress if it took a big room)

Updating the local repository according to the subversion server:

# git pull =>
git-svn rebase

Sending the local changes to the subversion server:

# git push =>
git-svn dcommit

Basic usage

Edition

Schedule a file for committing

git add <file>

Committing

git commit

Note that a modified file must be explicitly added every time, unless you use

git commit -a

File renaming is implicit, so you don't have to take care, just rename your files if you want (really??), but there is also the explicit commands

git mv file
git rm file

Diff/patch

Diff between working files & repository

git diff

With specific revision or path

git diff <rev> <path>

E.g with one but last commit

git diff HEAD~1

This provides usage patches, including metadata, can be applied with

git apply

Status & revert

Status of local working files

git status

To restore (revert) a file from the last revision

git checkout <path>

You can amend your latest commit (re-edit the metadata as well as update the tree) using

git commit --amend

Or toss your latest commit away completely using

git reset HEAD^

This will not change the working tree.

History

git log

See from which revision came the lines of a file

git blame <file>

Or search for commits affecting a specific line

git log -S"string"

You can see the contents of a file

git show rev:path/to/file

The listing of a directory

git show rev:path/to/directory

Or a commit with:

git show rev

Tags & branches

Create a tag:

git tag -a name

List tags and show the tag message:

git tag -l
git show tag

Create a branch:

git branch branch [<rev>]

Switch to the branch

git checkout branch

List branches (current is flagged by a *)

git branch

To move your tree to some older revision, use:

git checkout rev
git checkout prevbranch

Ignoring some files

cat > .gitignore <<EOF
*.pyc
*~
EOF
git add .gitignore

Now you can also delete all files neither tracked nor ignored with:

git clean

Misc

  • Revisions are SHA-1 hashes, not incremental numbers.
    You can refer to the latest revision by HEAD, its parent as HEAD^ and its parent as HEAD^^ = HEAD~2
    You can also just type the first digits of the hash (if it's enough to get a unique ID)
    man git-rev-parse for more details
  • The Git commands are in the form git command. You can interchangeably use the git-command form as well.

etckeeper

TODO