Friday, December 28, 2012

GIT survival kit


  ssh -T git@192.168.0.444    (list remote git projects)
  git clone git@192.168.0.444:leg/testgitlab.git
               (sort of check-out, but copy all the history of all the files)
  cd testgitlab/
  vi 2.txt
  git add 2.txt      ("stage" the file)
  git commit -m " new file "  
               (local commit, commit everything that is "staged" into the local git repository)

  • show deleted files for each commit
    •   git log --diff-filter=D --summary
  • branches
    • to add a branch "prod" locally and in the remote repository
      • git branch your-branch
      • git push origin your-branch
    • git branch -av
      • list all remote branches wit their pointer value
    • "checkout" a remote branch
      • git switch MEDNEM-whatever
    • to add a branch "prod" in .git/config
      • git branch --set-upstream prod origin/prod
    • to have master point to a  former commit
      • git reset --hard 112ab334
      • git push -f
    • rename a branch locally and remotely
      • git branch -n new-name.  (locally)
      • git push origin --delete old-name
      • git push --set-upstream origin new-name
  • diff between the local repo and the central repo
    • git diff master origin/master
  • ignore last commit not yet pushed (move the reference pointer, doesn't delete the commit)
    • git reset HEAD~1
  • remove last commit locally and remotely (if branch not protected)
    • git reset --hard HEAD^ 
    • git push -f 
  • "undo" a commit (if branch protected)
    • 
      
    • git revert  e52b3378f42c82720c41e7dc08ae211c56ef164d
    • git push

    • if I want to revert to a commit that was a merge
      • git revert -m 1 6b90123523c78ecc5a63e88b4a8605a530e80e56
  • You can recover an individual file with:
git checkout -- JavaTVServiceXlet.java...
or to restore all the files that are have been deleted, you could do:
git ls-files -z --deleted | xargs -0 -n 1 git checkout --
  • get local repo at a specific commit id
    • git  reset  --hard  e52b3378f42c82720c41e7dc08ae211c56ef164d
    • see also at the end
      • git checkout -b tmp    03d0e...26a
  • Dev scenario 1
 2572  git clone git@192.168.0.44:/leg/leg/search-l
 2577  git branch
 2548  git branch dev_local
 2549  git branch
 2550  git checkout dev_local
----------- I do my dev and others commit too on the central repo
 2556  git status
 2557  git commit -a -m"." (you have to commit everything here, otherwise see Dev scenario 2)
 2558  git status
 2559  git checkout master
 2560  git pull
 2561  git checkout dev_local
 2562  git merge master dev_local
 2563  git checkout master
 2564  git merge dev_local  master
 2565  git push
  • Dev Scenario 2 (cherry-pick scenario)
2776  git commit your stuff
2776  git stash
2776  git branch tmp2
 2777  git checkout tmp2
 2781  git cherry-pick 96c53838ba55b72ef16d31f1076393de03c2e0e1 (possibly with option  -m 1)
 2789  git checkout master
 2790  git merge tmp2
 2793  git pull 
 2794  git push
 2795  git branch -D tmp2  
  • Dev Scenario 3 (add a branch out of release)
git checkout release
git branch my_branch release
git push origin my_branch   (my_branch points to release)
git checkout my_branch
git add myfile.txt
git commit ...
git push origin my_branch   (the commits is shown as head, ahead of master)
  • Dev Scenario 4 (create a branch from tags)
    • git submodule foreach git checkout tags/3.3.11
    • git submodule foreach git checkout  -b prod
    • git submodule foreach git push origin prod
  • Dev scenario 5: I mistakenly commit on branch and want to restart 
  • git stash
    • if you haven't commit anything and want to update the branch
    • git stash pop    (get back what was stashed, and remove it from the stash list)
  • To remove local tag
    •  git tag -d 3.3.03.rc2
  • To remove tags on central repository
    • git push origin :3.1.2.b1
    • git submodule foreach git push origin :3.1.2.b1
  • tagging
  • git tag -a 3.1.1.b1 -m"tag 3.1.1.b1 Apr 5th 2013 sprint 6"
    git push --tags
    • git  checkout  tad_id    (get a specific tag)
  • git merge conflict resolution
    • git merge one_branch
    • git checkout --ours pom.xml   (keep master version numbers), or edit files
    • git add pom.xml
    • git commit
    • git push
    •  
    • Note that during git rebase and git pull --rebase, 'ours' and 'theirs' may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.
  • git rebase
    • instead of
      • git merge one_branch
    • do
      • git rebase one_branch
      • git push --force
    • git rebase -i develop-1
      • allow to chose or get rid off some commits
      • allow to squash by replacing "pick" by "squash" all line from the second one
      • squashing
        • git rebase -i HEAD~3  (no. of commits you want to squash including the current one)
        • You will get one interactive prompt where you need to Pick the top commit and insert squash or s in front of those which you want to combine/squash.
        • you will get another interactive prompt, put # in front of commits message that you don't want, and or add your own message.
        • git push --force
  • Mistakenly committed to the master branch (but didn't push)
    • git branch -m  one_branch     (rename master to   one_branch)
    • more .git/config 
    • git branch --unset-upstream
    • more .git/config   (should be no more remote)
    • git push --set-upstream origin one_branch
    • git checkout master
  • Reverting Working Copy to Most Recent Commit
  • Show added files since a commit
    • git diff b28d54ac880c2381d700001aa9f50dcbb5f73617 --diff-filter=A --numstat
  • A branch from which I created my branch is removed
    • git fetch
    • git branch -f MEDNEM-3665 origin/MEDNEM-3665
    • git rebase --onto origin/MEDNEM-3665   7319...34a   MEDNEM-3716
      • (while you are on MEDNEM-3716)
  • I want to create a branch from a commit ID to test an older version
    • git checkout -b tmp    03d0e...26a
  • Config
  • command line commit tree visualization
    • git log --graph --oneline --all
    • there is another tool "gitk" but it adds limited value)
  • difftool
    • git difftool   commit_id_1   commit_id_2

    .