git Usage (14)

Commit History

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

git log

username@macchef3:~/testrepo$ git log
commit f89314cb7b752d60ae5d31677899addea104112d (HEAD -> main, test)
Author: michael.grossbach <michael.grossbach@hmtm-hannover.de>
Date:   Thu Jun 5 16:30:21 2025 +0200

    Edit file

commit 00a19a370f0b1ceecd930518b083ad3c76a13b15
Author: michael.grossbach <michael.grossbach@hmtm-hannover.de>
Date:   Thu Jun 5 16:30:21 2025 +0200

    Bump version

commit 0bb180397d80578a8dc81b80f5dac27883fa91b1
Author: michael.grossbach <michael.grossbach@hmtm-hannover.de>
Date:   Thu Jun 5 16:30:21 2025 +0200

    Initial commit
username@macchef3:~/testrepo$

The Repository keeps track of all the commit messages along with unique hashes for each commit

The log can be searched

  • git log | grep "Initial"
  • git log | grep -C5 "Bump version"

git Usage (15)

Remove Files from the Staging Area

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

  • touch dont_add.txt (create a file w/o content)
  • git add dont_add.txt
  • To remove, either
    • git rm --cached dont_add.txt to keep the file, or
    • git rm -f dont_add.txt to additionally delete the file from the pc

git Usage (16)

Delete Files from the Repo

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

Accidentally included a file in the repo?

  • touch accidentally_added
  • git add accidentally_added
  • git commit -m "Commit accidentally_added"

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

  • git rm accidentally_added
  • git commit -m "Remove accidentally_added from repo"

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

git Usage (17)

Restore a Deleted File (1)

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

Accidentally deleted a file?

  • touch accidentally_deleted
  • git add accidentally_deleted
  • git commit -m "Add file to be accidentally deleted"

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

  • rm accidentally_deleted (Delete file from computer)
  • ls

git Usage (18)

Restore a Deleted File (2)

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

  • git restore accidentally_deleted
  • ls
  • git commit -m "Restore deleted file"

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"

git Usage (19)

Revert to an Older Version (1)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"

Code stopped working? Accidentally deleted and commited?

Exercise:

  • Create a new file with the line 1st line in it
  • Include it in your repo
  • Add a second line to the file: 2nd line
  • echo "1st line" > test_file (Create file w/ content)
  • cat test_file (Print out file content)
  • git add test_file
  • git commit -m "Add file w/ '1st line'"

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"
   commit id: "1st line"

  • echo "2nd line" >> test_file (Add second line)
  • cat test_file

git Usage (20)

Revert to an Older Version (2)

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"
   commit id: "1st line"

  • Add the changes to your repo
  • Find the hash of the commit you need to revert to to get rid of 2nd line
  • Return your working directory to the status it was in at the time of that commit
  • git add test_file
  • git commit -m "Add '2nd line'"

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"
   commit id: "1st line"
   commit id: "2nd line"

  • git log
  • git revert c2079e56c4a13c2ddcbe77f64406630b749c6741 -m "Revert to version before '2nd line' was added"

gitGraph
   commit id: "Initial commit"
   commit id: "Bump version"
   branch test
   checkout test
   commit id: "Edit file"
   checkout main
   merge test tag: "First merge"
   commit id: "Accidentally added"
   commit id: "Remove"
   commit id: "Add file"
   commit id: "Restore file"
   commit id: "1st line"
   commit id: "2nd line"
   commit id: "Revert" type: REVERSE

git revert is a safe command to revert changes to an earlier stage, as it leaves the repository’s history intact

Cf.: git reset can be much more dangerous but is also a more powerful command (throws away all uncommited changes)

git Usage (21)

Keep Files From Being Versioned

Everything listed in the .gitignore file is kept out of the repo

nano .gitignore

  • my_webpage.html
  • *.html
  • .RData
  • .Rhistory
  • subfolder/
  • subdirectory/*.txt

Clone a Remote Repository

Remote Repositories with git (1)

Create and Clone a Remote Repository (1)

Remote Repositories with git (2)

Create and Clone a Remote Repository (2)

  • git clone git@gitlab.gwdg.de:michael.grossbach/my2ndtestrepo.git
  • cd my2ntestdrepo
  • git show

Remote Repositories with RStudio (1)

Clone a Remote Repository

cd ..
rm -rf my2ndtestrepo

https://xkcd.com/1597/

List of Useful Linux/Unix Terminal Commands

Command Optional Arguments Meaning Examples Remark
mkdir -p Make a directory mkdir testrepo Create the new directory ’testrepo in the current directory
mkdir -p new_dir/sub_dir Create ‘new_dir’ which contains ‘sub_dir’
cd <path> Change directory cd ~ Change to the Home directory
cd testrepo Change to directory ‘testrepo’
cd .. Change to the parent of the current directory
ls -a List files in current directory ls List files and directories in the current directory
ls -a As ls, but shows even invisible files and directories
rm -f, -r Remove (i.e., delete) files or directories rm my_file Remove (i.e., delete) the file ‘my_file’
rm -r my_dir Remove (recursively) directory ‘my_dir’ including all the files and possibly sub directories within
rm -f my_protected_file Force the removal of protected files

List of git Commands

Command Select Arguments Meaning Examples Remark
init -b <branch_name> Initialize a git repository git init Initialize a new Repository
git init -b main Initialize a new Repository with the default branch ‘main’
add Add one or more file(s) to the Staging Area (a.k.a. Index) git add Add the file ‘my_file’ to the Staging Area
git add my_dir/my_file Add the file ‘my_file’ in the directory ‘my_dir’ to the Staging Area
git add *.txt Add all files whose name ends with ‘.txt’ to the Staging Area
commit -m Commit one or more files and/or directories to the repository git commit “Initial commit” Commit everything in the Staging Area to the Repository
branch <branch_name> <commit_hash> git branch test 23e89360dc5b
checkout <branch_name> git branch test
merge <source_branch> git merge test
remote add <remote_name> <remote_address> git remote add origin git@gitlab.gwdg.de:/testrepo.git
pull
push

Thank You for Your Attention!