Skip to content

Instantly share code, notes, and snippets.

@phaustin
Last active April 18, 2016 16:02
Show Gist options
  • Select an option

  • Save phaustin/9a4a225c7c202882545e to your computer and use it in GitHub Desktop.

Select an option

Save phaustin/9a4a225c7c202882545e to your computer and use it in GitHub Desktop.
Readme_git.org

delete a remote branch


git push origin :pha

To git@github.com:phoustona/A405.git

  • [deleted] pha

working with remote repository


git remote show origin

git remote update

git workflow


don’t commit to master, work on a feature branch if your commit history is messy, before you merge with master, use rebase

for example to squash commits into a single commit

git co working-branch git rebase -i master #shows commits not on master edit file to pick the first commit and squash the rest if this doesn’t work, edit the commit file and do git rebase –continue

then

git co master git merge working-branch

to move one set of changes from one branch to another:

git co working-branch #Readme_git.nm working branch is different from other_branch, #make a patch to move the changes over to other_branch # git diff -p other_branch Readme_git.nm > patch git co other-branch #apply the patch git apply patch http://www.gitready.com/intermediate/2009/03/04/pick-out-individual-commits.html

useful commands


git log HEAD..origin/master

git clone ssh://roc/space2/phil/repos/group_bare group git clone /space2/phil/repos/group_bare group

git br ccorbel git co ccorbel – puts you in new branch

git remote show origin –

#set up a tracking branch

git checkout –track -b phil origin/phil

git co –track -b missing origin/missingdata

#to show difference between origin and local git log origin/master..master

up vote 13 down vote

1.

Use “git log origin..HEAD” 2.

Use “git fetch” followed by “git log HEAD..origin”. You can cherry-pick individual commits using the listed commit ids.

The above assumes, of course, that “origin” is the name of your remote tracking branch (which it is if you’ve used clone with default options). link|edit|flag

answered Oct 23 ‘08 at 19:55 Greg Hewgill 128k20252407

(And if you’re not tracking the remote branch, it’s “git log origin/master..HEAD”.) – plindberg Aug 17 ‘10 at 13:16 up vote 5 down vote

Not a full answer but git fetch will pull the remote repo and not do a merge. You can then do a

git diff master origin/master

shared repository


git clone ssh://yourhost/~you/repository

mkdir readmes_bare cd readmes_bare git init –shared=group –bare

#then to the repository you want

git remote add origin /home/phil/public_html/readmes_bare

#push master git push origin master

#or add another branch

git branch –track phil origin/phil

or

git checkout –track -b phil origin/phil

git fetch origin phil

git merge origin/phil

~/repos/group phil@tern% git fetch origin phil phil@roc’s password: From ssh://roc/space2/phil/repos/group_bare

  • branch phil -> FETCH_HEAD

~/repos/group phil@tern% git merge FETCH_HEAD Updating e8a0251..44db371 Fast-forward christophe_code/parameters.py | 65 ++++++++++++++++++++++------------------- 1 files changed, 35 insertions(+), 30 deletions(-)

remote origin


~/repos/group phil@tern% git remote show origin phil@roc’s password:

Fetch URL: ssh://phil@roc/space2/phil/repos/group_bare Push URL: ssh://phil@roc/space2/phil/repos/group_bare HEAD branch: master Remote branches: master tracked phil tracked test new (next fetch will store in remotes/origin) Local branches configured for ‘git pull’: master merges with remote master phil merges with remote phil Local refs configured for ‘git push’: master pushes to master (up to date) phil pushes to phil (up to date)

~/repos/group phil@tern% git ls-remote phil@roc’s password: From ssh://phil@roc/space2/phil/repos/group_bare e8a025118bfe7b23d0fb7b19597f6c4e900eedd3 HEAD e8a025118bfe7b23d0fb7b19597f6c4e900eedd3 refs/heads/master 44db3716b9f1ce7e89c953cee8170f81cee527f7 refs/heads/phil 44db3716b9f1ce7e89c953cee8170f81cee527f7 refs/heads/test

hg clone git+ssh://git@github.com/phaustin/e582.git

In .ssh/config

Host github.com User git Port 22 Hostname github.com IdentityFile ~/.ssh/gitkey_rsa TCPKeepAlive yes IdentitiesOnly yes

[extensions] hgext.mq = hgext.extdiff = hgext.graphlog = #rdiff=/home/phil/lib/python/rdiff.py hgext.bookmarks = hggit =

#or, if keyword.py is not in the hgext folder: #keyword=/path/to/keyword.py

[bookmarks] track.current = True

~ phil@roc% ssh-add ~/.ssh/gitkey_rsa Enter passphrase for home/phil.ssh/gitkey_rsa: Identity added: home/phil.ssh/gitkey_rsa (home/phil.ssh/gitkey_rsa)

