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

Getting existing git branches to track remote branches:

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

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 bug using bisect on specific folder or file

 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 git@github.com:duleorlovic/tips.git
git remote set-url  --add all git@heroku.com: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

Versioning SEMVER

Multiple accounts

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

Host newbitbucket.org
  Hostname bitbucket.org
  IdentityFile ~/.ssh/id_rsa_new

Host bitbucket.org
  Hostname bitbucket.org
  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 git@newbitbucket.org:company/repo.

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

ssh -T git@bitbucket.org
ssh -v git@secure.bitbucket.org

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 git@github.com:company/repo.git'

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)

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 and change author you can

git clone --depth=1 your-repo-url
git commit --amend --author "New Author Name <email@address.com>" --no-edit

Set author localy (config is for local folder only)

git config user.email myemail@asd.asd
git config user.name my-name
git commit --amend --reset-author

Apply patch

git diff > patchfile
patch -p1 < patchfile

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

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

git tag --contains <commit>

Add global gitignore for all projects

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

Clone for local or remote folders

git remote add local orlovic@192.168.2.103:/home/orlovic/ruby/securiPi/.git

Tutorials

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.

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

grep_reg='-.*DO_NOT_COMMIT'
# CHECK
if test $(git diff --cached | grep -e $grep_reg | wc -l) != 0
then 
  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 ]
  then
    exit 0; #THE USER WANTS TO CONTINUE
  else
    exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
  fi
fi

Time tracking

  • https://github.com/laughedelic/gtm is old unmaintained
  • https://github.com/git-time-metric/gtm

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

Glass

https://github.com/timeglass/glass

Download and create links in your /usr/local/bin and run sudo glass install https://github.com/timeglass/glass/blob/master/docs/manual_installation.md

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

Github

For you project you can set instructions for issues, look example https://github.com/arsduo/koala/issues/new

Very usefull github cheat sheet https://github.com/tiimgreen/github-cheat-sheet#github-talks

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

https://hub.github.com/ Download release for linux arm 64 https://github.com/github/hub/releases 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