Add this to your user's ~/.gitconfig under [alias] to make it globally available:
lsch = "!f() { git diff --name-status -r "HEAD~$1"; }; f"
You can invoke this to retrieve all affected files in the last 7 commits like so:
$ git lsch 7
Instead of using -f or --force developers should use
--force-with-lease
Why? Because it checks the remote branch for changes which is absolutely a good idea. Let's imagine that James and Lisa are working on the same feature branch and Lisa has pushed a commit. James now rebases his local branch and is rejected when trying to push. Of course James thinks this is due to rebase and uses --force and would rewrite all Lisa's changes. If James had used --force-with-lease he would have received a warning that there are commits done by someone else. I don't see why anyone would use --force instead of --force-with-lease when pushing after a rebase.
git rebase -i $parent_hash
git rm file
git commit --amend
git rebase --continue
he Base Script
This bash "one-liner" displays all blob objects in the repository, sorted from smallest to largest.
For my sample repo, it ran about 100 times faster than the other ones found here.
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '/^blob/ {print substr($0,6)}' \
| sort --numeric-sort --key=2 \
| cut --complement --characters=13-40 \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
This will generate nice human-readable output like this:
...
0d99bb931299 530KiB path/to/some-image.jpg
2ba44098e28f 12MiB path/to/hires-image.png
bd1741ddce0d 63MiB path/to/some-video-1080p.mp4
Filtering
To achieve further filtering, insert any of the following lines before the sort line.
To exclude files that are present in HEAD, insert the following line:
| grep -vF "$(git ls-tree -r HEAD | awk '{print $3}')" \
To show only files exceeding given size (e.g. 1 MiB = 220 B), insert the following line:
| awk '$2 >= 2^20' \
Output for Computers
To generate output that's more suitable for further processing by computers, omit the last two lines of the base script. They do all the formatting. This will leave you with something like this:
...
0d99bb93129939b72069df14af0d0dbda7eb6dba 542455 path/to/some-image.jpg
2ba44098e28f8f66bac5e21210c2774085d2319b 12446815 path/to/hires-image.png
bd1741ddce0d07b72ccf69ed281e09bf8a2d0b2f 65183843 path/to/some-video-1080p.mp4
for branch in git branch -r | grep -v HEAD
;do echo -e git show --format="%ci %cr" $branch | head -n 1
\t$branch; done | sort -r
To clean old file in repo
Or "how to search text in git diff history"
git log -p -S "string"
cat prepare-commit-msg
NAME=$(git branch | grep '' | sed 's/ //'|cut -d'/' -f2|cut -d'-' -f1,2)
echo "$NAME $(cat "$1")" > "$1"
for dir in $(ls); do cp prepare-commit-msg $dir/.git/hooks/prepare-commit-msg; done
text=" [FIX] ()
🔄 [MOD] ()
✅5 [ADD] ()
🔀 [TEST] ()
[DOC] ()"
echo "$text $(cat "$1")" > "$1"
The easiest way would be to find the head commit of the branch as it was immediately before the rebase started in the reflog...
git reflog
and to reset the current branch to it (with the usual caveats about being absolutely sure before reseting with the --hard option).
Suppose the old commit was HEAD@{5} in the ref log:
git reset --hard HEAD@{5}
What will happen if I use git pull --rebase ?
git pull --rebase is roughly equivalent to
git fetch
git rebase origin/master
i.e. your remote changes (C) will be applied before the local changes (D), resulting in the following tree
A -- B -- C -- D
What will happen if I use git pull --ff-only ?
It will fail.
git pull --ff-only corresponds to
git fetch
git merge --ff-only origin/master
--ff-only applies the remote changes only if they can be fast-forwarded. From the man:
Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward
Since your local and remote branches have diverged, they cannot be resolved by a fast-forward and git pull --ff-only would fail.
I've edited my .vimrc to add fugitive plugin and commited it by using fugitive