GitCookbook - henk52/knowledgesharing GitHub Wiki

Introduction

  • foxtrot merge

References

Vocabulary

  • branch: basically a reference?
  • commit id: is a checksum, that represents:
    • Content
    • Author
    • Date
    • Log
    • Previous commit id
    • Every commit is unique

    • Every commit id is unique

    • Commit never changes

  • fast-forward: when no merge is required, the references (head, branch) are simply moved to the newer commit object(id) (25:00)
  • origin/bugfix vs. origin bugfix
    • origin/bugfix: refers to the reference, by that name, in this repo
    • origin bugfix: refers to bugfix in the the origin repo
  • references:
    • branch
    • tag
    • commit id
    • remote branch
  • Staging area: aka index, aka cache

Installation

support multiple repo-users on same device

See: Multiple-github-accounts-on-the-same-computer

  • Generate an SSH-key ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa_MY_USER, follow the prompts and decide a name, e.g. id_rsa_doe_company.
  • Copy the SSH public-key to GitHub from ~/.ssh/id_rsa_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_rsa_doe_company.
  • Create the configuration file in: ~/.ssh/config with the content shown below:
  • Add your remote:
    • git remote add origin git@<github-doe-company>:<username>/<reponame>.git
    • or change using git remote set-url origin git@<github-doe-company>:<username>/<reponame>.git
  • To clone a new repo
    • git clone git@<github-doe-company>:<username>/<reponame>.git

~/.ssh/config

Host github-doe-company
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_doe_company

git push/clone hangs

Was it my ISP that was the problem???

configure git globally

  • git config --global -e
[push]
  default = simple

[alias]
    a = add
    # Amend the previous commit
    amend = commit --amend --reset-author --verbose
    # Commit and show diff in editor too
    ci = commit --verbose
    co = checkout
    up = pull --ff-only
    st = status
    # Show status for current sub-directory only
    s = status -- .
    # List all branches (locally and remote)
    br = branch -a
    # Remove branch
    rmbr = branch -d
    # Remvoe remote branch (locally)
    rmrbr = branch -d -r
    # Remvoe remote branch (locally)
    rmrbr = branch -d -r
    # Show the staged/cached/indexed changes
    idiff = diff --cached
    # Show info about tree objects
    ls = ls-tree HEAD
    # Show info about all tree objects
    list = ls-tree --full-tree -r HEAD
    pick = cherry-pick

    unstash = stash pop
    # Unapply the changes recorded in the most resent stash
    stash-unapply = !git stash show -p | git apply -R

    # Show summary info about a commit
    s = show --name-only --pretty=fuller

    # Create a Work In Progress commit (alternative to stash)
    wip = commit --all -m WIP

    # Show commits that have not been pushed yet
    # sc:  show commits (ahead)
    # scg: show commits (ahead) graph
    sc = log '@{upstream}'..HEAD --oneline --decorate
    scg = log '@{upstream}'..HEAD --oneline --decorate --graph
    # The same but with full commit messages
    # scl:  show commits (ahead) long
    # sclg: show commits (ahead) long graph
    scl = log '@{upstream}'..HEAD --decorate
    sclg = log '@{upstream}'..HEAD --decorate --graph

    # Single-line or short log of everything
    slog = log --graph --format=oneline --all
    # Single-line or short branch log
    blog = log --graph --format=oneline
    # Single-line or short log of everything with more info
    hist = log --pretty=format:\"%C(auto)%h %ad |%d %s [%Cblue%an%Creset]\" --graph --date=local --all
    # Single-line or short branch log with more info
    bhist = log --pretty=format:\"%C(auto)%h %ad |%d %s [%Cblue%an%Creset]\" --graph --date=local

    # prune orphan commits / housekeeping
    prune = gc --prune=now --aggressive
    oldest-ancestor = !zsh -c 'diff --old-line-format= --new-line-format= <(git rev-list --first-parent \"${1:-master}\") <(git rev-list --first-parent \"${2:-HEAD}\") | head -1' -

[color]
    # Show colour on terminals
    ui = auto

[log]
    # abbreviate commit hashes
    abbrevCommit = true
    # show tags, branches, etc. in logs
    decorate = short

[push]
    # The default as of git 2.0, and the safest for beginners :)
    default = simple

[rerere]
    # Record conflict resolutions
    enabled = true

[core]
    pager = less -i -S
    editor = vim
[init]
  defaultBranch = main

