Catagorized Notes
Renamed notes to fit categories and be easier to find later: blog, config, howto
This commit is contained in:
105
blog - git tricks.md
Normal file
105
blog - git tricks.md
Normal file
@@ -0,0 +1,105 @@
|
||||
# Git Tricks
|
||||
|
||||
## Rename the master branch
|
||||
|
||||
NOTE: _This only works in your local repository BEFORE pushing to a remote_
|
||||
|
||||
```shell
|
||||
git branch -m master main
|
||||
```
|
||||
|
||||
* Reference: [How to rename the "master" branch to "main" in Git](https://www.git-tower.com/learn/git/faq/git-rename-master-to-main)
|
||||
|
||||
---
|
||||
|
||||
## mass rename
|
||||
|
||||
Example: For each .txt file, git mv it to itself, replacing the extension **.txt** with **.md**
|
||||
|
||||
```shell
|
||||
for i in $(find . -iname "*.txt"); do git mv "$i" "$(echo $i | rev | cut -d '.' -f 2- | rev).md"; done
|
||||
```
|
||||
|
||||
* Reference: [StackOverflow - git rename or move all files at once](http://stackoverflow.com/questions/9151920/git-rename-or-move-all-files-at-once)
|
||||
|
||||
---
|
||||
|
||||
## Git hooks
|
||||
|
||||
Git has a built-in powerful automation platform called [Git hooks](https://www.redhat.com/sysadmin/git-hooks?intcmp=7013a000002qLH8AAM). Git hooks allows you to execute scripts that will run when certain events happen in Git. You can write scripts in any scripting language you prefer that is available to your environment. There are 17 hooks available.
|
||||
|
||||
If you look in any repo's `.git/hooks` folder, you see a collection of `.sample` files. These are pre-written samples meant to get you started. Some of them contain some odd-looking code. Odd, perhaps, until you remember that these mainly were added to serve the original use case for Linux kernel work and were written by people living in a sea of Perl and Bash scripts. You can make the scripts do anything you want.
|
||||
|
||||
Here's an example of a hook I use for personal repos:
|
||||
|
||||
```bash
|
||||
#!/sur/bin/env bash
|
||||
curl https://icanhazdadjoke.com
|
||||
echo “”
|
||||
```
|
||||
|
||||
In this example, every time I run `git commit` but before the commit message is committed to my Git history, Git executes the script. (Thanks to Edward Thomson's [git-dad](https://www.edwardthomson.com/blog/reintroducing_git_dad.html) for the inspiration.)
|
||||
|
||||
Of course, you can do practical things, too, like checking for [hardcoded secrets before making a commit](https://github.com/GitGuardian/ggshield#pre-commit). To read more about Git Hooks and to find many, many example scripts, check out Matthew Hudson's fantastic [GitHooks.com](https://githooks.com/) site.
|
||||
|
||||
* Reference: [Git concepts in less than 10 minutes](https://opensource.com/article/22/11/git-concepts)
|
||||
|
||||
---
|
||||
|
||||
## List top 3 oldest commits on all branches
|
||||
```shell
|
||||
for branch in `git branch --format='%(refname:short)'`; do echo $branch; git log --format="%cs, %cr, %an, %ae" $branch | sort -r | head -3; done
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
```shell
|
||||
add-windows
|
||||
2023-01-26, 5 days ago, chawley (tfadmin), chawley@overdrive.com
|
||||
2023-01-26, 5 days ago, Charles Hawley, 56652268+chawley-od@users.noreply.github.com
|
||||
2022-12-13, 7 weeks ago, chawley (tfadmin), chawley@overdrive.com
|
||||
main
|
||||
2023-01-26, 5 days ago, chawley (tfadmin), chawley@overdrive.com
|
||||
2023-01-26, 5 days ago, Charles Hawley, 56652268+chawley-od@users.noreply.github.com
|
||||
2022-12-13, 7 weeks ago, chawley (tfadmin), chawley@overdrive.com
|
||||
```
|
||||
|
||||
* Reference: [Git branch list without asterisk](https://salferrarello.com/git-branch-list-without-asterisk/)
|
||||
* Reference: [git\-log \- Show commit logs](https://git-scm.com/docs/git-log)
|
||||
|
||||
---
|
||||
|
||||
## git merge dry-run
|
||||
|
||||
As noted previously, pass in the `--no-commit` flag, but to avoid a fast-forward commit, also pass in `--no-ff`, like so:
|
||||
|
||||
```shell
|
||||
git merge --no-commit --no-ff $BRANCH
|
||||
```
|
||||
|
||||
To examine the staged changes:
|
||||
|
||||
```shell
|
||||
git diff --cached
|
||||
```
|
||||
|
||||
And you can undo the merge, even if it is a fast-forward merge:
|
||||
|
||||
```shell
|
||||
git merge --abort
|
||||
```
|
||||
|
||||
### make it a shell function
|
||||
Add to `.zsh_functions` or `.bash_functions`
|
||||
|
||||
```shell
|
||||
# git testmerge
|
||||
testmerge() {
|
||||
git merge --no-commit --no-ff $1
|
||||
git diff --cached
|
||||
git merge --abort
|
||||
}
|
||||
```
|
||||
### Reference
|
||||
|
||||
* [Is there a git-merge --dry-run option](https://stackoverflow.com/questions/501407/is-there-a-git-merge-dry-run-option)
|
||||
Reference in New Issue
Block a user