Run this command periodically to clean loose objects and compress stuff.
git gc --aggressive
List all local branches sorted by last commit date, oldest last
for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
List all local branches sorted by last commit date, oldest first
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
List all branches that are fully merged
git branch -a --merged
Sync local git branches with remote
git remote prune origin
Delete local branches already merged into main (source)
git branch --merged main | grep -v "\* main" | xargs -n 1 git branch -d
Delete remote branch
git push origin --delete <branch_name>
Lightweight Tag
git tag v0.1.19
Pushing tags
git push origin --tags
Deleting tags
git tag -d v0.1.19
git push origin :refs/tags/v0.1.19
Reset to the previously fetched HEAD of the corresponding upstream branch
git reset --hard @{upstream}
Or the shorthand git reset --hard @{u}
.
I had accidentally committed and pushed changes to a branch. I wanted to undo that commit and return the branch to the previous commit, both locally and on git, while applying the changes to a new branch.
// Undo the commit
git reset --soft HEAD~1
// Unstage the added changes
git reset HEAD --
// Force-push the branch which will return it to the previous commit
// This effectively returns the branch to the previous commit
git push origin {{branch_name}} -f
// Now make a new branch and commit like normal
git checkout -b {{new_branch_name}}
git add -A
git commit -m "commit message"
Given this tree:
* 49c57f4 (origin/fix/crash, fix/crash)
| * 561be79 (origin/update/settings-urls, update/settings-urls)
| * …
| * 1207637
|/
* ee50e14 (HEAD, tag: v1.3.0.47, origin/BETA, update/settings-urls-beta, BETA)
* …
Let’s say you want to move the update/settings-urls
onto history of the BETA
branch.
git checkout -b update/settings-urls-beta
git cherry-pick 561be79
git push origin update/settings-urls-beta -u
Now the git history looks like this:
* 9342ea0 (HEAD, origin/update/settings-urls-beta, update/settings-urls-beta)
| * 49c57f4 (origin/fix/crash, fix/crash)
|/
| * 561be79 (origin/update/settings-urls, update/settings-urls)
| * …
| * 1207637
|/
* ee50e14 (tag: v1.3.0.47, origin/BETA, BETA)
* …
Then you can delete the old branch.
git branch -D update/settings-urls
# delete on github, then
git remote prune origin
* 9342ea0 (HEAD, origin/update/settings-urls-beta, update/settings-urls-beta)
| * 49c57f4 (origin/fix/crash, fix/crash)
|/
| * …
| * 1207637
|/
* ee50e14 (tag: v1.3.0.47, origin/BETA, BETA) Bump build to 47
* …
Stash a single file:
git stash -- filename.ext
Save a stash:
git stash save "stash name"
List stashes
git stash list
> stash@{0}: stash name
To apply a stash and remove it using stash@{n}
git stash pop stash@{0}
View a diff without a pager:
git --no-pager diff
View a diff between two branches:
git --no-pager diff older..newer
# example, using branch names:
git --no-pager diff main..HEAD
# example using commit hashes:
git --no-pager diff 9972d366..4e207bda
List all changes between two branches, commits, etc.
git log --pretty=format:"%h %s" 5d20289...DEV
where you can place any hash, tag, or branch name on either side of the ...