Git Cheat Sheet / Cheatsheet

  • “origin” is just an alias that points to your GitHub Repository
  • “upstream” is a common name for a repository that was the source of a “fork”

Hate git?

10 things I hate about Git

Tutorials

How to get started with GIT and work with GIT Remote Repo
Git Explained: For Beginners
git – Der einfache Einstieg
Git for Computer Scientists

Ralf Ebert

Commit-Hashes und Historie
Änderungen an Working Tree und Index verwerfen

Hands-On

Git Hands-On Workshop
Git hands-on session with command line
Advanced Git and GitHub Skills

First Time

GitHub For Beginners: Don’t Get Scared, Get Started
Git basics – a general workflow

How can I change the author name / email of a commit?
Getting Started – First-Time Git Setup

$ git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -nosession"
$ git config --list
$ git config --global user.name "Andreas Bachmann"
$ git config --global user.email bachi@te-clan.ch
$ git config user.name "Andreas Bachmann"
$ git config user.email bachi@te-clan.ch

Staging Area (Ladedock)

Der Staging Bereich
What ‘stage’ means in git source control

Adding empty directories

Short answer – you can’t.
The design of the Git staging area only accounts for files, as described in the Git FAQ, and other books like Pro Git.
Git empty directories FAQ – How to add an empty directory to Git

Adding a remote

github: Adding a remote
Adding a remote to existing git repo
2.5 Git Basics – Working with Remotes
Git Basics – Working with Remotes

$ git remote -v
origin	/home/andreas/src/ZZZ (fetch)
origin	/home/andreas/src/ZZZ (push)

$ git remote remove origin
$ git remote -v
$ git remote add origin https://XXX.ch/svn/ZZZ/trunk
$ git remote -v
origin	https://XXX.ch/svn/ZZZ/trunk (fetch)
origin	https://XXX.ch/svn/ZZ/trunk (push)

$ git svn fetch --fetch-all

Branching and Merging

$ git branch -a
Lokal (grün)
* master
Extern (rot)
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/psram_malloc
  remotes/origin/master
  remotes/origin/release/v2.0
  remotes/origin/release/v2.1

$ git remote -v
origin	https://github.com/espressif/esp-idf.git (fetch)
origin	https://github.com/espressif/esp-idf.git (push)
  • master Branch (Zeiger)
  • origin Repository
  • HEAD der “aktuelle Branch”

Externe Branches

Externe (Remote) Branches sind Referenzen auf den Zustand der Branches in Deinen externen Repositorys. Es sind lokale Branches die Du nicht verändern kannst, sie werden automatisch verändert wann immer Du eine Netzwerkoperation durchführst. Externe Branches verhalten sich wie Lesezeichen, um Dich daran zu erinnern an welcher Position sich die Branches in Deinen externen Repositories befanden, als Du Dich zuletzt mit ihnen verbunden hattest.

Hochladen

Deine lokalen Zweige (Branches) werden nicht automatisch mit den Remote-Servern synchronisiert wenn Du etwas änderst – Du musst die zu veröffentlichenden Branches explizit hochladen (pushen).

  git push (remote) (branch)
$ git push origin serverfix

Hierbei handelt es sich um eine Abkürzung. Git erweitert die serverfix-Branchbezeichnung automatisch zu refs/heads/serverfix:refs/heads/serverfix, was soviel bedeutet wie “Nimm meinen lokalen serverfix-Branch und aktualisiere damit den serverfix-Branch auf meinem externen Server”
Du kannst auch git push origin serverfix:serverfix ausführen, was das gleiche bewirkt – es bedeutet “Nimm meinen serverfix und mach ihn zum externen serverfix”

$ git fetch origin

Es ist wichtig festzuhalten, dass Du mit Abrufen eines neuen externen Branches nicht automatisch eine lokale, bearbeitbare Kopie derselben erhältst. Mit anderen Worten, in diesem Fall bekommst Du keinen neuen serverfix-Branch – sondern nur einen origin/serverfix-Zeiger den Du nicht verändern kannst.

Wenn Du Deine eigene Arbeitskopie des serverfix-Branches erstellen möchtest, dann kannst Du diesen auf Grundlage des externen Zweiges erstellen:

$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

Dies erstellt Dir einen lokalen bearbeitbaren Branch mit der Grundlage des origin/serverfix-Zweiges.

Tracking Branches

