This blog entry gives a quick insight into Git basics, is aimed at beginners and is intended to make working and getting started easier. The very first steps are shown, the most important functions are presented, the advantages and disadvantages are considered and a comparison to SVN is made. For advanced users, further entries will follow, covering various focal points, advanced functions and graphical GUI clients.
What is Git
Git is a distributed versioning system for managing files. Non-linear development and decentralized storage are typical of Git. Although there is a central repository, 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 absolutely necessary, only for synchronization. In addition, Git is platform-independent and can therefore be set up in almost any environment.
First steps and most important 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 all existing Git commands can be executed. The first and most important steps after installation are as follows:
The (basic) configuration
git config -1 | Show configuration |
Git config -global user.name "Max Mustermann" | Set up user (displayed in every commit) |
git config -global user.email "max.mustermann@in-factory.com" | Set up e-mail |
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 (you must be in the respective directory for this) |
git status | Query status in the repository |
git clone /directory/repository | creates a local copy of a repository |
git clone username@host:/directory/repository | creates a local copy of a remote repository |
The workflow
"Git defines three main states in which a file can be: committed, modified and staged.
Modified means that a file has been changed but not yet checked into the local database.
Staged means that a changed file is marked for the next commit in its current state.
Committed means that the data is securely stored in the local database." (see Scott Chacon & Ben Straub, Pro Git (Second Edition)¹, 2021, p. 17)
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 | Send changes in the local repository to the remote repository |
git remote add origin | Upload created repository |
Branching
Branches, a major difference to SVN, are used to isolate different developments from each other. The master or main branch is the main branch of each repository. Other branches should be used during development, which are then merged with the master or main branch at a later stage.
git checkout -b | creates new branch |
git checkout master | Switch branch to master branch |
git branch -d | Delete branch |
git push origin | Upload branch to remote repository (make available to others) |
Update & Merge
git pull | Pull the latest version from the remote repository into the 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 from SVN, for example, the Commit_Id is referenced with the tag) |
git log | Show commit list |
Undo change
git checkout - | Reset locally changed status to the checked-in status |
Git fetch origin Git reset -hard origin/master |
Discard local version and retrieve status from remote repository |
git revert | Undo commit |
Lifecycle status of a file in Git
Advantages
- Parallel working (decentralized, branching)
- Protection through distributed repository
- Flexibility through different working methods
- Offline work possible due to decentralized and linkable structure
- Extensibility (various web clients and plug-ins)
- Free of charge
Disadvantages and criticism
One major point of criticism is that it is not so easy to learn. For beginners, some of the terminology is new and users of other systems are sometimes confused, e.g. "-revert" means something different in SVN than in Git.
If, for example, you want to completely record the structures of empty directories for some 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 of their kind and both have their respective advantages and disadvantages. Here are the most relevant differences:
Git | SVN | |
Version management | distributed | central |
Repository | Local repository copies in which development takes place | Central repository in which work progress is created |
Connection with network | Only required for synchronization | with every access |
Authorization/access | entire directory | path-based |
Changes | records content | records files |
Traceability/history | Repository and local copies contain complete history | only complete in the repository, local copies only contain the latest version |
The book can be downloaded free of charge here