The git Workflow—A Worked Example

git Usage (1)

Create a Working Directory & a Local Repository

Open a Terminal

testuser@mac3:~$

mkdir testrepo

cd testrepo/

testuser@mac3:~/testrepo$

git init -b main

Leeres Git-Repository in /home/testuser/testrepo/.git/ initialisiert

git status

git Usage (2)

Create an ASCII1 File (1)

Open your favorite text editor

Windows MacOS Linux
notepad BBedit gedit
Notepad++ UltraEdit nano
VS Code VS Code emacs
Positron RStudio vim
Positron VS Code
RStudio
Positron

Don’t use MS Word, LibreOffice et al.

git Usage (3)

Create an ASCII File (2)

Type the following into the text editor

This is the first file of my project
Version: 1

and save the file as my_firstfile.txt in the Working Directory you just created for your project (~/testrepo); close the editor

Note

The file is now in your Working Directory.
This is the file you change if you edit it
(this sounds awfully trivial now but will become important later on).

git Usage (4)

Project Status

In the Terminal type:

git status

Auf Branch main

Noch keine Commits

Unversionierte Dateien:
  (benutzen Sie "git add <Datei>...", um die Änderungen zum Commit vorzumerken)
    my_firstfile.txt

nichts zum Commit vorgemerkt, aber es gibt unversionierte Dateien
(benutzen Sie "git add" zum Versionieren)

git status shows the difference between the Staging Area and the Repository

git Usage (5)

Add File to Index

In the Terminal, type:

git add my_firstfile.txt

Note

The file has been added to the Staging Area (Index) and git will detect any changes made to it from now on.

git status

git Usage (6)

Commit Staged File(s) to Repository

To commit all files in the Staging Area to the Repository, type:

git commit -m "Initial commit"

gitGraph
   commit id: "Initial commit"

Note

The Local Repository now contains a snapshot of the file along with a timestamp and the message “Initial commit”, describing the ‘reason’ for snapshooting.

The commit message will later make the identification of snapshots easier.

git status

git Usage (7)

Edit a File

gitGraph
   commit id: "Initial commit"

  • Re-open testrepo/my_firstfile.txt
  • Change Version: 1 to read Version: 2
  • Save, and exit editor
  • git status
  • git add my_firstfile.txt
  • git commit -m "Bump version _in file_"

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"

  • Use git status and git show to investigate repo

git Usage (8)

Intermediate Overview

Workflow-centered view

---
config:
  look: handDrawn
---
flowchart LR
    mkdir["Create Working Directory<br>mkdir ~/testrepo"] --> cd["Change into Working Directory<br>cd ~/testrepo"]
    cd --> gitinit["Create Local Repository<br>git init"]
    gitinit --> nano["Create/change file<br>nano my_firstfile.txt"]
    nano --> gitadd["Add to Staging Area (Index)<br>git add my_firstfile.txt"]
    gitadd --> nano
    gitadd --> gitcommit["Commit staged changes to Local Repository<br>git commit -m 'Initial commit'"]
    gitcommit --> nano

git-centered view (modeled after https://www.baeldung.com/ops/git-guide)

block-beta
    columns 2
        block
            columns 3
            local["Local Repository"]:3            
            wdt["Working Directory"] idxt["Staging Index"] lrt["Commit History"]
            space:2 gitclonedotId1((" "))
            gitaddId((" ")) -- "git add" --> gitadddotId((" ")) space
            space:3
            space gitadddotId --> gitcommitId((" ")) 
            gitcommitId -- "git commit" --> gitcommitdotId((" "))
            style local stroke-width:0px,fill:#fefbe0;
            style gitaddId stroke-width:0px,fill:#fefbe0;
            style gitcommitId stroke-width:0px,fill:#fefbe0;
            style gitcommitdotId stroke-width:0px,fill:#fefbe0;
            style gitadddotId stroke-width:0px,fill:#fefbe0;
            style gitclonedotId1 stroke-width:0px,fill:#fefbe0;
        end

Setup GitLab (1)

Setup GitLab (2)

  • Test connection:
    Either
    ssh -T git@gitlab.gwdg.de
    or
    ssh -T git@gitlab.com

git Usage (9)

‘Mirror’ a Local Repository Remotely

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"

  • In the Terminal, type
    git remote add origin git@gitlab.gwdg.de:<your-gitlab-handle>/testrepo.git1
  • git push --set-upstream origin main

git Usage (10)

Pull/Push Remote (Collaborate!) (1)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"

  • git pull (fetches & merges remote w/ local current branch)
  • Obtain the hash of commit ‘Bump version[…]’ from
    git show
  • git checkout -b test <commit-hash> (create new branch test from commit)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test

  • git status

git Usage (11)

Pull/Push Remote (Collaborate!) (2)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test

  • Add this line
    Edited file in branch test
    to the file my_firstfile.txt (change file contents in branch test)
  • git add my_firstfile.txt (add changes to index of branch test)
  • git commit -m "Edit file" (commit to branch test)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"

git Usage (12)

Pull/Push Remote (Collaborate!) (3)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"

  • Optional:
    git push origin test (provide new remote branch name)
  • git checkout main (return to main branch)
  • Open my_firstfile.txt—which version do you see?
  • git pull (sync with remote main branch)

git Usage (13)

Pull/Push Remote (Collaborate!) (4)

Merge a Branch with main

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"

  • git merge test (merge everything in branch test w/ branch main—implies a commit)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"

  • Check the contents of my_firstfile.txt!
  • git push (push everything online, i.e. publish)

End of Part 2

Thank you for your attention!