Git commands

Add and commit in one command alias

git config --global alias.add-commit '!git add -A && git commit'

checkout remote branch and track it

git checkout -t origin/branch

Push branch and track

git push bitbucket development:development

Get current branch name

git symbolic-ref --short HEAD

Getting existing git branches to track remote branches:

git branch --set-upstream gh-pages-bitbucket bitbucket/gh-pages

Show latest branch ordered by updated time

git branch --sort=-committerdate

Git log

Show commits that are descendant of commit1 AND ancestors of commit2, in the same time

git log --reverse --ancestry-path commit1^..commit2
git log $(git merge-base HEAD branch)..branch  # show commits only on branch

See all the changed code

git log -p

Also with adding

git add -p # e to open editor

Git stash only unstaged changes

git stash --keep-index

Git checkout previous branch

git checkout -

When you want to undo some commands

git reflog

For work in progress you can use wip

git -m wip # one word does not need ""

Show file at specific revision

git show HEAD~4:index.html

Find difference of current file with some point

git diff 123123123 -- index.html

Find difference of all files and folders with meld diff tool

git difftool master --dir-diff

Find difference on this branch (from common ancestor) use three dots

git diff master...my_branch

Find bug using bisect on specific folder or file to jump half steps between

 git bisect start -- app/controllers/registration_controller.rb

If you need to push to two remote repositories, you can do it in one command

git remote add all [email protected]:duleorlovic/tips.git
git remote set-url  --add all [email protected]:duleorlovic_tips.git
git push all master --set-upstream
git push

To push to heroku without changes, try

git commit --allow-empty -m "empty commit"
git push heroku master

To sort authors by number of commits

git shortlog -e -s -n

Versioning SEMVER

Multiple accounts

If there is two accounts on bitbucket or github with different keys use .ssh/config file and put:

  IdentityFile ~/.ssh/id_rsa_new

  IdentityFile ~/.ssh/id_rsa

Note that you need to use git protocol url when seting remote (https protocol does not use this mappings). To change current url you can edit .git/config or use command git remote set-url origin [email protected]:company/repo.

To see which username (-T) and wich key (-v) is using:

You can see which keys currenlty is used with ssh-add -L. You can create new session with ssh-agent bash -c 'ssh-add ~/.ssh/id_rsa_my_company_account; git clone [email protected]:company/repo.git'. But new session will not stop ssh for offering other keys. To specificy which key to offer and disable others you can

ssh -v -i my_key [email protected]$PRODUCTION_IP

If you want, you can generate id_rsa_secure with password, so it will asked each time you use git command. Heroku uses keys from ~/.netrc which is created when you type heroku login (once is setup, it stays forever)

Note that if you have a lot of keys than all them will be offered and you can received error before it asks for password

debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:bXvs6rHfIUJaL2iCeI/7HzEYTXrNEgydI5ufuK7r0mE [email protected]
Received disconnect from port 22:2: Too many authentication failures
Disconnected from port 22

You can try to ssh localhost and than ssh [email protected]$PRODUCTION_IP in which case if will not offer all keys

debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/orlovic/.ssh/id_dsa
debug1: Trying private key: /home/orlovic/.ssh/id_ecdsa
debug1: Trying private key: /home/orlovic/.ssh/id_ed25519
debug1: Next authentication method: password

Solution is to remove some public keys or to specify key with -i option

Git commit

If you want to ignore/reignore some files that are tracked (gitignore does not work on tracked files). Note that when you change branch with changes to that files, error will be reported. Also note that git checkout . or git stash will be also applied to those hidden changes.

git update-index --assume-unchanged config/database.yml
# then to track changes again
git update-index --no-assume-unchanged config/database.yml

If you want to add folder to git structure but ignore all its content (like backup or tmp folder) you can do with three steps

mkdir tmp
echo '*' > tmp/.gitignore
git add tmp/.gitignore -f

If you want to skip history and download latest code

git clone --depth=1 your-repo-url

Clone all repositories from same user or organization, for example twitter

curl -s | ruby -rubygems -e 'require "json"; JSON.load( { |repo| %x[git clone #{repo["ssh_url"]} ]}'

Set author localy (config is for local folder only)

git config [email protected]
git config 'Dusan Orlovic'
git commit --amend --reset-author

To change author in all commits, you can rebase to first commit

# find first commit <sha1-of-root>
git rebase -i `git log --reverse | if read a commit ; then echo $commit ; fi`
# replace all `pick` with `e`, edit = use commit, but stop for amending
# repeat the following command
git commit --amend --reset-author --no-edit && git rebase --continue

To change on initial commit you can use rebase --onto HEAD