Das Auschecken eines lokalen Branches von einem Remote-Branch erzeugt automatisch einen sogenannten Tracking-Branch. Tracking Branches sind lokale Branches mit einer direkten Beziehung zu dem Remote-Zweig. Wenn Du Dich in einem Tracking-Branch befindest und git push eingibst, weiß Git automatisch zu welchem Server und Repository es pushen soll. Ebenso führt git pull in einem dieser Branches dazu, dass alle entfernten Referenzen gefetched und automatisch in den Zweig gemerged werden.

Git Branching – Externe Branches
Git housekeeping: remove “redundant” branches from the remote and your local repositories

A successful Git branching model
Gitflow Workflow
Git Workflows That Work
What Git branching models work for you?

3.2 Git Branching – Basic Branching and Merging
What is git HEAD, exactly?

3.5 Git Branching – Remote Branches

Tilde, Hat, At

  • HEAD~2
  • HEAD^2
  • HEAD@{2}
  • HEAD~~
  • HEAD^^

HEAD~ vs HEAD^ vs HEAD@{} also known as tilde vs caret vs at sign
What’s the difference between HEAD^ and HEAD~ in Git?
git-rev-parse – Pick out and massage parameters

Undo

Undoing Changes

$ git checkout <file>
or
$ git reset --hard

Remove latest commit

$ git reset --soft HEAD~1

Mistakenly reset

$ git reset HEAD^4

$ git log --oneline
c5cada1 (HEAD -> master) move code to object-oriented design
cbac193 add splash screen
12a215f add FotoFinder logo
3ddcbea initial commit

$ git reflog
c5cada1 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
c67578c HEAD@{1}: reset: moving to HEAD^
6fac355 (origin/master, origin/HEAD) HEAD@{2}: reset: moving to HEAD~
952985d HEAD@{3}: reset: moving to HEAD~
e2c44d1 HEAD@{4}: revert: Revert "Communication module: receive with timeout"
952985d HEAD@{5}: commit: Communication module: receive with timeout
6fac355 (origin/master, origin/HEAD) HEAD@{6}: commit: splashscreen state-machine
c67578c HEAD@{7}: commit: refactor code design
c5cada1 (HEAD -> master) HEAD@{8}: commit: move code to object-oriented design
cbac193 HEAD@{9}: commit: add splash screen
12a215f HEAD@{10}: commit: add FotoFinder logo
3ddcbea HEAD@{11}: reset: moving to HEAD
3ddcbea HEAD@{12}: clone: from https://github.zhaw.ch/dermolockin/DermaIrEsp32Display.git

$ git checkout --force 952985d .

Commit without log?!

Pushing amended commits

How to push to repo after doing ‘git commit –amend’
Git: pushing amended commits
How do I push amended commit to the remote git repo?

$ git commit [...]
$ git push

$ git commit --amend --reset-author
or
$ git commit --amend --author="Andreas Bachmann <bachi@te-clan.ch"

$ git push
To git@github.com:user/repo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:user/repo.git'

No other commits are pushed:

$ git push --force
Counting objects: 1, done.
Writing objects: 100% (1/1), 207 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:te-bachi/dns-amplification-defender.git
 + 4f3ec7c...de70182 master -> master (forced update)

Commit only modified

$ git add -u
or
$ git commit -a

git add only modified changes and ignore untracked files
Difference between “git add -A” and “git add .”

Color

Avoid escape characters in GIT
Can less retain colored output?
git diff displays colors incorrectly

