Wednesday, 26 April 2017

[git] work with tags

1. Create

  • lightweight tag
    $ git tag v0.1
    
  • annotated tag
$ git tag -a v0.1 -m "my first version"
  • tag to a specific commit
    $ git tag -a v0.1 4ba28bf
    

2. Query

  • list all tags
    $ git tag
    ether_sysml.v0.4
    ether_sysml_broadcast_unicast_ether.v0.1
    fcu_sysml.v1.0
    fcu_sysml.v1.1
    uav_swarm_sysml.v1.0
    uav_swarm_sysml.v1.0.3
    
  • list all tags with annotations
$ git tag -n99
or
$ git tag -n -l
  • search with patterns

$ git tag -l "ether*"

ether_sysml.v0.4 ether_sysml_broadcast_unicast_ether.v0.1
  • show details of a tag
$ git show ether_sysml.v0.4
tag ether_sysml.v0.4
Tagger: Kangfeng Ye <kangfeng.ye@york.ac.uk>
Date:   Tue Apr 11 20:29:32 2017 +0100

the first version to have Ether with two Ends and create a test project successfully

commit 4ba28bf7e23e091e4d3768814e23204075f66b4c
...
  • sort by creation date
$ git for-each-ref --format '%(refname) %09 %(taggerdate) %(subject) %(taggeremail)' refs/tags  --sort=taggerdate
refs/tags/fcu_sysml.v1.0         Wed Mar 15 14:31:32 2017 +0000 V1.0 of FCU test model <kangfeng.ye@york.ac.uk>
refs/tags/fcu_sysml.v1.1          Thu Mar 16 12:55:34 2017 +0000 Update SysML by changing float to double  <kangfeng.ye@york.ac.uk>
refs/tags/uav_swarm_sysml.v1.0   Thu Mar 16 16:07:36 2017 +0000 SysML test model for UAV Swarm <kangfeng.ye@york.ac.uk>
refs/tags/uav_swarm_sysml.v1.0.3          Mon Mar 20 18:11:20 2017 +0000 Correct constant from 2.2 to 1.2  <kangfeng.ye@york.ac.uk>
refs/tags/ether_sysml_broadcast_unicast_ether.v0.1        Tue Apr 4 18:52:35 2017 +0100 This is a tag for the temporary version  to us <kangfeng.ye@york.ac.uk>
refs/tags/ether_sysml.v0.4        Tue Apr 11 20:29:32 2017 +0100 the first version to   <kangfeng.ye@york.ac.uk>

3. Push tags to remote repositories

  • a specific tag
$ git push origin v0.1
  • all tags
$ git push origin --tags

4. Checkout tags

  • checkout a tag into a new local branch

git checkout -b bv0.1 v0.1
Reference:

Wednesday, 19 April 2017

[Git] difference between two branches

 Show changes and log between two branches
  • $ git diff master..branch
  • $ git log master..branch
  • $ git shortlog master..branch
Show which files are changed between two branches
  • $ git diff --name-status master..branch
Show changes to a specific file between two branches
  • $ git diff branch master -- path/to/thefile
  • or $ git diff master..branch -- path/to/thefile

References:
  • this link (http://linux.yyz.us/git-howto.html#diff_branch)
  • this question (http://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-two-revisions)

[Git] how to merge a file from one branch into current branch

Scenario:
  • A branch (newbranch) is created and some update on this branch
  • Later you want to merge the changes made to a specific file (thefile) on  newbranch to another branch (oldbranch)
Steps:
  1. If current working branch is oldbranch, go to Step 4. Otherwise, go to Step 2
  2. Clone master branch
  3. Fetch all branches and create local branches to track all remote branches (following this post
  4. Switch to oldbranch, $ git checkout oldbranch
  5. Merge by $git checkout --patch newbranch relative_path/to/thefile
    • Apply this hunk to index and worktree [y,n,q,a,d,/,j,J,g,e,?]?
    • The meaning of each is given with the help of $ git add --help
patch
           This lets you choose one path out of a status like selection. After choosing the path, it presents the diff between the index and the working tree file and asks you if you want to stage the change of each hunk. You can select one of the following options and type return:

               y - stage this hunk
               n - do not stage this hunk
               q - quit; do not stage this hunk or any of the remaining ones
               a - stage this hunk and all later hunks in the file
               d - do not stage this hunk or any of the later hunks in the file
               g - select a hunk to go to
               / - search for a hunk matching the given regex
               j - leave this hunk undecided, see next undecided hunk
               J - leave this hunk undecided, see next hunk
               k - leave this hunk undecided, see previous undecided hunk
               K - leave this hunk undecided, see previous hunk
               s - split the current hunk into smaller hunks
               e - manually edit the current hunk
               ? - print help

[Git] fetch all remote branches and make them tracked by local branches


$ git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done 
$ git fetch --all
$ git pull --all

Reference from this How to fetch all git branches