GIT

From DaqWiki
Jump to navigation Jump to search

Create personal "cloneable" repository

This uses the "dual" repository method: the first repository is a bare repository is exported to the world via httpd, the second repository is where all work is done, committed. When ready, the work repository is pushed to the bare repository and becomes visible to the world.

$ cd ~/public_html/git
$ git init --bare rpms.git
Initialized empty Git repository in /home/olchansk/public_html/git/rpms.git/
$ cd rpms.git/hooks
$ mv post-update.sample post-update
$ chmod a+x ./post-update 
$ cd ~/git/
$ git clone ~/public_html/git/rpms.git rpms
warning: You appear to have cloned an empty repository.
$ cd rpms
$ date > test
$ git add test
$ git commit -m"test"
[master (root-commit) 9236a84] test
$ git push origin master
To /home/olchansk/public_html/git/rpms.git
 * [new branch]      master -> master
$ rm test
$ date > test 
$ git add test
$ git commit -m"test"
[master 54a6b4e] test
$ git push
To /home/olchansk/public_html/git/rpms.git
   9236a84..54a6b4e  master -> master
$ ssh to somewhere else
$ git clone http://ladd00.triumf.ca/~olchansk/git/rpms.git rpms

Merge history from another git repository

$ git fetch file:///home/olchansk/sysadm/rpm/diskscrub/ master:diskscrub_merge
$ git checkout master:diskscrub_merge
$ mkdir diskscrub
$ git add diskscrub
$ git mv *.* diskscrub
$ ls -la
$ git status
$ git commit -m"move into subdirectory" -a
$ git checkout master
$ ls -la
$ git merge diskscrub_merge
$ git log --graph
$ git log --graph --oneline
$ git push

Setup mirror of the ROOT git repository

ssh daqweb@ladd00
   204  16:53   cd public_html/
   206  16:54   cd git/
   221  17:04   git clone --mirror http://root.cern.ch/git/root.git root.git
   231  17:23   cd root.git/hooks/
   233  17:23   mv post-update.sample post-update
   234  17:24   chmod a+x ./post-update

Additional commands:

  • fetch latest version: cd root.git; git fetch; git update-server-info
  • list all version tags: cd root.git; git tag -l
  • list all version branches: cd root.git; git branch

Convert ROOTANA SVN to GIT

  • extract the authors list: svn log -q file:///home/daqweb/svn/rootana | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors.txt
  • adjust authors list: vi authors.txt
(no author) = (no author) <(no author)>
daqweb = Konstantin Olchanski <daqweb>
lindner = Thomas Lindner <lindner>
olchansk = Konstantin Olchanski <olchansk>
  • run conversion: git svn clone --stdlayout -A authors.txt file:///home/daqweb/svn/rootana
  • doctor the SVN revision comments: cd rootana; git filter-branch -d /tmp/tmpfs/gitrewrite --msg-filter 'sed "/git-svn-id:/s;^.*/\([^/]*\)@\([0-9]*\).*$;SVN revision: \2;"' -- --all
  • new git commit messages look like this:
commit 84461b29deccf4ce82893f9275f6e44a303943c4
Author: Thomas Lindner <lindner>
Date:   Mon Jul 8 23:30:09 2013 +0000

    Add a sleep to offline/paused display; otherwise program takes 100% of CPU...
    
    
    SVN revision: 133

Convert MUSR CVS to GIT

  • login to el6 (SL6) machine: ssh ladd00
  • install recent svs2svn
# get version 2.3.0
#cd ~olchansk/packages/cvs2svn
#wget ftp://ftp.pbone.net/mirror/dag.wieers.com/redhat/el6/en/x86_64/dag/RPMS/cvs2svn-2.3.0-1.el6.rf.noarch.rpm
#rpm -vh --install cvs2svn-2.3.0-1.el6.rf.noarch.rpm
# get version 2.5.0 (latest as of 5 july 2019)
cd ~olchansk/packages/cvs2svn
wget http://cvs2svn.tigris.org/files/documents/1462/49543/cvs2svn-2.5.0.tar.gz
tar xzvf cvs2svn-2.5.0.tar.gz
  • copy the CVS repository:
cd ~olchansk/tmp
rsync -av dasdevpc2:/usr/local/cvsroot/musr .
chown -R root.root musr # as root
  • run the conversion
~/packages/cvs2svn/cvs2svn-2.5.0/cvs2git --blobfile=git-blob.dat --dumpfile=git-dump.dat --username=musr --fallback-encoding=ascii --symbol-default=branch --write-symbol-info=symbols.dat --keep-trivial-imports musr >& xxx1
  • import into git
