<commit_before>#+TITLE: Git

* Table of Contents                                                :TOC_2_gh:
 - [[#reference][Reference]]
   - [[#add][add]]
   - [[#branch][branch]]
   - [[#clone][clone]]
   - [[#config][config]]
   - [[#diff][diff]]
   - [[#flow][flow]]
   - [[#gitignore][gitignore]]
   - [[#list-tree][list-tree]]
   - [[#log][log]]
   - [[#pull][pull]]
   - [[#rebase][rebase]]
   - [[#rm][rm]]
   - [[#submodule][submodule]]
   - [[#tag][tag]]
   - [[#worktree][worktree]]
 - [[#details][Details]]
   - [[#git-attributes][Git Attributes]]
 - [[#how-to][How to]]
   - [[#get-git-root-directory][Get git root directory]]
   - [[#how-to-git-commit-nothing-without-an-error][How to git commit nothing without an error]]
   - [[#list-all-todos-written-by-a-specific-user][List all TODOs written by a specific user]]
   - [[#purge-files-including-their-histories][Purge files including their histories]]
   - [[#resolve-conflicts][Resolve conflicts]]
   - [[#run-commands-while-not-in-a-git-directory][Run commands while not in a git directory]]
 - [[#links][Links]]
 - [[#staging][Staging]]
   - [[#2017-02-14-tue-git-hooks][<2017-02-14 Tue> git hooks]]

* Reference
** add
*** intent-to-add
- https://robots.thoughtbot.com/intent-to-add
- ~git add --intent-to-add~, the specified untracked files will be added to the index, *but without content.*

#+BEGIN_SRC shell
  $ echo hello > target-file
  $ git add --patch
  No chagnes.
  $ git add --intent-to-add target-file
  $ git add --patch
  index e69de29..ce01362 100644
  --- a/sub1/target-file
  +++ b/sub1/target-file
  @@ -0,0 +1 @@
  +hello
#+END_SRC

#+BEGIN_SRC shell
  # Mark all files including untracked to add
  git add --intent-to-add --all
#+END_SRC

** branch
- http://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

*** Cleanup branches
#+BEGIN_SRC shell
  $ git checkout master

  # Show targets before deleting
  $ git branch --merged
  $ git branch -d old-merged-feature

  $ git branch --no-merged
  $ git branch -D old-abandoned-feature
#+END_SRC

#+BEGIN_SRC shell
  # Delete all merged, except master
  $ git branch --merged | grep -vE '(master|develop)' | xargs git branch -d
#+END_SRC

*** Cleanup remote branch references
#+BEGIN_SRC shell
  # Show targets
  $ git branch --remotes
  $ git remote prune origin
#+END_SRC

#+BEGIN_SRC shell
  # prune when fetching
  $ git fetch --prune
#+END_SRC

** clone
- https://stackoverflow.com/questions/1911109/how-to-clone-a-specific-git-branch

#+BEGIN_SRC shell
  # Clone only 'gh-pages' branch of 'note.git' repo into './notes-gh-pages'
  git clone -b gh-pages --single-branch https://github.com/yeonghoey/notes.git notes-gh-pages
#+END_SRC

** config
- https://git-scm.com/docs/git-config/2.13.1
- About the contents of ~.git/config~

| ~branch.<name>.remote~     | tells which remote to ~git fetch~ from and ~git push~ to |
| ~branch.<name>.pushRemote~ | overrides ~branch.<name>.remote~ for pushing             |

** diff
#+BEGIN_SRC shell
  git diff --exit-code # Exits 0: no differences, 1: differences
  git diff --quiet     # No outputs. Implies --exit-code
  git diff --name-only # Show only names of changed files
#+END_SRC

** flow
- http://danielkummer.github.io/git-flow-cheatsheet/index.html

*** git flow release finish
Make sure the local ~develop~ and ~master~ branches are fresh.
When trigger ~finish~, the merge flow is following:
1. merges ~release/something~  into ~master~
2. creates a tag on the ~master~ commit.
3. merges ~master~ into ~develop~

*** When it comes to code review
- http://softwareengineering.stackexchange.com/questions/187723/code-review-with-git-flow-and-github

~git flow feature finish~ just merges into develop without code review.
To work around this, just don't use ~finish~ but make a pull request
and delete the branch manually after it merged.<commit_msg>Update git#config<commit_after>#+TITLE: Git

* Table of Contents                                                :TOC_2_gh:
 - [[#reference][Reference]]
   - [[#add][add]]
   - [[#branch][branch]]
   - [[#clone][clone]]
   - [[#config][config]]
   - [[#diff][diff]]
   - [[#flow][flow]]
   - [[#gitignore][gitignore]]
   - [[#list-tree][list-tree]]
   - [[#log][log]]
   - [[#pull][pull]]
   - [[#rebase][rebase]]
   - [[#rm][rm]]
   - [[#submodule][submodule]]
   - [[#tag][tag]]
   - [[#worktree][worktree]]
 - [[#details][Details]]
   - [[#git-attributes][Git Attributes]]
 - [[#how-to][How to]]
   - [[#get-git-root-directory][Get git root directory]]
   - [[#how-to-git-commit-nothing-without-an-error][How to git commit nothing without an error]]
   - [[#list-all-todos-written-by-a-specific-user][List all TODOs written by a specific user]]
   - [[#purge-files-including-their-histories][Purge files including their histories]]
   - [[#resolve-conflicts][Resolve conflicts]]
   - [[#run-commands-while-not-in-a-git-directory][Run commands while not in a git directory]]
 - [[#links][Links]]
 - [[#staging][Staging]]
   - [[#2017-02-14-tue-git-hooks][<2017-02-14 Tue> git hooks]]

* Reference
** add
*** intent-to-add
- https://robots.thoughtbot.com/intent-to-add
- ~git add --intent-to-add~, the specified untracked files will be added to the index, *but without content.*

#+BEGIN_SRC shell
  $ echo hello > target-file
  $ git add --patch
  No chagnes.
  $ git add --intent-to-add target-file
  $ git add --patch
  index e69de29..ce01362 100644
  --- a/sub1/target-file
  +++ b/sub1/target-file
  @@ -0,0 +1 @@
  +hello
#+END_SRC

#+BEGIN_SRC shell
  # Mark all files including untracked to add
  git add --intent-to-add --all
#+END_SRC

** branch
- http://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

*** Cleanup branches
#+BEGIN_SRC shell
  $ git checkout master

  # Show targets before deleting
  $ git branch --merged
  $ git branch -d old-merged-feature

  $ git branch --no-merged
  $ git branch -D old-abandoned-feature
#+END_SRC

#+BEGIN_SRC shell
  # Delete all merged, except master
  $ git branch --merged | grep -vE '(master|develop)' | xargs git branch -d
#+END_SRC

*** Cleanup remote branch references
#+BEGIN_SRC shell
  # Show targets
  $ git branch --remotes
  $ git remote prune origin
#+END_SRC

#+BEGIN_SRC shell
  # prune when fetching
  $ git fetch --prune
#+END_SRC

** clone
- https://stackoverflow.com/questions/1911109/how-to-clone-a-specific-git-branch

#+BEGIN_SRC shell
  # Clone only 'gh-pages' branch of 'note.git' repo into './notes-gh-pages'
  git clone -b gh-pages --single-branch https://github.com/yeonghoey/notes.git notes-gh-pages
#+END_SRC

** config
- https://git-scm.com/docs/git-config/2.13.1
- About the contents of ~.git/config~

| ~branch.<name>.remote~     | Tells which remote to ~git fetch~ from and ~git push~ to                   |
| ~branch.<name>.pushRemote~ | Overrides ~branch.<name>.remote~ for pushing                               |
| ~remote.pushDefault~       | Overrides ~branch.<name>.remote~, overridden by ~branch.<name>.pushRemote~ |

** diff
#+BEGIN_SRC shell
  git diff --exit-code # Exits 0: no differences, 1: differences
  git diff --quiet     # No outputs. Implies --exit-code
  git diff --name-only # Show only names of changed files
#+END_SRC

** flow
- http://danielkummer.github.io/git-flow-cheatsheet/index.html

*** git flow release finish
Make sure the local ~develop~ and ~master~ branches are fresh.
When trigger ~finish~, the merge flow is following:
1. merges ~release/something~  into ~master~
2. creates a tag on the ~master~ commit.
3. merges ~master~ into ~develop~

*** When it comes to code review
- http://softwareengineering.stackexchange.com/questions/187723/code-review-with-git-flow-and-github

~git flow feature finish~ just merges into develop without code review.
To work around this, just don't use ~finish~ but make a pull request
and delete the branch manually after it merged.
{'fraction_non_alphanumeric': 0.15988955823293172, 'fraction_numerical': 0.016817269076305222, 'mean_word_length': 3.252401280683031, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7410701', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2674, 'n_words': 714}