Git Hacks
Published on Jan 10, 2020 by Sachin.
The following are few1 Git configs I like and use the most2.
Ignoring files System-wide
This ensures the file listed in ~/.gitignore_global
are not listed when I
execute git status
even for the fresh project with no local .gitignore
file configured.
1: # Content of ~/.gitconfig 2: [core] 3: excludeFile = ~/.gitignore_global
Content of ~/.gitignore_global
1: # .gitignore_global 2: # ignore files and dirs globally 3: 4: *~ 5: etc/ 6: #*.rst 7: *.pyc 8: backup.py 9: *.log 10: 11: # ignore APK binary 12: *.apk 13: bin/ 14: gen/ 15: .settings/ 16: local.properties 17: *.img 18: 19: 20: # tar-balls system 21: *.gz 22: *.tar 23: *.bz2 24: *.pdf
Show staged files
1: # Content of ~/.gitconfig 2: [alias] 3: dfc = diff --cached
Example:
git dfc
Show ignored file(s)
If I want to list all the ignored files just in-case I need to add the file
to git
deliberately.
1: # Content of ~/.gitconfig 2: [alias] 3: ign = ls-files -o -i --exclude-standard
Examples:
1: # Create a fresh project: foo 2: $ git init foo 3: 4: # Copy a file which is ignored 5: $ cp ~/Downloads/w_lina09.pdf . 6: 7: # Check the status of the project 8: $ git status 9: On branch master 10: 11: No commits yet 12: 13: nothing to commit (create/copy files and use "git add" to track) 14: 15: # but using `git ign`, the PDF file is visible 16: $ git ign 17: w_lina09.pdf
Fetch the Pull Request/Merge Request
This is really helpful for local code reviews.
1: # Content of ~/.gitconfig 2: [alias] 3: pr = !sh -c 'git fetch $1 pull/$2/head:$3 && git checkout $3' - 4: mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
Examples:
# GitHub # Syntax: git pr <REMOTE> <PR_ID> <BRANCH> git pr upstream 2384 findmnt_propagation # GitLab # Syntax: git mr <REMOTE> <MR_ID> git mr upstream 2127
Add modified files
1: # Content of ~/.gitconfig 2: [alias] 3: u = add -u
Example:
git u
Footnotes:
2
I use Magit to manage git projects. To know more about Magit please visit https://opensource.com/article/19/1/how-use-magit