Skip to content

Latest commit

 

History

History
169 lines (119 loc) · 4.78 KB

File metadata and controls

169 lines (119 loc) · 4.78 KB

git Tips

Table of Contents

 

Check Impact-of-Change

See how many files (and lines) have changed, between two commits

git diff --shortstat <commit1> <commit2>

(from How can I calculate the number of lines changed between two commits in git)

 

Count Lines-of-Code

Using the CLOC Tool

CLOC (Count Lines Of Code) is a Perl-script that does just that.

You can also install it as a package from your Linux-distribution (which is great)
or run it as a docker-container (which is an overkill in most cases).

Run it in a Git-repo, like so:

cloc --vcs git
# or
cloc --git .

Using Git and Unix Commands

Count ALL lines-of-code in a repository (limited to a selected set of file-types) -

git ls-files | grep -P "^[^\s]+[.](c|cpp|h|hpp|java|js)$" | xargs cat | wc -l

This example does not handle file-names with spaces, so skipping these files.

(from Count number of lines in a git repository)

 

Find Commits that Introduced a Change

# Find commits with a specific RegEx
git log -G '<regex-to-find>'

# ... ignore-case
git log -i -G '<regex-to-find>'

# ... in all branches
git log --all -G '<regex-to-find>'

 

Find Large Files in a Repository

Find Files Larger than 1MB (1000000 bytes)

export SIZE_IN_BYTES=1000000;
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | grep -v -e 'tag' | awk '$3 > ENVIRON["SIZE_IN_BYTES"]' | sort --numeric-sort --key=3

Find the 7 Largest Files in a Repository

For macOS, make sure you have coreutils:

brew install coreutils

then run:

export FILE_COUNT=7;
git rev-list --objects --all |
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
  sed -n 's/^blob //p' |
  sort --human-numeric-sort --reverse --key=2 |
  $(command -v gcut    || echo cut) -c 1-12,41- |
  $(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest | head -n "$FILE_COUNT"

(from How to find/identify large commits in git history?)

Find the Commit that Added a (Large) File

git log --raw --all --find-object=<BLOB_ID>

(from Which commit has this blob?)

Get Commit-Statistics

Get Commit-Count by Author

git shortlog --summary --numbered --all
# or
git shortlog --summary --numbered --all --no-merges
# or
git shortlog --summary --numbered --all --no-merges --email

(from Git number of commits per author on all branches)

 

Merge Two Git-Repositories

Use this when you have two git-repositories that started the same, and later got diverged.
(If these repositories are completely different than there is not my point in merging them)

To merge branch some-branch from project-a into project-b:

cd path/to/project-a
git checkout some-branch

cd path/to/project-b
git remote add project-a /path/to/project-a
git fetch project-a --tags
git merge                             project-a/some-branch
# OR:
git merge --allow-unrelated-histories project-a/some-branch
git remote remove project-a

(from How do you merge two Git repositories?)

 

Rename the Default Branch from 'master' to 'main'

  1. Change the name of the default branch via the UI of GitLab/GitHub
  2. Run the following commands on your local copy of that repo:
    git branch -m master main
    git fetch origin
    git branch -u origin/main main
    git remote set-head origin -a
    # Optional:
    git fetch --prune