git init --bare musr.git
cd musr.git
cat ../git-blob.dat ../git-dump.dat | git fast-import
git gc --prune=now
  • push to bitbucket:
# on bitbucket, create an empty git repository, then:
git remote add origin git@bitbucket.org:ttriumfdaq/musr.git
git remote -v
git push -u origin --all

Push ROOTANA GIT to BITBUCKET

  • create empty bitbucket public repository
  • git remote add origin https://dd1@bitbucket.org/tmidas/test.git ### authentication using https password
  • git remote add origin git@bitbucket.org:tmidas/test.git ### authentication using ssh key
  • git push -u origin --all ### you will be prompted for your password

Clone, commit and push to BITBUCKET

 1141  git clone https://bitbucket.org/tmidas/test.git
 1143  cd test
 1145  cd tests
 1147  touch yyy
 1148  git add yyy
 1149  git commit
 1151  git push https://dd1@bitbucket.org/tmidas/test.git

Add alternative git source to your local repository

git remote add triumf http://daq.triumf.ca/git/midas.git

Fix defective commits

!!! THIS CAN ONLY BE DONE BEFORE "git push" !!!

When I commit some changes and accidentally miss some file, I commit the missing file in the next commit, but now I have 2 commits instead of 1. Fix it by squashing the two commits into one:

Run git rebase -i HEAD~2, an editor will open listing the 2 commits. For the second commit, replace "pick" with "fixup", save and exit. Git will merge the 2 commits discarding the commit message of the second (junk) commit.

Create local branch, push it to bitbucket

  522  git branch feature/hsconfig
  524  git checkout feature/hsconfig
  525  git branch -a
  528  xemacs src/mlogger.cxx 
  546  git commit src/mlogger.cxx 
  556  git push -u origin feature/hsconfig
  557  git remote show origin
  558  git status
  559  git pull
  560  git push

"git remote show origin" should show something like this:

konstantin-olchanskis-macbook:midas olchansk$ git remote show origin
* remote origin
  Fetch URL: git@bitbucket.org:tmidas/midas
  Push  URL: git@bitbucket.org:tmidas/midas
  HEAD branch: develop
  Remote branches:
    develop           tracked
    feature/CSS       tracked
    feature/MSCB_scan tracked
    feature/MT_trans  tracked
    feature/hsconfig  tracked
    master            tracked
  Local branches configured for 'git pull':
    develop          merges with remote develop
    feature/hsconfig merges with remote feature/hsconfig
  Local refs configured for 'git push':
    develop          pushes to develop          (up to date)
    feature/hsconfig pushes to feature/hsconfig (up to date)
konstantin-olchanskis-macbook:midas olchansk$ 

git blame

Commands used to trace old changes to mhttpd.cxx:

 (note the filename change from .cxx to .c)
 (note "^" means start looking from the previous commit, otherwise we stay on the same commit forever)

 1002  git blame src/mhttpd.cxx > xxx
 1023  git blame 5304a64^ -- src/mhttpd.c > xxx1
 1026  git blame ee2c5242^ -- src/mhttpd.c > xxx2
 1028  git blame c71e7851^ -- src/mhttpd.c > xxx3
 1030  git blame ef832017^ -- src/mhttpd.c > xxx4

git remote

git remote -v
git remote set-url --push origin olchansk@ladd00.triumf.ca:public_html/git/scripts.git

Split files into separate repo

We will split midas/include/mjson.h and midas/src/mjson.cxx into a separate repository mjson/{mjson.h,mjson.cxx}

git clone midas
rsync -av midas midas-tmp
cd midas-tmp/midas
git remote remove origin ### for safety disconnect from bitbucket
git subtree split --prefix=include -b include
git subtree split --prefix=src -b src
cd ..
mkdir mjson
cd mjson
git init
touch .gitignore
git add .gitignore 
git commit .
git subtree add --prefix=include ../midas include
git subtree add --prefix=src ../midas src
git mv include/mjson.h .
git mv src/mjson.cxx .
git status .
git commit .
git annotate mjson.h ### shows full history
git annotate mjson.cxx  ### shows full history
git rm include src
git rm -r include src
git commit .
git reflog expire --expire=now --all
git gc --prune=now --aggressive
cd ../..
mv midas-tmp/mjson .
rm -rf midas-tmp

If it is only one subdirectory (i.e. split rootana/libMidasInterface to libmidasio), the second half is simpler:

(in rootana: git subtree split --prefix=libMidasInterface -b midasio)
mkdir midasio
cd midasio/
git init
git remote add rootana ../rootana
git pull rootana midasio
git log ## full history should be there

Wisdom from ROOT