# git checkout <sha1-of-root>
git checkout `git log --reverse | if read a commit ; then echo $commit ; fi`
git commit --amend --reset-author --no-edit
git rebase --onto HEAD HEAD master

Apply patch

git diff > patchfile
patch -p1 < patchfile

Pick only changes from certain files instead of cherry pick

git show ffbd16fa313825e52b79ed9348b2a8ab963ec87a -- app/javascript/ | git apply -

Count files in staging

git diff --cached --numstat | wc -l

Rebase branches

 git pull --rebase # pull remote changes and rebase your local commits
 git rebase master # call this while you are on some feature branch

Interactivelly rebase pull request

git rebase -i HEAD~2 # rebase last two commits, or use origin/master
# pick : use same commit
# squash : meld with previous commit (on this list is previous)
# reword : change commit message
# edit : use commit but stop for amending

git fetch rails
git checkout guides_i18n
git rebase -i rails/master
git push origin guides_i18n --force-with-lease

Mark commit with a tag

git tag -l
git tag v1.5

# push tag is not automatically, you should do it manually
git push origin v1.5
# or push all tags
git push origin --tags

To find if given commit hash is incuded on some release (release is a tag)

git tag --contains <commit>

To remove tag

git tag -d v1.5

Add global gitignore for all projects

git config --global core.excludesfile ~/.gitignore

Clone for local or remote folders

git remote add local [email protected]:/home/orlovic/ruby/securiPi/.git

Big files

sudo apt install git-lfs
git lfs install
git lfs track *.mov

Bin folder added to path PATH=".git/safe/../../bin:$PATH", but only for project where you run

mkdir -p .git/safe

Submodule is used when you have other git repository within your project. Add submodule with git submodule add [email protected]:duleorlovic/myapp.git When someone clone your project, it needs to do two steps, initialize configuration file and fetch all data. Instead of those two commands you can pass option when someone is cloning your project --recurse-submodules or if it already cloned, it can use git submodule update --init --recursive (this is done every time you need to sync latest commited changes of submodules when pulling superproject updates)

git submodule init
git submodule update

to fetch upstream changes of submodules, you can navigate to each submodule folder and run get fetch and git merge, or you can do from main repo using

git submodule update --remote

to see differences in submodules you can use git diff --submodule (or you can set option git config --global diff.submodule log so you do not need to pass --submodule attribute every time). Similar for status, you can se git config status.submodulesummary 1 so git status will show changes that are done for submodules. To work inside submodules you need to checkout branch (initially git submodule update is leaving the subrepository in detached HEAD state). After you commit you need to push to submodule repo or you can do from superrepository with

git push --recurse-submodules=check


git help tutorial-2
git help everyday
git help workflows

Use pre commit hooks to check for something before commit happens, for example do not commit secret keys instead of DO_NOT_COMMIT tag. You can git commit --no-verify to ignore hooks.

# .git/hooks/pre-commit
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty

if test $(git diff --cached | grep -e $grep_reg | wc -l) != 0
  exec git diff --cached | grep -ne $grep_reg
  read -p "There are some occurrences of DO_NOT_COMMIT at your modification. Are you sure want to continue? (y/n)" yn
  echo $yn | grep ^[Yy]$
  if [ $? -eq 0 ]

Time tracking

  • is old unmaintained
  • Install linux brew
     /bin/bash -c "$(curl -fsSL"

    Than install gtm

    brew tap git-time-metric/gtm
    brew install gtm

    Use in your project

    gtm init
    gtm status
    gtm report -last-month -format summary
  • approximate based on git commit times
    # yesterday  or 2021-01-01 yesterday
    git-hours --since lastweek


Download and create links in your /usr/local/bin and run sudo glass install

glass init
glass status
git commit -am "Work"
git log -n 1 --show-notes=time-spent

To disable adding commit message use space (not empty)

cat >> /var/lib/timeglass/timeglass.json << HERE_DOC
  "mbu": "1m",
  "commit_message": " ",
  "auto_push": false


For you project you can set instructions for issues, look example

Very usefull github cheat sheet

Use labels:

  • devops for CI tasks
  • missing_requirements and ready for status of issue
  • feature, bug for type of issue

Use gitleaks to search history for secret keys, so you can remove them.

Hub Download release for linux arm 64 and run sudo ./install. To create alias, run and copy output hub alias -s

Hub add new commands:

  • git browse open current project github page in the default browser git browse -- issues to open issues. git browse user/repo to open user/repo
  • git create create this repository on GitHub and add GitHub as origin. After you create, you need to push
  • ci-status Show the CI status of a commit
  • compare Open a compare page on GitHub
  • fork Make a fork of a remote repository on GitHub and add as remote
  • issue List or create issues
  • pr Work with pull requests
  • pull-request Open a pull request on GitHub
  • release List or create releases

Github CLI