Skip to content
This repository has been archived by the owner on Jul 26, 2020. It is now read-only.

Latest commit

 

History

History
248 lines (198 loc) · 6.61 KB

git.org

File metadata and controls

248 lines (198 loc) · 6.61 KB

Git

Table of Contents

References

add

intent-to-add

$ 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
# Mark all files including untracked to add
git add --intent-to-add --all

branch

Cleanup branches

$ 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
# Delete all merged, except master
$ git branch --merged | grep -vE '(master|develop)' | xargs git branch -d

Cleanup remote branch references

# Show targets
$ git branch --remotes
$ git remote prune origin
# prune when fetching
$ git fetch --prune

diff

git diff --exit-code # Exits 0: no differences, 1: differences
git diff --quiet     # No outputs. Implies --exit-code

flow

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

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.

list-tree

# lists all of the already committed files being tracked by your git repo.
$ git ls-tree --full-tree -r HEAD

log

Find deleted files

git log --all --full-history -- **/thefile.*
git log --all --full-history -- <path-to-file>  # if you know the exact path

# Reveal the content
git show <SHA> -- <path-to-file>

# Note '^', checking out from the previous commit.
# There won't exist the file in <SHA> because it has been deleted.
git checkout <SHA>^ -- <path-to-file>

pull

git pull --rebase --autostash # git pull accepts '--autostash' from 2.9
git fetch
git rebase --autostash

rm

# Untrack <file> recursively
$ git rm -r --cached <file>

tag

Prune local git tags that don’t exist on remote

$ git tag -l | xargs git tag -d # remove all local tags
$ git fetch -t                  # fetch remote tags

worktree

# Basics
$ git fetch
$ git worktree add -b bugfix-1234 ../bugfix origin/master

# Temporary
$ git worktree add --detach ../project-build HEAD

# Cleanup
$ rm -rf ../bugfix && git worktree prune
  • -b bugfix-1234 option creates a new branch named bugfix-1234
  • ../bugfix is the new local copy
  • Based on origin/master
  • --detach makes the working copy detached.

gitignore

How to

Get git root directory

git rev-parse --show-toplevel

Run commands while not in a git directory

git -C ~/foo status  # equivalent to (cd ~/foo && git status)

Resolve conflicts

If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-a

Fix the conflict like this:

If you have questions, please open an issue or ask in our IRC channel if it's more urgent

Stage it:

$ git add guide.md
  • Case1. commit when merging
$ git commit -m "Resolved merge conflict"
  • Case2. rebase –continue when rebasing
$ git rebase --continue

Purge files including their histories

git clone --mirror [email protected]:yeonghoey/yeonghoey.git
java -jar ~/.local/bin/bfg.jar --strip-blobs-bigger-than 1M yeonghoey.git
cd yeonghoey.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

How to git commit nothing without an error

if ! git diff --quiet --cached; then
  git commit --verbose
fi

# or just
git diff --quiet --cached || git commit