Command overview

  • add: add binary? to the repository.
  • branch: creates a branch with the given name
  • checkout: marks the given branch as the reference to advance/move with new commits.
    • move head to that commit id
    • and get the files related to the branch reference
  • commit: create a commit object of what has already been added.
    • and add labels: head and master(current branch?) to it
  • fetch
  • pull: actually a fetch + merge
  • push
  • rebase:
  • reset
  • reflog:
  • tag: is a reference, it doesn't move.

Overview

  • git branch -d PlaceInTime
    • delete the local branch
  • git branch -d -r origin/SOMETHING
    • delete the branch remot
  • git branch PlaceInTime eea3d0e
    • add the branch tag to the give id
  • git checkout master
    • Switch branch to master
  • git checkout PlaceInTime
    • Switch branch to PlaceInTime
  • git cherry-pick --abort
  • git clean -dxn
    • Show which files will be cleaned.
    • git clean -dxf
      • actually clean the files and dirs
  • git fetch
  • git info 45d42a3
    • get info on the commit id
  • git merge --abort
  • git pick 12c4ee6
  • git pull
  • git push
  • git push -f feature/ALPHA
  • git push -f origin feature/BETA
  • git push origin --delete DELTA
  • git rebase --abort
  • git rebase --continue
  • git rebase master
  • git show 45d42a3
  • git show --name-only 45d42a3
  • git slog
  • git stash
  • git stash drop
  • git stash show
  • git status
  • git unstash

checkout

  • -b: branch + checkout

commit

output:

[master abbd4b3] Second commit
 1 file changed, 2 insertions(+)
  • master: branch reference
  • abbd4b3: commit object?
  • 'Second commit': The commit text

merge

  • git merge master

    • merge master into the current branch
  • git merge master bugfix

    • merge master into bugfix
  • git reset --hard HEAD^

    • go back to the commit before the head, and check it out.

rebase

  • the-dark-side-of-the-force-push

  • git rebase

  • 1 git status

    • Ensure everything is committed and pushed
  • 2 git checkout master

  • 3 git pull --ff-only

  • 4 git checkout MY_BRANCH

  • 5 git rebase master MY_BRANCH

  • 6 git status

  • 7 git push -f

reset

Parameters:

  • --hard : also checkout to this

Cookbook

operations

Re-add executability to a file

  1. git ls-files --stage
  2. git update-index --chmod=+x FILE_NAME
  3. git ls-files --stage

rebase recipe

  • Thou shall not rebase a public branch

  • merge to bring in large topic branches back into the master branch.

  • merge any time the branch is already public.

  • rebase when you want to add minor commits that are in master to a topic branch

  • rebase whenever you want to move commits from one branch to another.

  • git rebase --continue

    • It seems you have to do this after having updated the files you wanted to update/merge.
  • git rebase --skip

    • use fully the file that the rebase branch(the one we are trying to rebase on)
  • git rebase --abort

    • As if the rebase was never started
  • git rebase --onto newbase upstream branch

    • newbase: The new branch that is going to be the base.
    • upstream: The current base branch for the feature-branch 'branch'
      • upstream of the feature branch.(Parent of the feature branch)
    • branch: The feature branch.
  • git reset --hard ORIG_HEAD

    • undo a rebase operation
  • git rebase -i

    • Interactive, we get a text file to change, where all affected commits are listed.
  • git pull --rebase

  • git pull --rebase=preserve

  • git pull --rebase=interactive

Possible process??

  1. git checkout master
  2. git refresh
  3. git up
  4. git checkout FEATURE
  5. git rebase master

Structure

.git directory

  • config
  • description
  • HEAD

.git/objectes directory

git has its own files system, stored in the 'object' directory.

Object types:

  • blop - Here git store all files of any types, both text and binary files.
  • tree - Store informations about directories
  • commit - store different version of the project
  • annotated tag - persistent text poitnres to specific commits

Git low-level commands

  • git hash-object - create new object in git structure

  • git cat-file - read git objects

  • git mktree -

  • echo "Hello, Git" | git hash-object --stdin -w

    • directory b7 and file .git/objects/b7/aec520dec0a7516c18eb4c68b64ae1eb9b5a5e
  • key in git equals the hash value

  • the hash of the file is used as the

    • folder name - two first bytes of the hash
    • filename - rest of the bytes of the hash
  • the hash function creates a fixed length hash from any variable length input

  • the same input will always create the same hash

  • git uses sha1 which is 160bit/40 hex decimal/80 bytes

  • max files in git: 2^160

  • two different files can produce the same hash; the probability is 1/320

⚠️ **GitHub.com Fallback** ⚠️