Difference between revisions of "Git phrasebook"

From Creative Commons
Jump to: navigation, search
(Creating a new local branch that tracks a remote branch)
(cgit tips and tricks)
 
(16 intermediate revisions by 3 users not shown)
Line 8: Line 8:
 
== Creating a new local branch that tracks a remote branch ==
 
== Creating a new local branch that tracks a remote branch ==
  
  git branch --track LOCALFUN remote/REMOTE_BRANCH_NAME
+
Assuming you want to pull from the remote called "origin":
 +
 
 +
  git branch --track LOCALFUN origin/REMOTE_BRANCH_NAME
 
  git checkout LOCALFUN
 
  git checkout LOCALFUN
 
  git pull # You should see, "Already up-to-date."
 
  git pull # You should see, "Already up-to-date."
 
  git push # You should see, "Everything up-to-date"
 
  git push # You should see, "Everything up-to-date"
 +
 +
However, FIXME!
 +
 +
The push does nothing. :-(
  
 
== Switching which branch you are on ==
 
== Switching which branch you are on ==
Line 17: Line 23:
  
 
NOTE: Do not use this to '''create''' branches!
 
NOTE: Do not use this to '''create''' branches!
 +
 +
== Making a local branch available as a remote branch ==
 +
To make the branch BRANCH_NAME appear as a remote branch called BRANCH_NAME:
 +
 +
git push origin BRANCH_NAME
 +
 +
If you want to change the branch name:
 +
 +
git push origin LOCAL_BRANCH_NAME:REMOTE_BRANCH_NAME
 +
== Undo local changes to a file ==
 +
 +
Have you changed FILE in a way that you never want to commit?  Just check out the version in the current HEAD with:
 +
 +
git checkout FILE
 +
 +
== Configuring git to push a particular local branch to a particular remote branch every time you call "git push" ==
 +
Edit .git/config.  Look for [remote "origin"].
 +
 +
Inside there, add:
 +
push = refs/heads/LOCAL_BRANCH_NAME:refs/heads/REMOTE_BRANCH_NAME
 +
 +
Two important notes:
 +
# You may have as many push lines as you like.  Every "git push" with no arguments will push all of them.
 +
# LOCAL_BRANCH_NAME and REMOTE_BRANCH_NAME are often the same, but naturally do not have to be.
 +
 +
== Deleting a remote branch ==
 +
git push origin :BRANCH_NAME_TO_DELETE
 +
== Adding a new version of a submodule to the git index ==
 +
cd path/to/submodule
 +
git checkout origin/WHICHEVER_BRANCH_YOU_WANT
 +
cd .. # top level of project
 +
git add path/to/submodule # NOTE: No trailing slash!
 +
 +
== Undoing a git-add ==
 +
git reset
 +
will clear out your index, in case you git-add'd something that you didn't want to.
 +
 +
== Pushing tags ==
 +
git-push does not automatically push any tags you may have created. For this, use
 +
git push --tags
 +
 +
== [http://hjemli.net/git/cgit/about/ cgit] tips and tricks ==
 +
If you want to view a diff of changes to a file between arbitrary revisions you can formulate a URL like:
 +
 +
<nowiki>http://$domain/$cgit/$repository/diff/$pathtofile?id=$newer-sha1&id2=$older-sha1</nowiki>
 +
 +
Example: http://code.creativecommons.org/viewgit/civicrm.git/diff/bin/ContributionProcessor.php?id=ce519f4c83f6b1208394c14e88309a9d8493c1b4&id2=87fdecb84f70e4e46af741de3430844ee2d9333c
 +
 +
If you want to view a file in the tree as it appeared at a given commit you can formulate a URL like:
 +
 +
<nowiki>http://$domain/$cgit/$repository/plain/$pathtofile?id=$sha1</nowiki>
 +
 +
Example: http://code.creativecommons.org/viewgit/civicrm.git/plain/bin/OneClickDonate.php?id=1c7708665008b3f5775b6535df3094e0633fd227

Latest revision as of 22:46, 13 October 2009

Everything in ALLCAPS should be considered something you think about before typing.

Checking out a project

git clone PROJECT_URL
git branch -a # Take a look at all the remote branches you can pick from.

Creating a new local branch that tracks a remote branch

Assuming you want to pull from the remote called "origin":

git branch --track LOCALFUN origin/REMOTE_BRANCH_NAME
git checkout LOCALFUN
git pull # You should see, "Already up-to-date."
git push # You should see, "Everything up-to-date"

However, FIXME!

The push does nothing. :-(

Switching which branch you are on

git checkout <BRANCH_NAME>

NOTE: Do not use this to create branches!

Making a local branch available as a remote branch

To make the branch BRANCH_NAME appear as a remote branch called BRANCH_NAME:

git push origin BRANCH_NAME

If you want to change the branch name:

git push origin LOCAL_BRANCH_NAME:REMOTE_BRANCH_NAME

Undo local changes to a file

Have you changed FILE in a way that you never want to commit? Just check out the version in the current HEAD with:

git checkout FILE

Configuring git to push a particular local branch to a particular remote branch every time you call "git push"

Edit .git/config. Look for [remote "origin"].

Inside there, add:

push = refs/heads/LOCAL_BRANCH_NAME:refs/heads/REMOTE_BRANCH_NAME

Two important notes:

  1. You may have as many push lines as you like. Every "git push" with no arguments will push all of them.
  2. LOCAL_BRANCH_NAME and REMOTE_BRANCH_NAME are often the same, but naturally do not have to be.

Deleting a remote branch

git push origin :BRANCH_NAME_TO_DELETE

Adding a new version of a submodule to the git index

cd path/to/submodule
git checkout origin/WHICHEVER_BRANCH_YOU_WANT
cd .. # top level of project
git add path/to/submodule # NOTE: No trailing slash!

Undoing a git-add

git reset

will clear out your index, in case you git-add'd something that you didn't want to.

Pushing tags

git-push does not automatically push any tags you may have created. For this, use

git push --tags

cgit tips and tricks

If you want to view a diff of changes to a file between arbitrary revisions you can formulate a URL like:

http://$domain/$cgit/$repository/diff/$pathtofile?id=$newer-sha1&id2=$older-sha1

Example: http://code.creativecommons.org/viewgit/civicrm.git/diff/bin/ContributionProcessor.php?id=ce519f4c83f6b1208394c14e88309a9d8493c1b4&id2=87fdecb84f70e4e46af741de3430844ee2d9333c

If you want to view a file in the tree as it appeared at a given commit you can formulate a URL like:

http://$domain/$cgit/$repository/plain/$pathtofile?id=$sha1

Example: http://code.creativecommons.org/viewgit/civicrm.git/plain/bin/OneClickDonate.php?id=1c7708665008b3f5775b6535df3094e0633fd227