Difference between revisions of "Git cheatsheet"
From Wasya Wiki
(→Clear staged files) |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Git cheatsheet - nifty git usage example. | Git cheatsheet - nifty git usage example. | ||
| + | |||
| + | === rebuild cache === | ||
| + | rm -f .git/index | ||
| + | git reset | ||
=== Clear staged files === | === Clear staged files === | ||
| − | + | git clean -df | |
| + | # remove untracked files | ||
| + | git clean -n | ||
| + | |||
| + | === clear stale remotes === | ||
| + | git remote prune origin | ||
=== download a specific tag === | === download a specific tag === | ||
| − | + | git checkout tags/<tag_name> | |
=== which commit does a blob belong to? === | === which commit does a blob belong to? === | ||
Latest revision as of 22:34, 14 March 2026
Git cheatsheet - nifty git usage example.
Contents
- 1 rebuild cache
- 2 Clear staged files
- 3 clear stale remotes
- 4 download a specific tag
- 5 which commit does a blob belong to?
- 6 show history for a file
- 7 show one commit
- 8 check branch
- 9 see most recent branch
- 10 What's the latest commit in the branch?
- 11 list conflicting files
- 12 set globalgitignore
- 13 Allow empty commit
- 14 revert
- 15 create an empty branch
rebuild cache
rm -f .git/index git reset
Clear staged files
git clean -df # remove untracked files git clean -n
clear stale remotes
git remote prune origin
download a specific tag
git checkout tags/<tag_name>
which commit does a blob belong to?
which.sh:
#!/bin/sh
obj_name="$1"
shift
git log "$@" --pretty=format:'%T %h %s' \
| while read tree commit subject ; do
if git ls-tree -r $tree | grep -q "$obj_name" ; then
echo $commit "$subject"
fi
done
which.sh <blob>
show history for a file
git log --follow -p -- file
show one commit
git diff <commit>~ <commit>
check branch
git branch | grep -e "^*" | cut -d' ' -f 2
see most recent branch
git branch -a --sort=-committerdate git branch -a --sort=committerdate
What's the latest commit in the branch?
git log -n 1 <branchname> --format=format:%ci
list conflicting files
git diff --name-only --diff-filter=U
set globalgitignore
git config --global core.excludesfile '~/.gitignore'
Allow empty commit
git commit --allow-empty -m 'trigger build'
revert
How do you do reverts, by the way? Do you create a reverse patch from a hash, and commit it on top to develop ?
I’m very careful, so I create a new branch and revert on that, then merge the branch in.
1. checkout and pull develop 2. cut new branch 3. git revert <commit hash> 4. push to origin and open PR
Exactly the same process as cherry-picks, just with revert instead of cherry-pick
create an empty branch
From: https://stackoverflow.com/questions/34100048/create-empty-branch-on-github
git switch --orphan <new branch> git commit --allow-empty -m "Initial commit on orphan branch" git push -u origin <new branch>