
This blog entry gives a quick insight into the Git basics, is aimed at beginners and should make it easier to work and get started. The very first steps are shown, the most important functions are presented, as well as the advantages and disadvantages are considered and a comparison to SVN is made. For advanced users, further entries will follow, which take up various focal points, advanced functions and graphical GUI clients.
What is Git?
Git is a distributed versioning system for managing files. Typical for Git are a non-linear development and a decentralized storage. There is a central repository, but each user has a local copy including the corresponding versions, which also represent a backup (distributed system). A continuous connection to the remote repository is therefore not mandatory, only for synchronization. In addition, Git is platform-independent and can therefore be set up in almost any environment.
First steps and main functions
Git comes “out of the box” with a command line, without a GUI. For some this is a great thing, others prefer to work with one of the many graphical interfaces or use plug-ins that are available for various editors. One advantage of the command line in contrast to all the GUI alternatives is that really all available Git commands can be executed. The first and most important steps after installation are as follows:
The (basic) configuration
git config -1 | View configuration |
Git config –global user.name „Max Mustermann“ | Set up user (will be displayed in every commit) |
git config –global user.email „max.mustermann@in-factory.com“ | Set up email |
git config user.name | Query individual configuration |
git config –global init.defaultBranch main | Configure branch name “main” |
Create/clone the repository
git init | initialize a new repository (for this you have to be in the respective directory) |
git status | Get status in the repository |
git clone /verzeichnis/repository | creates a local copy of a repository |
git clone benutzername@host:/verzeichnis/repository | creates a local copy of a remote repository |
The Workflow
“Git defines three main states that a file can be in: committed, modified, and marked for commit.
Modified means that a file has been modified but not yet checked into the local database.
Staged means that a changed file is marked in its current state for the next commit.
Committed means that the data is stored securely in the local database.“ (vgl. Scott Chacon & Ben Straub, Pro Git (Second Edition)¹, 2021, S. 17)
Directory tree, staging area and Git directory
Source: Scott Chacon & Ben Straub, Pro Git¹, Second Edition, 2021, S. 18
Add & Commit
git add | Add file (staging) |
git add * | add all files in the directory (staging) |
git commit | Commit “saves” the new version (affects the local repository, not the remote one) |
git commit -m „Message Text“ | Commit “saves” the new version with comment |
git log | List commits |
git reset | Undo action |
“Upload” the changes
git push origin | Sending changes in the local repository to the remote repository |
git remote add origin | Upload the created repository |
Branching
Branches, an essential difference to SVN, are used to isolate different developments from each other. The master or main branch is the main branch of each repository. During development, other branches should be used, which are then merged with the master or main branch at a later stage.
git checkout –b | create new Branch |
git checkout master | Change branch to master branch |
git branch –d | Delete branch |
git push origin | Upload branch to remote repository (make it available for others) |
Update & Merge
git pull | pull latest state from remote repository to local repository |
git merge | merge a branch with the current repository |
git diff | Show differences between two branches |
Tagging
git tag 1.0.0 | create a new tag (release tags as known e.g. from SVN, the Commit_Id is referenced with the tag) |
git log | View commit list |
Undo changes
git checkout — | Reset locally changed state to the checked-in state |
Git fetch origin Git reset –hard origin/master |
Discard local version and get state from remote repository |
git revert | Reverse commit |
Lifecycle status of a file in Git
The status of your files at a glance
Source: Scott Chacon & Ben Straub, Pro Git¹, 2021, S. 30
Advantages
- Parallel working (decentralized, branching)
- Security through distributed repository
- Flexibility through different working methods
- Offline work possible due to decentralized and linkable structure
- Extensibility (various web clients and plug-ins)
- for free
Disadvantages and critics
A big point of criticism is the not so easy learnability. For beginners, the terminology is partly new and users of other systems are sometimes irritated, e.g. “-revert” means something different in SVN than in Git.
For example, if you want to completely record the structures of empty directories for a reason, Git is not suitable. If the directories have no content, Git discards them.
Comparison with SVN
Whether Git or SVN, they are probably the two best known among their kind and both have their respective advantages and disadvantages. Here are the most relevant differences:
Git | SVN | |
Version control | distributed | central |
Repository | local repository copies, in which development takes place | central repository where work progress is created |
Connection with network | Only required for synchronization | for each access |
Permission/Access | complete directory | path-based |
Changes | Records content | Records files |
Traceability/History | Repository and local copies contain complete history | only complete in the repository, local copies contain only the latest version |
¹The book is free to download here
Author: Arkadius Buchatz