Skip to main content

Git Commands

https://github.com/GitAlias/gitalias - Aliases for Git commands

The git commands I run before reading any code

https://piechowski.io/post/git-commands-before-reading-code - https://news.ycombinator.com/item?id=47687273

Who built this. Every contributor ranked by commit count. If one person accounts for 60% or more, that’s your bus factor.

git shortlog -sn --no-merges

Also see https://github.com/GitAlias/gitalias/tree/main/doc/git-who and https://github.com/GitAlias/gitalias/tree/main/doc/git-chart

What changes the most. The 20 most-changed files in the last year. Run this from app/ or src/, not the repo root, otherwise lockfiles, changelogs and generated code will dominate the list.

git log --format=format: --name-only --since="1 year ago" | sort | uniq -c | sort -nr | head -20

Also see https://github.com/GitAlias/gitalias/tree/main/doc/git-churn

Where do bugs cluster:

git log -i -E --grep="fix|bug|broken" --name-only --format='' | sort | uniq -c | sort -nr | head -20

Is this project accelerating or dying. Commit count by month, for the entire history of the repo:

git log --format='%ad' --date=format:'%Y-%m' | sort | uniq -c

How often is the team firefighting:

git log --oneline --since="1 year ago" | grep -iE 'revert|hotfix|emergency|rollback'

remote

View:

git remote -v

Set:

git remote set-url origin git@github.com:AlbertVilaCalvo/Wiki.git

Change:

git remote set-url origin git@github.com:AlbertVilaCalvo/Wiki.git

Remove:

git remote remove origin

log

Docs: https://git-scm.com/docs/git-log

Viewing the Commit History: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

View file line history/changes

Show the changes from line 135 to 140:

git log --pretty=short -u -L 135,140:file/path/something.txt

source

View log (commits) for a file or folder

# file
git log package.json
git log -- package.json

# folder
git log path/to/folder
git log -- path/to/folder

https://stackoverflow.com/questions/11950037/view-git-history-for-folder

Search string on git history

git log -S something
git log -p -S something

source

Search by commit message

git log --all --grep='Something' # Commits whose message contain 'Something'
git log -i --grep='Something' # Case insensitive. Matches 'something' too
git log --invert-grep --grep='Something' # Commits whose message don't contain 'Something'

# Can have multiple patterns
git log --grep=<pattern1> --grep=<pattern2> # Commits whose message matches _any_ of the patterns
git log --all-match --grep=<pattern1> --grep=<pattern2> # Commits whose message matches _all_ of the patterns

source

Show author, author date, commit and commit date

git log --pretty=fuller

Output:

Author: Albert Vila Calvo <albert@gmail.com>
AuthorDate: Sun Jun 2 20:41:28 2024 +0200
Commit: Albert Vila Calvo <albert@gmail.com>
CommitDate: Sun Jun 2 20:42:45 2024 +0200

For the difference see:

show

https://git-scm.com/docs/git-show

git show
git show HEAD~1
git show HEAD@{5} # Use 'git reflog' to see the reflog
git show @~1 # @ is the most recent commit
git show -2 # Last 2 commits, both
git show --summary # Do not show file changes, only header info
git show 2.4.0 # A tag
git show branch:file # Eg 'git show main:README.md'
git show main@{yesterday}

Referencing previous commits

https://stackoverflow.com/questions/16062358/referring-to-the-previous-next-commit-in-git

https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

add

https://git-scm.com/docs/git-add

Stage all (new, modified and deleted): git add -A or git add --all

Stage new and modified files, but not deleted, in the current directory: git add .

Stage modified and deleted files, but not new files: git add -u

Add portions of file: git add -p see commands here

reset

https://git-scm.com/docs/git-reset

Unstage all staged files:

git reset

Reset parts of a file: git reset -p

stash

https://git-scm.com/docs/git-stash

Stash with message

git stash push -m "Some message"

Stash single file

git stash -- path/filename.txt
git stash -- path/filename1.txt path/filename2.txt
git stash -m "Some message" -- path/filename.txt

source

Stash only staged changes

https://git-scm.com/docs/git-stash#Documentation/git-stash.txt---staged

git stash push --staged
git stash push -S
git stash save --staged
git stash save -S

commit

Multiline commit in the terminal:

git commit -F - <<'EOF'
The title
The description
More description
EOF

Useful for scripts. You can also do:

git commit -m "The title" -m "The description" -m "More description"