git remote add origin git@github.com:phaustin/mpi.git git push origin master hg clone git+ssh://git@github.com/phaustin/mpi.git

github


http://www.newartisans.com/2008/04/git-from-the-bottom-up.html https://github.com/sympy/sympy/wiki/Git-hg-rosetta-stone

https://github.com/fperez/datarray-doc http://fperez.github.com/datarray-doc

https://github.com/phaustin/pystar https://github.com/phaustin/pystar-doc http://phaustin.github.com/pystar-doc

to delete master

git checkout gh-pages git delete -D master

change default branch in github admin to gh-pages

~/repos/git/pystar-doc phil@tern% git push origin :master To git+ssh://git@github.com/phaustin/pystar-doc.git

  • [deleted] master

git remote add upstream /home/datatmp/xue/repos/git/untangle git pull upstream xue or get fetch upstream git branch –set-upstream xue upstream/xue

git fetch upstream git rebase upstream/master

git fetch upstream git merge –no-ff upstream/master

git log –oneline –graph git log -p upstream/master..

git push upstream my-new-feature:master

git branch my-new-feature git checkout my-new-feature

git show all files for a commit

git show –pretty=”format:” –name-only s11

index, trunk (HEAD), working tree


  • git diff shows you the differences from index to working tree
  • git diff HEAD shows you the differences from trunk to working tree
  • git diff –cached shows you the differences from trunk to index

http://smalltalk.gnu.org/blog/bonzinip/using-git-without-feeling-stupid-part-2

converting a repostitory


mkdir the_repo cd the_repo git init then home/phil/bin/hg-fast-export.sh -r /home/datatmp/phil/repos/scmtools

links


http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/ http://djwonk.com/blog/2009/04/18/tracking-remote-git-branches/

numpy workflow

https://mail.google.com/mail/u/0/?shva=1#search/label%3Al-python+and+git/12b9db145db13143

reflog

http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html

https://mail.google.com/mail/u/0/?shva=1#search/label%3Al-python+and+git+and+reflog/12b0ccd723197859

http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide

http://stackoverflow.com/questions/4337364/how-do-i-manage-merging-and-rebasing-in-git

http://docs.scipy.org/doc/numpy/dev/gitwash/development_workflow.html

http://stackoverflow.com/questions/137102/whats-the-best-visual-merge-tool-fo

http://stackoverflow.com/questions/2459084/git-difftool-full-file-tree-compare-using-meld

http://nathanhoad.net/how-to-meld-for-git-diffs-in-ubuntu-hardy

http://blog.sarathonline.com/2009/02/use-meld-as-git-diff-gui-viewer.html

https://mail.google.com/mail/u/0/?shva=1#search/label%3Al-python+and+reflog/12b0ccd723197859

http://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-visual-diff-program

http://denis.tumblr.com/post/50507027/using-kompare-for-git-diffs

kernal hackers guide to git http://linux.yyz.us/git-howto.html#list_branches

git config –global diff.external /home/phil/usr64/bin//gitdiff.py git diff filename

I’ve tested this on a recent version of meld and it is now built in. You can use it with meld <directory path> so “meld .” works for the current directory. You can checkout a version git co [version] then git reset [version]^ and then run “meld .” to compare any version with it’s previous version.

git init git remote add upstream /home/datatmp/xue/repos/git/fresh git fetch upstream

remote: Counting objects: 2088, done. remote: Compressing objects: 100% (851/851), done. remote: Total 2088 (delta 1173), reused 1924 (delta 1080) Receiving objects: 100% (2088/2088), 8.28 MiB | 14.13 MiB/s, done. Resolving deltas: 100% (1173/1173), done. From /home/datatmp/xue/repos/git/fresh

  • [new branch] astex_comp_cmake -> upstream/astex_comp_cmake
  • [new branch] master -> upstream/master
  • [new branch] mcgill_cmake -> upstream/mcgill_cmake
  • [new branch] s11cmake -> upstream/s11cmake
  • [new branch] s12cmake -> upstream/s12cmake
  • [new branch] s12cmake2 -> upstream/s12cmake2
  • [new branch] s6cmake -> upstream/s6cmake

indefero debug


https://www.indefero.net/media/idfa/css/style.css?6bb11 http://phaustin.indefero.net/media/idf/css/yui.css?6bb11

~/repos/temp phil@tern% git init –bare Initialized empty Git repository in nfs/kite/home/phil/repos/temp ~/repos/temp phil@tern% s 1 bash: pushd: /home/datatmp/phil/temp: No such file or directory ~/repos/temp phil@tern% d ~/repos/temp ~/repos/temp ~/repos/temp /home/datatmp/phil ~/repos/temp ~/repos/temp phil@tern% dt 1 ~/repos/temp ~/repos/temp /home/datatmp/phil ~/repos/temp ~/repos/temp phil@tern% dt bash: popd: +: invalid number popd: usage: popd [+N | -N] [-n] ~/repos/temp phil@tern% dt 1 ~/repos/temp /home/datatmp/phil ~/repos/temp ~/repos/temp phil@tern% dt 1 ~/repos/temp ~/repos/temp ~/repos/temp phil@tern% d ~/repos/temp ~/repos/temp ~/repos/temp phil@tern% dt 1 ~/repos/temp ~/repos/temp phil@tern% pd /home/datatmp/phil/temp bash: pushd: /home/datatmp/phil/temp: No such file or directory ~/repos/temp phil@tern% pd /home/datatmp/phil /home/datatmp/phil ~/repos/temp /home/datatmp/phil phil@tern% git clone /home/phil/repos/temp Cloning into temp… done. warning: You appear to have cloned an empty repository. /home/datatmp/phil phil@tern% cd temp /home/datatmp/phil/temp phil@tern% git remote add origin /home/phil/repos/tep fatal: remote origin already exists. /home/datatmp/phil/temp phil@tern% touch readme /home/datatmp/phil/temp phil@tern% git add readme /home/datatmp/phil/temp phil@tern% git commit -a -m ‘initial commit’ [master (root-commit) b3670d9] initial commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 readme /home/datatmp/phil/temp phil@tern% git push origin No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as ‘master’. fatal: The remote end hung up unexpectedly error: failed to push some refs to ‘/home/phil/repos/temp’ /home/datatmp/phil/temp phil@tern% git push master origin fatal: ‘master’ does not appear to be a git repository fatal: The remote end hung up unexpectedly /home/datatmp/phil/temp phil@tern% git push origin master Counting objects: 3, done. Writing objects: 100% (3/3), 210 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To /home/phil/repos/temp

  • [new branch] master -> master

/home/datatmp/phil/temp phil@tern% git co -b newbranch Switched to a new branch ‘newbranch’ /home/datatmp/phil/temp phil@tern% touch newreadme /home/datatmp/phil/temp phil@tern% git add newreadme /home/datatmp/phil/temp phil@tern% git commit -m ‘second commit’ [newbranch 53aaaac] second commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 newreadme /home/datatmp/phil/temp phil@tern% git push origin newbranch Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 238 bytes, done. Total 2 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (2/2), done. To /home/phil/repos/temp

  • [new branch] newbranch -> newbranch

/home/datatmp/phil/temp phil@tern% git co master Switched to branch ‘master’ /home/datatmp/phil/temp phil@tern% git br -f newbranch origin/newbranch Branch newbranch set up to track remote branch newbranch from origin. /home/datatmp/phil/temp phil@tern% git co newbranch Switched to branch ‘newbranch’ /home/datatmp/phil/temp phil@tern% touch readme3 /home/datatmp/phil/temp phil@tern% git add readme3 /home/datatmp/phil/temp phil@tern% git br master

newbranch

/home/datatmp/phil/temp phil@tern% git commit -m ‘another commit’ [newbranch 3dc64bb] another commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 readme3 /home/datatmp/phil/temp phil@tern% git push Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 242 bytes, done. Total 2 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (2/2), done. To /home/phil/repos/temp 53aaaac..3dc64bb newbranch -> newbranch /home/datatmp/phil/temp phil@tern%

https://clouds.eos.ubc.ca/indefero/index.php http://help.github.com/troubleshooting-ssh/ https://groups.google.com/group/indefero-users/browse_thread/thread/f763acf8cff08df3/72cfbcc5ca3bfaee?#72cfbcc5ca3bfaee

desired behavior is:

if we’ve got the public key added to indefero

  1. git clone git@clouds.eos.ubc.ca/indefero:scm.git myscm
  2. cd myscm
  3. touch README
  4. git add README
  5. git commit -a -m ‘initial add’
  6. .git/config – is indefero already listed under remotes? like this example from ~phil/repos/git/pystar:

[remote “origin”] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:phaustin/pystar.git

  1. git push origin master (phil will check the syntax) and the repository on kite should be updated with the new file
  2. outsiders with no public key should not be able to clone

sharing a repository


git clone ssh://yourhost/~you/repository $ cat >>.git/config <<EOF [remote “public-repo”] url = ssh://yourserver.com/~you/proj.git nEOF

git –bare init

add g+w on all directories