ESC[1mdiff --git a/include/packet/ethernet_header.h b/include/packet/ethernet_header.hESC[m
ESC[1mindex 9239f0b..a8f6553 100644ESC[m
ESC[1m--- a/include/packet/ethernet_header.hESC[m
ESC[1m+++ b/include/packet/ethernet_header.hESC[m
ESC[36m@@ -53,6 +53,7 @@ESC[m ESC[mstruct _ethernet_header_t {ESC[m
$ git config --global core.pager "more -r"

Von Revision zu Revision springen

How to move HEAD back to a previous location? (Detached head)
How do I move forward and backward between commits in git?
How to revert Git repository to a previous commit?

$ git checkout HEAD~1 .
$ git checkout HEAD~1
==> HEAD ist nun eins zurück

$ git checkout master
==> HEAD wieder auf master

Log / Diff

Get the short git version hash

abbreviated SHA-1 checksum

$ git log --abbrev-commit doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
commit 52570bc
Author: Giaele Quadri <quadrgia@students.zhaw.ch>
Date:   Mon Feb 26 20:52:27 2018 +0100
[...]

full checksum

$ git log doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
commit 52570bc4ee5f67de56143fa616369ed812613a6d
Author: Giaele Quadri <quadrgia@students.zhaw.ch>
Date:   Mon Feb 26 20:52:27 2018 +0100
[...]

diff

$ git diff 52570bc ea5102a doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
diff --git a/doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex b/doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
index e6742cb..dc5a2b9 100644
--- a/doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
+++ b/doc/Bericht_BA18_DermoInspectMini/BerichtBA.tex
[...]

Revision Log

$ git reflog
c9daee7 HEAD@{0}: checkout: moving from fdfcceea0650e3a96e83175eb53d59360f63a779 to HEAD@{1}
fdfccee HEAD@{1}: checkout: moving from c9daee7cd6036fab5826ddb3f19c3c7669dca041 to HEAD@{1}
c9daee7 HEAD@{2}: checkout: moving from fdfcceea0650e3a96e83175eb53d59360f63a779 to HEAD@{1}
fdfccee HEAD@{3}: checkout: moving from c9daee7cd6036fab5826ddb3f19c3c7669dca041 to HEAD@{1}
c9daee7 HEAD@{4}: checkout: moving from fdfcceea0650e3a96e83175eb53d59360f63a779 to HEAD@{1}
fdfccee HEAD@{5}: checkout: moving from c9daee7cd6036fab5826ddb3f19c3c7669dca041 to HEAD~1
c9daee7 HEAD@{6}: checkout: moving from 724fe825b81cbfc26448ae38276ab72aff5848b4 to HEAD~1
724fe82 HEAD@{7}: checkout: moving from master to HEAD~1
80a7302 HEAD@{8}: commit: Move logic to PhysioTrain.cpp
724fe82 HEAD@{9}: checkout: moving from c9daee7cd6036fab5826ddb3f19c3c7669dca041 to master
c9daee7 HEAD@{10}: checkout: moving from master to HEAD~1
724fe82 HEAD@{11}: commit: Move logic to PhysioTrain.cpp
c9daee7 HEAD@{12}: commit: PES4: communication between the two IMUs and the MCU
fdfccee HEAD@{13}: commit: Add header comment
aef832e HEAD@{14}: commit: Log out acceleration and quaternion
2ccaf0c HEAD@{15}: commit: Initial PhysioTrain
62bc3fa HEAD@{16}: clone: from git@github.com:te-bachi/PhysioTrain.git

Glossary

master “Standard”-Branch
origin “Standard”-Remote Repository
index Gleich wie staging area
stage / staging area Ladedock, wo du entscheidest welche Änderungen verschifft (commited) werden.
HEAD Der aktuell benutzte Branch

Subversion

Siehe auch Subversion -> Migration

8.1 Git und andere Versionsverwaltungen – Git und Subversion

$ git svn clone https://server/project project
$ cd project
$ git commit -m "..."
$ git svn dcommit

Robby on Rails – git-svn is a gateway drug – Working with Subversion branches
How do I make git-svn use a particular svn branch as the remote repository?
How to switch svn branches using git-svn?

Subversion Remote Branch

How do I tell git-svn about a remote branch created after I fetched the repo?

$ git config --add svn-remote.newbranch.url https://svn/path_to_newbranch/
$ git config --add svn-remote.newbranch.fetch :refs/remotes/newbranch
$ git svn fetch newbranch [-r<rev>]
$ git checkout -b local-newbranch -t newbranch
$ git svn rebase newbranch

I don’t handle protocol ‘https’

git: fatal: I don’t handle protocol ‘​​http’

$ git clone https://github.com/xxx/repo.git
Cloning into 'repo'...
fatal: I don't handle protocol 'https'

$ git --version
git version 2.4.5

I copied and pasted the whole line git clone ….
The character between clone and http://… looks like a space, but it is a special Unicode character!
After removing this empty character, and entering a real space, it worked!

$ git clone https://github.com/xxx/repo.git
Cloning into 'repo'...
remote: Counting objects: 146, done.
Receiving objects:  87remote: Total 146 (delta 0), reused 0 (delta 0), pack-reused 146
Receiving objects: 100% (146/146), 74.51 KiB | 0 bytes/s, done.
Resolving deltas: 100% (76/76), done.
Checking connectivity... done.

Leave a Reply

Your email address will not be published. Required fields are marked *