From ccadd256930252ad5457e7dcbda0164c25c54ff9 Mon Sep 17 00:00:00 2001 From: chawley Date: Thu, 27 Apr 2023 14:37:44 -0400 Subject: [PATCH] Migrated notes --- Fun with Find.md | 8 +--- git tricks.md | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 git tricks.md diff --git a/Fun with Find.md b/Fun with Find.md index 6dc5f23..91771ff 100644 --- a/Fun with Find.md +++ b/Fun with Find.md @@ -44,10 +44,4 @@ find /var/log/hbase -type f -iname "*log*" -mtime +360 -delete Replace `hbase` with the log directory you want to search. If you don't need to save the archives anywhere, skip step two. -[Reference](https://www.cyberciti.biz/faq/linux-unix-find-tar-files-into-tarball-command/) - ---- - - -Tags: - howto \ No newline at end of file +[Reference](https://www.cyberciti.biz/faq/linux-unix-find-tar-files-into-tarball-command/) \ No newline at end of file diff --git a/git tricks.md b/git tricks.md new file mode 100644 index 0000000..48d44ca --- /dev/null +++ b/git tricks.md @@ -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)