/home/datatmp/mvandenb/repos phil@swift% ls -ltd test/* drwxrwxr-x 7 mvandenb users 4096 Dec 7 13:34 test/objects drwxrwxr-x 2 mvandenb users 4096 Dec 7 13:23 test/branches -rw-rw-r– 1 mvandenb users 66 Dec 7 13:23 test/config -rw-rw-r– 1 mvandenb users 73 Dec 7 13:23 test/description -rw-rw-r– 1 mvandenb users 23 Dec 7 13:23 test/HEAD drwxrwxr-x 2 mvandenb users 4096 Dec 7 13:23 test/hooks drwxrwxr-x 2 mvandenb users 4096 Dec 7 13:23 test/info drwxrwxr-x 4 mvandenb users 4096 Dec 7 13:23 test/refs

gitignore


to get git to always ignore things like .pyc files (without having to put .gitignore files in every project):

create a ~/.gitignore in your user directory

.DS_Store *.pyc .svn

then run:

git config –global core.excludesfile ~/.gitignore

~/public_html/readmes phil@tern% git remote add readmes_tern /home/phil/public_html/readmes_bare

~/public_html/readmes phil@tern% git checkout –track -b tern readmes_tern/master fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout ‘readmes_tern/master’ which can not be resolved as commit?

~/public_html/readmes phil@tern% git remote show readmes_tern

remote readmes_tern

Fetch URL: /home/phil/public_html/readmes_bare Push URL: /home/phil/public_html/readmes_bare HEAD branch: master Remote branch: master new (next fetch will store in remotes/readmes_tern) Local ref configured for ‘git push’: master pushes to master (fast-forwardable)

~/public_html/readmes phil@tern% git ls-remote readmes_tern 23afebeba0e0c256ae7cc8f932c4175bd8092faf HEAD 23afebeba0e0c256ae7cc8f932c4175bd8092faf refs/heads/master

http://stackoverflow.com/questions/3036817/in-git-how-do-i-check-out-a-remote-repositorys-remote-branches http://gitready.com/beginner/2009/03/09/remote-tracking-branches.html http://stackoverflow.com/questions/5149872/how-to-git-remote-add-and-track-a-branch-in-the-same-filesystem

git branch –track tern1 refs/remotes/readmes_tern/master http://stackoverflow.com/questions/5149872/how-to-git-remote-add-and-track-a-branch-in-the-same-filesystem

~/public_html/readmes phil@tern% git show-ref 38fd0d835cc973c8054696cba1a2bbb7c87e85ec refs/heads/master 22af8879d66add83559d8c20901828b39b84494f refs/heads/phil 38fd0d835cc973c8054696cba1a2bbb7c87e85ec refs/heads/tern 23afebeba0e0c256ae7cc8f932c4175bd8092faf refs/heads/tern1 38fd0d835cc973c8054696cba1a2bbb7c87e85ec refs/remotes/origin/HEAD 38fd0d835cc973c8054696cba1a2bbb7c87e85ec refs/remotes/origin/master 22af8879d66add83559d8c20901828b39b84494f refs/remotes/origin/phil 23afebeba0e0c256ae7cc8f932c4175bd8092faf refs/remotes/readmes_tern/master 5e6ecff95d354d0d36950fbc84ff56caf5bf31af refs/stash

~/public_html/readmes phil@tern% git remote show readmes_tern

remote readmes_tern

Fetch URL: /home/phil/public_html/readmes_bare Push URL: /home/phil/public_html/readmes_bare HEAD branch: master Remote branch: master tracked Local branch configured for ‘git pull’: tern1 merges with remote master f Local ref configured for ‘git push’: master pushes to master (fast-forwardable)

~/public_html/readmes phil@tern% git branch -vv master 38fd0d8 [origin/master] checkpoint phil 22af887 Merge branch ‘cleanup’ into phil tern 38fd0d8 checkpoint

tern1 23afebe [readmes_tern/master] advanced git

not on any branch


Fix git “Not currently on any branch” Problem

This is quite geeky (goodies linked later :), but I want to post so I can find this fix later and because I didn’t see a whole lot of others posting about this problem, so others may find it useful as well.

I did a git svn rebase To update a local repo with changes from the master svn repository.

There was a conflict, and after resolving I ended up in the “Not currently on any branch” state.

To resolve, git commit -a -m “commit merge with svn to head” git checkout -b merged_with_svn git checkout original_branch git merge merged_with_svn

Hopefully someone else will find this useful. Otherwise, fear not—goodies!

cherry pick


git remote add ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation

setting up a remote repository


How to set up a shared group repository at cccma and push a branch that was developed at UBC (Jason’s offline radiation)

  1. at CCCma make the directory and initialize as a bare

repository (i.e. not a repository you will be developing in, but one you can clone and push to)

rpa:lxsrv3:Linux>cd .. cd .. /home/rpa/aux/repos rpa:lxsrv3:Linux>mkdir offline_radiation mkdir offline_radiation rpa:lxsrv3:Linux>cd offline_radiation cd offline_radiation /home/rpa/aux/repos/offline_radiation rpa:lxsrv3:Linux>git init –shared=group –bare git init –shared=group –bare Initialized empty shared Git repository in ra04/rpa/repos/offline_radiation

  1. at ubc cd to the existing repository (called jason) and add the new cccma repository as a remote called “origin”

~/repos/jason phil@tern% git remote add origin ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation

  1. push the master branch to cccma

~/repos/jason phil@tern% git push origin master Counting objects: 123, done. Delta compression using up to 4 threads. Compressing objects: 100% (122/122), done. Writing objects: 100% (123/123), 295.66 KiB, done. Total 123 (delta 15), reused 0 (delta 0) To ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation

  • [new branch] master -> master
  1. check the repository

~/repos/jason phil@tern% git remote show origin

remote origin

Fetch URL: ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation Push URL: ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation HEAD branch: master Remote branch: master tracked Local ref configured for ‘git push’: master pushes to master (up to date)

  1. now got to another computer and clone this repository

~/repos phil@roc% git clone ssh://acrnrpa@cccma/ra04/rpa/repos/offline_radiation Cloning into offline_radiation… remote: Counting objects: 123, done. remote: Compressing objects: 100% (107/107), done. remote: Total 123 (delta 15), reused 123 (delta 15) Receiving objects: 100% (123/123), 295.66 KiB, done. Resolving deltas: 100% (15/15), done.

~/repos phil@roc% cd offline_radiation ~/repos/offline_radiation phil@roc% ls SRC ~/repos/offline_radiation phil@roc% git branch

add a new branch to a repository


~/public_html/readmes phil@tern% git br -r

pulling an older file

git whatchanged stevens.py

origin/HEAD -> origin/master origin/master origin/phil origin/tern1 readmes_tern/master

~/public_html/readmes phil@tern% git push readmes_tern tern1 Counting objects: 11, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 2.29 KiB, done. Total 6 (delta 5), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. To /home/phil/public_html/readmes_bare

  • [new branch] tern1 -> tern1

~/public_html/readmes phil@tern% git br -r origin/HEAD -> origin/master origin/master origin/phil origin/tern1 readmes_tern/master readmes_tern/tern1

/space2/phil/repos/group/xue_code phil@roc% git whatchanged stevens.py commit cd74cd668d31ef8c078f11f8aa0a762e6706edc6 Author: Christophe CORBEL <ccorbel@eos.ubc.ca> Date: Thu Jan 6 11:51:19 2011 -0800

archi on roc updated

:000000 100755 0000000… 4645bf5… A xue_code/stevens.py git show 4645bf5 – xue_code/stevens.py > old_stevens.py

git show 9e1464b:CMakeLists.txt

By the way, you can also do

$ git clean -dfx

to make sure that EVERYTHING that doesn’t belong to the tree is plainly wiped out (don’t do that on your real checkouts unless you definitively have nothing to lose).

show all remote branches


git show-branch –all

show one file


differences: git show 4645bf5 – xue_code/stevens.py > old_stevens.py

actual content: git show cb0cb390c0fd9:SRC/hbuffer.f90

diff two files


git diff 6.9.6_dev nc_work – UTIL/SRC/stat2nc.f

ssl problem


env GIT_SSL_NO_VERIFY=true git clone https://github…

difference between git revert and reset


http://stackoverflow.com/questions/5128806/is-there-a-difference-between-git-reset-hard-head-and-git-checkout

up vote 8 down vote accepted

git checkout – . will obviously only work on the current directory (and subdirectories thereof), git reset –hard will operate on the complete working tree.

git checkout – . will only update the working tree and leave already staged files as is, whereas git reset –hard will match index and working tree with the HEAD commit.

when used with a refspec:

reset will set the current branch head to the given commit (and matches index and working tree) checkout will switch to that branch, leaving local changes intact, when they touch files which did not change between the current branch and the branch to be checked out

http://www.webnova.ca/Home/ViewPost/6669414313787834314

A quick google search will introduce you to the basics of git, however, what you will not find (rarely with git), is a clear explanation of the confusing usage of the reset and the revert commands. So, I will attempt to clarify for those of you who are still struggling with them, like I was, to know when to use each.

Think of git reset [commit], especially with the –hard argument as the command that moves the HEAD “pointer” and working set to any version or “commit/checkin”, disregarding all subsequent ones. It is like random access to any commit with the addition that it “resets” the HEAD pointer at that location. It is simply your HEAD mover.

On the other hand, git revert [commit] only reverts the [commit] and your current working set to the previous commit. It simply “reverts” the commit you point to the previous one. This last statement is very important to get and is mostly the source of confusion.

You can conclude from this that git revert only works on work that you have committed and not if you have code that is “not checked in” yet.

how to delete branch on remote


This is an action that many Git users need to do frequently, but many (including the author) have forgotten how to do so or simply don’t know how. Here’s the definitive guide if you’ve forgotten.

So let’s say you have checked out a new branch, committed some awesome changes, but now you need to share this branch though with another developer. You can push the branch up to a remote very simply:

git push origin newfeature

Where origin is your remote name and newfeature is the name of the branch you want to push up. This is by far the easiest way, but there’s another way if you want a different option. Geoff Lane has created a great tutorial which goes over how to push a ref to a remote repository, fetch any updates, and then start tracking the branch.

Deleting is also a pretty simple task (despite it feeling a bit kludgy):

git push origin :newfeature

That will delete the newfeature branch on the origin remote, but you’ll still need to delete the branch locally with git branch -d newfeature.

There’s a script called git-publish-branch created by William Morgan that can easily automate this process if you find yourself performing these actions frequently. It also makes deleting remote branches feel a bit more natural. Know of better or easier ways to do the above tasks? Let us know in the comments or submit your own tip!

generating git ids


~/repos/ubcplot/writegit.py

#!/usr/bin/env python import ConfigParser import os,textwrap,re,string

import shlex from subprocess import Popen, PIPE, STDOUT

def getstatusoutput(command_line): “”” usage: status,output=getstatusoutput(commandstring) In python > 3.0 this is subprocess.getstatusoutput “”” args = shlex.split(command_line) p = Popen(args, stdout=PIPE, stderr=STDOUT, shell=False) s = p.stdout.read() return p.wait(), s

find_branch=re.compile(‘^\#\#\s(?P<branch>.*)’)

def writeconfig(versionfile): #first get the branch and check to see whether it’s clean #if not, exit with a list of files to commit status,output=getstatusoutput(“git status -s -b -uno”) output=output.split(‘\n’) thematch=find_branch.match(output[0]) branch=thematch.group(‘branch’) if len(output) !=2: text = “”” ######################### git status says files need to be checked in, either commit or stash:


%s “”” raise Exception(textwrap.dedent(text) % string.join(output[1:],’\n’)) status,commitid=getstatusoutput(“git log –pretty=’%h’ -n 1”) status,date=getstatusoutput(“git log –pretty=’%ci’ -n 1”) status,tags=getstatusoutput(“git name-rev –always –tags HEAD”) status,machine_output=getstatusoutput(“uname -a”) working_dir= os.getcwd() machine= machine_output.strip() config=ConfigParser.ConfigParser() config.add_section(‘git_info’) config.set(‘git_info’,’commitid’,commitid.strip()) config.set(‘git_info’,’branch’,branch.strip()) config.set(‘git_info’,’commit_date’,date.strip()) config.set(‘git_info’,’nearest_tag’,tags.strip()) config.set(‘git_info’,’working_dir’,working_dir) config.set(‘git_info’,’machine’,machine) outfile=open(versionfile,’w’) config.write(outfile) outfile.close()

if __name__==”__main__”: configfile=’/tmp/versioninfo.cfg’ writeconfig(configfile) print “writing to config file: “,configfile print “contents of %s: are:” % configfile checkfile=open(configfile) all_lines=checkfile.read() print “-“*30 print all_lines print “-“*30 checkfile.close()

working with FETCH_HEAD


http://stackoverflow.com/questions/4192783/git-diff-giving-me-different-feedback-than-git-merge

http://gitster.livejournal.com/28309.html?thread=15509 git log -p ..FETCH_HEAD

git reference


http://gitref.org/

recovering lost commits


http://programblings.com/2008/06/07/the-illustrated-guide-to-recovering-lost-commits-with-git/

using reset


http://progit.org/2011/07/11/reset.html http://speakerdeck.com/u/schacon/p/a-tale-of-three-trees

patching an individual file


git log esat.py git diff -p 10faac..e4770 esat.py > out.patch patch < out.patch

commands for a405


git clone git@github.com:phoustona/A405.git mya405

$ git clone git@github.com:phoustona/A405.git mya405 Cloning into ‘mya405’… Enter passphrase for key ’c/Users/paustin.ssh/id_rsa’: remote: Counting objects: 204, done. remote: Compressing objects: 100% (133/133), done. remote: Total 204 (delta 85), reused 174 (delta 55) 204) Receiving objects: 100% (204/204), 49.55 KiB, done. Resolving deltas: 100% (85/85), done.

paustin@COOT ~/repos $ cd mya405/

clip1.png

paustin@COOT ~/repos/mya405 (master) $ git remote add readonly git://github.com/phaustin/A405.git

paustin@COOT ~/repos/mya405 (master) $ git remote add readonly git://github.com/phaustin/A405.git

paustin@COOT ~/repos/mya405 (master)

paustin@COOT ~/repos/mya405 (master) $ ls Read_today.txt Readme.rst docs matlab python

paustin@COOT ~/repos/mya405 (master) $ git fetch readonly remote: Counting objects: 74, done. remote: Compressing objects: 100% (41/41), done. remote: Total 62 (delta 27), reused 56 (delta 21) Unpacking objects: 100% (62/62), done. From git://github.com/phaustin/A405

  • [new branch] master -> readonly/master

clip2.png

paustin@COOT ~/repos/mya405 (master) $ git branch

master

paustin@COOT ~/repos/mya405 (master)

paustin@COOT ~/repos/mya405 (master) $ git branch –set-upstream a405main readonly/master Branch a405main set up to track remote branch master from readonly.

clip3.png

paustin@COOT ~/repos/mya405 (master) $ git branch a405main

master

paustin@COOT ~/repos/mya405 (master) $ git branch a405main

master

paustin@COOT ~/repos/mya405 (master) $ git rebase a405main First, rewinding head to replay your work on top of it… Applying: entropy added to glossary

clip4.png

austin@COOT ~/repos/mya405 (master) git push origin master nter passphrase for key ’c/Users/paustin.ssh/id_rsa’: o git@github.com:phoustona/A405.git ! [rejected] master -> master (non-fast-forward) rror: failed to push some refs to ‘git@github.com:phoustona/A405.git’ o prevent you from losing history, non-fast-forward updates were rejected erge the remote changes (e.g. ‘git pull’) before pushing again. See the Note about fast-forwards’ section of ‘git push –help’ for details.

austin@COOT ~/repos/mya405 (master) git rebase origin master irst, rewinding head to replay your work on top of it… pplying: change makeSkew, makeGrid signatures pplying: add mixing and heat engine plots pplying: add vapor calculation for 410 hPa pplying: python libs pplying: new thermo for python pplying: revert esat.py changes

clip5.png

paustin@COOT ~/repos/mya405 (master) $ git push origin master Enter passphrase for key ’c/Users/paustin.ssh/id_rsa’: Counting objects: 74, done. Delta compression using up to 2 threads. Compressing objects: 100% (62/62), done. Writing objects: 100% (62/62), 27.08 KiB, done. Total 62 (delta 27), reused 0 (delta 0) To git@github.com:phoustona/A405.git cf4d85f..472fdc3 master -> master

clip6.png

austin@COOT ~/repos/mya405 (master)

paustin@COOT ~/repos/mya405 (master)

paustin@COOT ~/repos/mya405 (master) $ git checkout master Already on ‘master’

paustin@COOT ~/repos/mya405 (master) $ git rebase a405main First, rewinding head to replay your work on top of it… Applying: entropy added to glossary

paustin@COOT ~/repos/mya405 (master)

$ git checkout master Already on ‘master’

paustin@COOT ~/repos/mya405 (master)

paustin@COOT ~/repos $ git clone git@github.com:phoustona/A405.git mya405 Cloning into ‘mya405’… Enter passphrase for key ’c/Users/paustin.ssh/id_rsa’: remote: Counting objects: 292, done. remote: Compressing objects: 100% (189/189), done. remote: Total 292 (delta 127), reused 252 (delta 87)Receiving objects: 67% (196 Receiving objects: 68% (199/292) Receiving objects: 100% (292/292), 78.97 KiB, done. Resolving deltas: 100% (127/127), done.

paustin@COOT ~/repos $ cd mya405

paustin@COOT ~/repos/mya405 (master) $ git remote add readonly git://github.com/phaustin/A405.git

paustin@COOT ~/repos/mya405 (master) $ git fetch readonly From git://github.com/phaustin/A405

  • [new branch] master -> readonly/master

paustin@COOT ~/repos/mya405 (master) $ git branch –set-upstream a405main readonly/master Branch a405main set up to track remote branch master from readonly.

paustin@COOT ~/repos/mya405 (master) $ git cd docs git: ‘cd’ is not a git command. See ‘git –help’.

Did you mean this? add

paustin@COOT ~/repos/mya405 (master) $ cd docs

paustin@COOT ~/repos/mya405/docs (master) $ git add glossary.rst

paustin@COOT ~/repos/mya405/docs (master) $ git commit -m ‘internal energy def.’ [master 7baf976] internal energy def. 1 files changed, 10 insertions(+), 0 deletions(-)

paustin@COOT ~/repos/mya405/docs (master) $ git push origin master Enter passphrase for key ’c/Users/paustin.ssh/id_rsa’: Counting objects: 7, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 569 bytes, done. Total 4 (delta 3), reused 0 (delta 0) To git@github.com:phoustona/A405.git acf9e7a..7baf976 master -> master

adding a remote branch to a repository


git remote add nchap_git git://github.com/nchaparr/A405.git git fetch nchap_git

~/public_html/courses/a405repos/a405 phil@tern% git co -b nchap_git nchap_git/master Branch nchap_git set up to track remote branch master from nchap_git.

showing remote branches in the repository


list all branches

git branch -r

git remote show origin

git ls-remote –heads origin

using nbstripout to strip figures on commit

https://github.com/cfriedline/ipynb_template

put this in ~/.gitconfig

[filter “nbstrip”] clean = /Users/phil/bin/nbstripout smudge =cat required

check this into repository

.gitattributes contains

*.ipynb filter=nbstrip

filter-branch

Larry, for future reference here’s a post-mortem on my scm git repository cloning. I was able to get the size of the cloned repository down from 38 Gbytes to 24 Mbytes with a little git-fu.

  1. doing a straight clone of /home/rls/gitrepo/scm.git

on lxsrv3 expands the size of your bare repository from 13 Gb to 38 Gb and throws the following error message for me:

acrnrpa@lxsrv3:~/repos$ git clone home/rls/gitrepo/scm.git scm Initialized empty Git repository in /home/rpa/repos/scm.git/ error: failed to read object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 at offset 7871459997 from home/rpa/repos/scm.git/objects/pack/pack-64738f372a05123dcc20c29d6027dc91dc8ceb2a.pack error: failed to read object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 at offset 7871459997 from .git/objects/pack/pack-64738f372a05123dcc20c29d6027dc91dc8ceb2a.pack fatal: object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 is corrupted

Not sure what’s going on here (git or filesystem?), but when I tarred your repository and scp’d it over to a UBC machine, cloning succeeded on a local disk without error. (git 1.7.9.5 on my machine vs. git 1.7.0.4 on lxsrv3)

  1. To get it down from 38 Gb to 24 Mb I:

a) used filter-branch as described here:

http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

to delete the data and archive directories from the repository

b) at this point the repository was still 13 Gb, because the pack file in gitrepo/scm.git/objects/pack was still 13 Gb. Following

http://stackoverflow.com/questions/1029969/why-is-my-git-repository-so-big

I cloned it using git clone file:///home/rpa/repos/… scm_pha, which copied over the files and references but pruned the pack file

c) The 24 Mb (bare) repository is now at /home/rpa/repos/scm_pha

best, Phil

accept all changes for cherry pick

git rebase -s recursive -X theirs ${branch}

push a new version of an old tag

git tag -d lab5 git push origin :refs/tags/lab5

clone using ssh:

git clone ssh://phil@pip.eos.ubc.ca//home/phil/repos/parpy_bare parpy

pull vs rebase

http://stackoverflow.com/questions/20158074/how-to-make-magit-pull-to-have-rebase-option-by-default

I do not know any Magit-specific setting, but you can just configure git itself accordingly:

git config –global pull.rebase true

I’d strictly advise against on enabling this setting globally.

As I have said in my comment, you should really change your development process so that there is no need for merging/rebasing on git pull. Don’t work on shared branches, but instead always create your own feature branches for your work, and explicitly merge when needed.

You can then explicitly configure individual shared branches in your repository for rebasing, e.g.

git config –local branch.my-fancy-feature.rebase true

As for the hook, there is no specific hook for git pull. However, there is no harm in updating the tags file on other changes to the tree as well, so you may just the post-rewrite (for git rebase) and post-merge (for git merge) hooks.

See Tim Pope’s post Effortless Ctags with Git for details on such a setup.

magit squash commits

http://howardism.org/Technical/Emacs/magit-squashing.html

magit-status

l l to bring up log

highlight region with changes to squash

r i for interactive rebase (not r e)

s to squash

C-c C-c to bring up message

edit

C-c C-c to commit

show branches in magit

y .

removing a binary file

Larry, for future reference here’s a post-mortem on my scm git repository cloning. I was able to get the size of the cloned repository down from 38 Gbytes to 24 Mbytes with a little git-fu.

  1. doing a straight clone of /home/rls/gitrepo/scm.git

on lxsrv3 expands the size of your bare repository from 13 Gb to 38 Gb and throws the following error message for me:

acrnrpa@lxsrv3:~/repos$ git clone home/rls/gitrepo/scm.git scm Initialized empty Git repository in /home/rpa/repos/scm.git/ error: failed to read object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 at offset 7871459997 from home/rpa/repos/scm.git/objects/pack/pack-64738f372a05123dcc20c29d6027dc91dc8ceb2a.pack error: failed to read object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 at offset 7871459997 from .git/objects/pack/pack-64738f372a05123dcc20c29d6027dc91dc8ceb2a.pack fatal: object 8cafafbbd3c70e8883901c2fb396916eaf1b46c2 is corrupted

Not sure what’s going on here (git or filesystem?), but when I tarred your repository and scp’d it over to a UBC machine, cloning succeeded on a local disk without error. (git 1.7.9.5 on my machine vs. git 1.7.0.4 on lxsrv3)

  1. To get it down from 38 Gb to 24 Mb I:

a) used filter-branch as described here:

http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

to delete the data and archive directories from the repository

b) at this point the repository was still 13 Gb, because the pack file in gitrepo/scm.git/objects/pack was still 13 Gb. Following

http://stackoverflow.com/questions/1029969/why-is-my-git-repository-so-big

I cloned it using git clone file:///home/rpa/repos/… scm_pha, which copied over the files and references but pruned the pack file

c) The 24 Mb (bare) repository is now at /home/rpa/repos/scm_pha

best, Phil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment