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
and GitLab, Pt. 3
Hochschule für Musik, Theater und Medien – Hannover
Institut für Musikphysiologie und Musiker-Medizin
2025-06-11
git
Usage (14)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)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
git rm --cached dont_add.txt
to keep the file, orgit rm -f dont_add.txt
to additionally delete the file from the pcgit
Usage (16)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)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)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)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:
1st line
in it2nd 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)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"
2nd line
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)Everything listed in the .gitignore file is kept out of the repo
nano .gitignore
git
(1)Clone with SSH
entrycd ~
git
(2)git clone git@gitlab.gwdg.de:michael.grossbach/my2ndtestrepo.git
cd my2ntestdrepo
git show
cd ..
rm -rf my2ndtestrepo
https://xkcd.com/1597/
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 |
git
CommandsCommand | 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: |
||
pull |
||||
push |
IMMM 2025