Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Last active November 3, 2021 15:56
Show Gist options
  • Save gwsu2008/3a4983510ad6bdabfde2db755b8d22c3 to your computer and use it in GitHub Desktop.
Save gwsu2008/3a4983510ad6bdabfde2db755b8d22c3 to your computer and use it in GitHub Desktop.
Useful git command
#!/bin/bash +x
git archive --format=tar --remote=ssh://git@bitbucket.io:7999/test/tools.git HEAD:cloud_gen | gzip >tools-1.4.0.tar.gz
git archive --format=tar --remote=ssh://git@bitbucket.io:7999/test/mobile-app.git --prefix=scripts/ HEAD:mobile-app/Scripts/Build-Phases | gzip >test.gz
# lets you add currently staged changes to your last commit.
# Exclude the --no-edit option and Git will prompt if you wish to change commit message.
# This command comes in very handy for those cases when you find out that you need to
# make a little change after you’ve already committed your changes.
git commit --amend --no-edit
# This is a powerful command that lets us do loads of stuff with commits that are present in our branch.
git rebase -i origin/master
# lets us delete local branches that were merged into master.
git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d
Let’s check what this command does :
git branch --merged master : This command lists the branches that were merged to master.
grep -v '^[ *]*master$' : This part of the command excludes master branch from getting deleted.
I can’t think of a scenario where you’d want to delete master branch.
Well, except when your main branch is not master, then maybe you’d want to delete master.
In that case, don’t forget to replace `master` with your main branch’s name in the above command.
xargs git branch -d : This is what actually deletes your branches.
# save current uncommited changes and restore them on a branch or another commit
git stash
git checkout feature1
git stash pop
git bisect start : Telling Git I mean business.
git bisect bad HEAD : Letting Git know that the bug is alive on HEAD.
git bisect good HEAD~10 : Marking the commit where I’m sure that the bug wasn’t there, as good.
Now, git will pick a commit between the two I provided above and ask me whether the commit is “good” or “bad”.
It will keep on going and in no time, I can find the culprit. Now, we got to blame someone for this, right?
# fix commit not at the same level between dev and master
git fetch && git pull
git checkout dev
git checkout master
git diff dev..master
git diff master..dev
git cherry -v master dev
git cherry -v dev master
git branch && git log -n1 # get master latest commit
git branch -f dev bf3d974c1865dccbd33fdf6c163f859629e4c319 # master commit
git push --force origin dev:dev
# show deleted files from git history
git --no-pager log --diff-filter=D --summary | grep delete
# Show change file on a commit
git --no-pager show --pretty="" --name-only 3cb6cce72181c159507af66267a9e9e7f5f46f60
# remove file and modify history
git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch "Application/"' --prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git push --all --prune --force
git push origin --force --tags
# Mirroring a repository
git clone --bare https://github.com/exampleuser/old-repository.git
cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git
cd ..
rm -rf old-repository.git
# Mirroring a repository that contains Git Large File Storage objects
git clone --bare https://github.com/exampleuser/old-repository.git
cd old-repository.git
git lfs fetch --all
git push --mirror https://github.com/exampleuser/new-repository.git
git lfs push --all https://github.com/exampleuser/new-repository.git
cd ..
rm -rf old-repository.git
# Mirroring a repository in another location
git clone --mirror https://github.com/exampleuser/repository-to-mirror.git
cd repository-to-mirror.git
git remote set-url --push origin https://github.com/exampleuser/mirrored
git fetch -p origin
git push --mirror
# Find large files in git
git verify-pack -v .git/objects/pack/pack-b831e1cc06ecd857ef3960575c8eece116f97b9b.idx | sort -k 3 -n | | tail -10 # last 10 big file
git rev-list --objects --all | grep 67cd37961 # get the file name
# List remote tag without checkout
git ls-remote --tags git://github.com/git/git.git
# rename a remote branch
git push origin :old-name new-name
# get files changed in a particular commit
git --no-pager whatchanged -m -n 1 -p commit_id --pretty=format: --name-only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment