Skip to content

Git-Repo erstellen und sichern

A git repository is a sort of enhanced Unix directory with the ability to track changes to every file and subdirectory.

- ein neues Verzeichnis erstellen
$ mkdir <dir>
- in das Verzeichnis wechseln
$ cd <dir>
- init (initialize) creates a special hidden directory called .git where Git stores the information it needs to track our project's changes
$ git init
- Falls das Verzeichnis nicht schon Inhalte hat:
- Um die Initialisierung des Repos fertigzustellen, müssen wir eine Änderung darin machen, da leere Verzeichnisse nicht initialisiert werden können:
$ git touch index.html
- Jetzt schauen wir uns das Ergebnis mal an:
$ git status
// On branch main - No commits yet - Untracked files:
// Untracked heißt: Git weiß noch nichts davon
- Wir fügen alles, was in dem Verzeichnis drin liegt, hinzu:
$ git add -A
// -A fügt untracked Verzeichnisse UND Dateien hinzu
- Weitere Optionen:
$ git add .
// alle Dateien im Verzeichnis
$ git add <name>
// eine bestimmte Datei
- Jetzt hat sich der Status geändert: von untracked zu unstaged (On branch main - No commits yet - Changes to be committed:)
- unstaged bedeutet: diese Datei ist ready, um ins Repo hinzugefügt zu werden
- untracked & unstaged => git add => staged => git commit => local repository => git push => remote repository
- make the changes part of the local repository - including a message indicating the purpose of the commit
- "m" (=message): "Initialize repository" (present tense, using imperative mood)- -a: all changes (but only in files that are already added)
$ git commit -am "m"
- combines the two commands
$ git add -A && git commit -m "m"
- to fix a typo in a commit-message (only possible if not yet pushed) with nano!!
$ git commit --amend
- shows a record of the commit (every commit is labeled with a hash - SHA)
$ git log
- shows the full diffs represented by each commit
$ git log -p
- shows the difference between the last commit and unstaged changes in the current project
$ git diff
- difference between staged changes and the previous version of the repo
$ git diff --staged
- shows the changes in the commit
$ git show <SHA>

Create a new repository at github (and set up the credentials HTTPS or SSH)

Section titled “Create a new repository at github (and set up the credentials HTTPS or SSH)”
- define github as the remote origin, the url is provided by github
$ git remote add origin <url>
- falls nicht eh schon bei der Initialisierung erledigt:
- ensures that the default branch is main
$ git branch -M main
- arranges to push the full repository to github (pushes branch to remote) -u sets GitHub as the upstream repository, which means we’ll be able to download any changes automatically when we run git pull
$ git push -u <loc> <branch>
$ git push -u origin main
- push to default remote
$ git push

Add a README.md $ touch README.md $ git add -A

Add a .gitignore $ touch .gitignore Contents: .file, *~, tmp/, … Datei(en) nachträglich aus .gitignore entfernen z.B. package.json beim ersten Mal mitschicken, dann aber ignorieren (weil z.B. .loc/.lvh.me unterschiedlich sind) nicht mehr benötigte Inhalte in die .gitignore aufnehmen, committen, pushen, dann: $ git rm -r - -cached . Cache leeren (komplett) oder $ git rm -r - -cached <file_name.ext> | Cache leeren (bestimmte Datei oder ganzen Ordner) – anschließend die Änderung committen

Make a branch: a self-contained copy of the project source which can be merged into another thereby incorporating the changes into the original branch $ git branch - create a branch $ git checkout - check out a branch $ git checkout -b - check out and create a branch $ git branch * about-page (= ) main - display all branches currently defined on the local machine - the asterisk * indicates the currently checked-out branch $ git branch -d - delete the branch $ git branch -D - delete an unmerged branch when checked out to another branch $ git diff branch-1 branch-2 - show diffs between branches $ git diff main - if the branch is left unspecified, Git automatically diffs against the current branch

Merge a branch: $ git checkout main - checks out the main branch ( option -b not needed, because the branch already exists) $ git merge - merge in the changes on the other branch (merge is the most common way to combine branches) $ git rebase - similar to merge, but forget it

Undo the last step: $ git log - list the latest steps $ git reset - resets the desired step - only local!!! (wenn noch nicht gepusht wurde) $ git revert - reverts the desired step

Recovering from errors: ERROR HAPPENED!!! $ git checkout -f 1.) WE WANT TO GET BACK TO THE STATE OF THE REPOSITORY AS OF THE MOST RECENT COMMIT (STATE KNOWN AS HEAD)

  • undo chances by passing the -f (= force) option to checkout, forces Git to checkout HEAD - but be careful, it wipes out all changes 2.) Use branches, if an error happens, commit the changes, but do not merge them, checkout to main and delete the branch. $ git checkout $ git checkout main 3.) Check out an earlier version of the repository. Use the SHA from the Git log => we are now in ‘detached HEAD’ state (inspect with $ git branch) => look around, eventually make changes, then check out toe main branch to apply them

Clone, push, pull: $ git clone - clones a repository (URL provided by GitHub) $ git pull - pull the remote changes

ERROR HAPPENED!!! https://stackoverflow.com/questions/2998832/git-pull-fails-unable-to-resolve-reference-unable-to-update-local-ref $ git restore -W . das hat scheinbar mal irgendwo geholfen - rausfinden, was das ist!!! $ git log —full-history — src/path/to/file.js https://twitter.com/JoshWComeau/status/1609320979213287424?s=20

# Note:
# To effectively apply the changes you will have
# to re-index the git index (if there are already
# commited files)
#
# $ git rm -r -f --cached .
# $ git add .
# $ git commit -m ".gitignore index rebuild"
#

Remove .git completly $ rm -rf .git