Git

Commit during dettached head

You commit your work and then you discovered that git head is dettached (typically because of commiting in git submodule).

git branch tempwork
git checkout master
git merge tempwork

Remove branches

  • Local branch: git branch -d the_local_branch
  • Remote branch: git push origin :the_remote_branch

Rename branches

  • Local branch: git branch -m <oldname> <newname>
  • Current local branch: git branch -m <newname>
  • Remove the remote: oldname and create the newname

Fixup commit

  • We have open merge request
  • Review with git log -p brings some necessary changes
  • Change the things
  • Do git commit as usual or …
  • Remember which commit brings the thing which should be changed
    git commit --fixup=COMMIT
    git rebase -i FIRST_NON_FIXUP_COMMIT_OR_PARENT --autosquash
    git push -f
    

Merge past commit together

git rebase -i THE_LATEST_COMMIT

Global configuration

File: ~/.gitconfig

[user]
    email = YOUREMAIL
    name = YOURNAME
    signingkey = YOURGPGKEY
[commit]
    gpgsign = True
[tag]
    gpgsign = True
    forceSignAnnotated = True
[push]
    default = current
[color]
    ui = true
[alias]
    st = status
    ci = commint
    co = checkout
    br = branch
    ll = log --oneline --graph --all --decorate
    wdiff = diff --word-diff
    merge-all = !~/.developer/git-merge-all.sh
    fix = "!git commit --fixup=HEAD"
    ri = "rebase -i --autosquash"

[diff "decrypt"]
    textconv = ansible-vault view
[core]
    excludesfile = ~/.gitignore
[includeIf "gitdir:~/Documents/work/"]
    path = ~/Documents/work/.gitconfig

Global gitignore

.*.swp
.*.swo
.*~

Delete tags

  • Local

    git push --delete origin TAGNAME
    
  • Remote

    git tag -d TAGNAME
    

Mirror to another git server

git remote add upstream git@ANOTHERGITSERVER:PATH
git fetch upstream
git push --force --tags --progress upstream HEAD:master

Remove submodule

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

Git watch

https://github.com/gitwatch/gitwatch

  • Install inotify-tools
  • Download your repository.
  • Copy file gitwatch.sh to your ~/.local/bin/gitwatch
  • Copy file gitwatch@.service to your ~/.config/systemd/user/gitwatch@.service
  • Change path in ExecStart to your actual home directory

    ExecStart=/home/bob/.local/bin/gitwatch $SCRIPT_ARGS
    
  • Initiate gitwatch for all required local repositories:

    systemctl --user --now enable gitwatch@$(systemd-escape "'-r git@github.com:bob/notes' /home/bob/Documents/notes").service
    
  • Check the status of the service:

    systemctl --user status gitwatch@$(systemd-escape "'-r git@github.com:bob/notes' /home/bob/Documents/notes").service