Difference between revisions of "Git Commands"

From Exterior Memory
Jump to: navigation, search
(tags)
Line 1: Line 1:
==Git commands==
+
The Git commands are in particular useful with GitHub
  
In particular useful with GitHub
+
==Download Code==
 
+
===Download and Upload Code===
+
  
 
====Download (fork) a remote repository====
 
====Download (fork) a remote repository====
Line 22: Line 20:
 
  git fetch origin
 
  git fetch origin
 
  git fetch --all
 
  git fetch --all
 +
 
====Fetch and merge updates from a remote repository====
 
====Fetch and merge updates from a remote repository====
 
  git pull master
 
  git pull master
Line 29: Line 28:
 
====Fetch updates from a remote subversion repository====
 
====Fetch updates from a remote subversion repository====
 
  git svn fetch
 
  git svn fetch
 +
 +
==Upload Code==
  
 
====Upload committed changes to a remote repository====
 
====Upload committed changes to a remote repository====
 
  git push
 
  git push
 
  git push origin
 
  git push origin
 +
 +
====Upload to a different remote repository====
 +
Push the local "upstream" branch to the "master" branch at repository "remote":
 +
git push remote upstream:master
 +
 +
====Upload committed changes to a remote subversion repository====
 +
git svn dcommit
  
 
====Upload tags to a remote repository====
 
====Upload tags to a remote repository====
 
  git push --tags origin
 
  git push --tags origin
  
====Delete tags from a remote repository====
+
====Upload to multiple repositories at the same time====
git push origin :refs/tags/12345
+
  
====Upload committed changes to a remote subversion repository====
+
See: http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations
git svn dcommit
+
  
===Create a Remote Repository===
+
==Create a Remote Repository==
  
 
====Set up an empty (bare) remote repository====
 
====Set up an empty (bare) remote repository====
Line 63: Line 69:
 
  git push -u origin master
 
  git push -u origin master
  
===Branching===
+
==Delete Remote Code==
 +
 
 +
====Delete a remote branch====
 +
By default branches are not deleted from a remote site, even not with a <code>git push --all</code>.
 +
 
 +
git push remotename :branchname
 +
git push origin :branchname
 +
 
 +
====Delete tags from a remote repository====
 +
git push origin :refs/tags/12345
 +
 
 +
==Branching==
  
 
====Create new branch====
 
====Create new branch====
Line 73: Line 90:
 
====Delete a branch====
 
====Delete a branch====
 
  git branch -D branchname
 
  git branch -D branchname
 
====Delete a remote branch====
 
By default branches are not deleted from a remote site, even not with a <code>git push --all</code>.
 
 
git push remotename :branchname
 
git push origin :branchname
 
  
 
====Rename a branch====
 
====Rename a branch====
 
  git branch -m oldname newname
 
  git branch -m oldname newname
  
===Branch Manipulation===
+
==Branch Manipulation==
  
 
====Create multiple pull requests (in GitHub)====
 
====Create multiple pull requests (in GitHub)====
Line 105: Line 116:
 
However, if you want to move the master branch pointer, it may be useful to do the following:
 
However, if you want to move the master branch pointer, it may be useful to do the following:
 
  git branch -f branch-name commit-sha1
 
  git branch -f branch-name commit-sha1
 +
 +
==Undo==
 +
 +
 +
==Cherry-Picking==
  
 
[[Category:Version_Control_Software]]
 
[[Category:Version_Control_Software]]

Revision as of 02:39, 17 April 2012

The Git commands are in particular useful with GitHub

Download Code

Download (fork) a remote repository

git clone remote-url
git clone git@github.com:macfreek/some-project.git

Add a second remote site as a branch

git remote add branchname remote-url
git remote add camblor https://github.com/macfreek/some-project.git

This will create branches under the macfreek/ prefix, e.g. macfreek/origin, macfreek/v1.4 etc. You need to git pull --all to download the new branch.

Download (fork) a remote subversion repository

git svn clone --stdlayout url
git svn clone http://tvgrabnlpy.googlecode.com/svn

Fetch updates from a remote repository

git fetch
git fetch origin
git fetch --all

Fetch and merge updates from a remote repository

git pull master
git pull origin master
git pull --all master

Fetch updates from a remote subversion repository

git svn fetch

Upload Code

Upload committed changes to a remote repository

git push
git push origin

Upload to a different remote repository

Push the local "upstream" branch to the "master" branch at repository "remote":

git push remote upstream:master

Upload committed changes to a remote subversion repository

git svn dcommit

Upload tags to a remote repository

git push --tags origin

Upload to multiple repositories at the same time

See: http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations

Create a Remote Repository

Set up an empty (bare) remote repository

mkdir new-project.git
cd new-project.git
git init --bare
git-update-server-info

Start a local repository

mkdir new-project
cd new-project
git init
[create some files or folders]
git add *
git commit -m "My initial commit message"

Initial push to remote repository

git remote add origin git@example.net:new-project.git
git push -u origin master

Delete Remote Code

Delete a remote branch

By default branches are not deleted from a remote site, even not with a git push --all.

git push remotename :branchname
git push origin :branchname

Delete tags from a remote repository

git push origin :refs/tags/12345

Branching

Create new branch

git branch branchname

Select a branch to work on it

git checkout branchname

Delete a branch

git branch -D branchname

Rename a branch

git branch -m oldname newname

Branch Manipulation

Create multiple pull requests (in GitHub)

Imagine you have made four commits, and want to create two separate pull requests. In this example branch topic1 contains commits B and C, while branch topic2 should only contain commits D and E.

A upstream/master -- B -- C topic1 -- D -- E topic2

Making a pull request for topic1 works as expected, but a pull request for topic2 will contain commits B, C, D and E. Not just D and E.

The solution is to create a new branch and to cherry-pick commits D and E:

git branch -b topic2_req upstream/master
git cherry-pick sha1_D sha1_E
git push origin topic2_req

Move branch pointer to a different commit

The normal action is to simply delete the old branch, and create a new branch at the desired commit:

git branch -D branch-name
git checkout commit-sha1
git branch branch-name

However, if you want to move the master branch pointer, it may be useful to do the following:

git branch -f branch-name commit-sha1

Undo

Cherry-Picking