Git

2021-08-22

  • 1 Basics
    • 1.1 git checkout
      • 1.1.1 Revert changes made to your working copy
    • 1.2 git reset
      • 1.2.1 Revert changes made to the index
    • 1.3 git revert
      • 1.3.1 Revert a change that you have committed
    • 1.4 git clean
      • 1.4.1 Remove untracked files
      • 1.4.2 Remove untracked directories
    • 1.5 git diff
      • 1.5.1 View the staged changes
      • 1.5.2 Check changes between 2 commits
    • 1.6 git stash
      • 1.6.1 Stash a specific file
    • 1.7 git remote
      • 1.7.1 Delete a remove branch
  • 2 Advanced
    • 2.1 Configuration
      • 2.1.1 Line endings in Windows
    • 2.2 git merge
      • 2.2.1 fatal: refusing to merge unrelated histories
      • 2.2.2 Merge changes using vimdiff
    • 2.3 hooks
      • 2.3.1 Overview
      • 2.3.2 An example of pre-commit

1 Basics

1.1 git checkout

1.1.1 Revert changes made to your working copy

git checkout .

1.2 git reset

1.2.1 Revert changes made to the index

Revert the changes to be committed.

git reset

1.3 git revert

1.3.1 Revert a change that you have committed

git revert <commit 1> <commit 2>

1.4 git clean

1.4.1 Remove untracked files

git clean -f

1.4.2 Remove untracked directories

git clean -fd

1.5 git diff

1.5.1 View the staged changes

git diff --staged
git difftool --cached # cached is a synonym for staged

1.5.2 Check changes between 2 commits

git diff commit-id-1 commit-id-2

1.6 git stash

1.6.1 Stash a specific file

git stash push -m welcome_cart app/views/cart/welcome.thtml

1.7 git remote

1.7.1 Delete a remove branch

git push -d <remote_name> <branch_name>
git branch -d <branch_name>

2 Advanced

2.1 Configuration

2.1.1 Line endings in Windows

CR LF is in Windows, LF is in Unix and CR is in Mac.

git config --global core.autocrlf false to disable the conversion to CR LF in the Windows system.

2.2 git merge

2.2.1 fatal: refusing to merge unrelated histories

The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other’s existence and have mismatching commit histories).

A situation of this is: You have created a new repository, added a few commits to it, and now you are trying to pull from a remote repository that already has some commits of its own. Git will also throw the error in this case, since it has no idea how the two projects are related.

The error is resolved by toggling the allow-unrelated-histories switch.

git pull origin master --allow-unrelated-histories

2.2.2 Merge changes using vimdiff

git config --global diff.tool vimdiff
git config --global difftool.prompt false

Typing git difftool yields the expected behavior.

You can use the following basic commands to merge:

  • do - Get changes from other window into the current window.
  • dp - Put the changes from current window into the other window.
  • ]c - Jump to the next change.
  • [c - Jump to the previous change.
  • zo - Open folded lines.
  • zc - Close folded lines.
  • zr - Unfold both files completely.
  • zm - Fold both files completely.
  • Ctrl-w-w - change window.

If more than 2 buffers in the merge, use :ls to show the buffer number. Then use 2do to put the changes from the no. 2 buffer to this buffer.

2.3 hooks

2.3.1 Overview

Refer to Git Hooks | Atlassian Git Tutorial.

2.3.2 An example of pre-commit

The following example formats *.f90 in the changes to be commited. If the formatter raises warnings, set exit 1 which results in failed git commit. Then turn to resolve the warnings of formatting and try the commit again.

#!/bin/bash
# Regexp for grep to only choose some file extensions for formatting
# exts="\.\(f90\|ext2\)$"
exts="\.\(f90\)$"
# The formatter to use
formatter=`which fprettify`
# Check availability of the formatter
if [ -z "$formatter" ]
then
  1>&2 echo "$formatter not found. Pre-commit formatting will not be done."
  exit 0
fi
# Format staged files
for file in `git diff --cached --name-only --diff-filter=ACMR | grep $exts`
do
  echo "Formatting $file"
  message=$("$formatter" "$file" 2>&1)
  if [[ "$message" == *"WARNING"* ]]; then
    printf "$message\n"
    ret=1
  else
    git add "$file"
  fi
done
if [[ "$ret" == "1" ]]; then
    RED='\033[0;31m'
    NC='\033[0m' # No Color
    printf "${RED}Formatting is not finished successfully!${NC}\n"
    printf "${RED}Thus the changes are not committed!${NC}\n"
    exit 1
fi
# If no files left in index after formatting - fail
ret=0
if [ ! "`git diff --cached --name-only`" ]; then
  1>&2 echo "No files left after formatting"
  exit 1
fi
.
Created on 2021-08-22 with pandoc