Skip to content

Instantly share code, notes, and snippets.

@vlrmprjct
Last active November 5, 2023 09:50
Show Gist options
  • Save vlrmprjct/0464d756451fe0740a87 to your computer and use it in GitHub Desktop.
Save vlrmprjct/0464d756451fe0740a87 to your computer and use it in GitHub Desktop.
Useful GIT commands #git
git commit --amend --author="John Doe <john@doe.org>"
git checkout -b {NEW_branch_name} {SOURCE_branch_name}
git clone --branch <tag_name> <repo_url> --depth 1
#delete local branch
git branch -d feature/login
#delete remote branch
git push origin --delete feature/login
git branch -d -f `git branch --list '3.2.*'`
git branch -d -f `git branch --list 'release/*'`

git commit stats

Commands to get commit statistics for a Git repository from the command line - using git log, git shortlog and friends.




List repository contributors by author name (sorted by name):

$ git log --format='%aN' | sort -u

Example output:

Jane Bar
John Foo
Steve Baz



List total commits by author (sorted by commit count):

$ git shortlog -sn

Example output:

136 Jane Bar
 41 John Foo
 17 Steve Baz

Ignore merge commits:

$ git shortlog -sn --no-merges

Example output:

121 Jane Bar
 36 John Foo
 14 Steve Baz

Even though the --no-merges option is not documented for git shortlog, it works exactly as defined for git log.




List file change stats by author:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat | awk '{inserted+=$1; deleted+=$2; delta+=$1-$2; ratio=deleted/inserted} END {printf "Commit stats:\n- Lines added (total)....  %s\n- Lines deleted (total)..  %s\n- Total lines (delta)....  %s\n- Add./Del. ratio (1:n)..  1 : %s\n", inserted, deleted, delta, ratio }' -

Example output:

Commit stats:
- Lines added (total)....  4625
- Lines deleted (total)..  836
- Total lines (delta)....  3789
- Add./Del. ratio (1:n)..  1 : 0.180757

Include file count:

$ git log --shortstat --author="Vorname Nachname" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total)..  %s\n- Lines added (total)....  %s\n- Lines deleted (total)..  %s\n- Total lines (delta)....  %s\n- Add./Del. ratio (1:n)..  1 : %s\n", files, inserted, deleted, delta, ratio }' -

Example output:

Commit stats:
- Files changed (total)..  439
- Lines added (total)....  4625
- Lines deleted (total)..  836
- Total lines (delta)....  3789
- Add./Del. ratio (1:n)..  1 : 0.180757

Ignore merge commits:

Note: Both commands above also count merge commits. But to ignore them, one can simply use the --no-merges option again:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat --since="1 Jan, 2015" | awk ...
# or
$ git log --shortstat --author="Vorname Nachname" --since="1 Jan, 2015" | grep -E ...

Filter stats by date:

You can filter the output of the above commands, for example, by adding --until or --since or --before:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat --since="1 Jan, 2015" | awk ...
# or
$ git log --shortstat --author="Vorname Nachname" --since="1 Jan, 2015" | grep -E ...






# FORCE GIT PULL - OVERRIDE LOCAL CHANGES
git fetch --all
git reset --hard origin/master
git checkout master
git pull origin master
git merge test
git push origin master
git checkout -b branchname remotes/origin/branchname --
#OR SHORTHAND
git checkout --track origin/branch_name
git checkout your-feature-branch
git pull --rebase origin master
git rm -r --cached path_to_your_folder/
# Checkout
git checkout --orphan latest_branch
# Add all the files
git add -A
# Commit the changes
git commit -am "commit message"
# Delete the branch
git branch -D master
# Rename the current branch to master
git branch -m master
# Finally, force update your repository
git push -f origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment