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):

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes (git add .gitmodules)
  3. Delete the relevant section from .git/config.
  4. Run git rm --cached path_to_submodule (no trailing slash).
  5. Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  6. Commit: git commit -m "Removed submodule"
  7. 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

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-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