Notes about easy-to-forget git features 😉.
Remote handling
git remote -v # link remotes
git remote show origin -n # show remote without querying status (-n)
git remote add github <remote-url> # e.g. git@github.com:<User>/<Repo>
git remote set-url origin <remote-url>
Find branches containing commit
git branch -a --contains <commit-hash>
Checkout last commit before merge
git reflog
git checkout HEAD@{refloghash}
Cloning tricks
# Clone without history
git clone --depth 1 <repository>
# Copy subfolder without whole repo (github)
svn export ${repo}/trunk/${directory}
Remove submodules (source):
- Delete the relevant section from the .gitmodules file.
- Stage the .gitmodules changes (
git add .gitmodules) - Delete the relevant section from .git/config.
- Run
git rm --cached path_to_submodule(no trailing slash). - Run
rm -rf .git/modules/path_to_submodule(no trailing slash). - Commit:
git commit -m "Removed submodule" - Delete the now untracked submodule files:
rm -rf path_to_submodule
Deleting branches
Delete remote branches
git push origin :branch-name
Delete all branches except master and release, uncomment ‘#’ if you’re sure:
git branch --remote | grep -vE '(master|release)$' | awk -F"/" '{for (i=2; i<NF ;i++) printf $i"/";printf $NF"\n"}' # | xargs -I {} git push origin :{}
Prune tracking branches
- prune tranching branches when there in no on e on remote https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote
List/remove merged remote branches
git branch -r --merged | grep -v master | sed 's/origin\//:/' # | xargs -n 1 git push origin
Rebase onto rebased branch
https://stackoverflow.com/a/31882353
git rebase --onto <TARGET> <OLD TARGET> <Your head>
Git related utils
- git-quick-stats
- LOC (line of code) counters:
Git config per project
- you can configure things in git globally (
$HOME/.gitconfig) or in specific repo. - repo specific configuration is done in
$PROJ/.git/config
Comments