To begin with, we shall ask ourself what is Git and which function does it have?
The basic answer to that question is rather simple. Git is a so called Version Control System or VCS in short. It allows the developers to track the changes to their project and included files/code, while also beeing able to simultaneously work and collaborate with others on the same file. At first glimps it might not sound as simple, especially without the context of its functionality and usage. Therefor I will further explain, on what exactly a VCS is, as well as listing a quick breakdown of the most applied commands and usage for Git in the upcoming paragraphs.
Version Control System VCS
So what exactly does a VCS do?
A VCS is a tool that records changes to a file over time, enabling the user to recall a specific versions of it later. It stores the informations of a file, by time and date, which developer made them and a short description of which changes were applied. Giving the developer the option to acces or use an older version of the same file during a later stage of the project.
Git is a distributed VCS, which means that each developer has their own local copy of the file and is able to work on it independently. This is different from a centralized VCS, where there is a single repository that all developers must access, and where conflicts can occur if for example multiple developers try to make changes to the same file at the same time. This kind of version control is achieved by using so called local branches or in other words copies of the reposetory for each individual developer.
Branches
Are we talking about trees, or what is a branch?
To picturize a tree might actually help with the approach. Imagine the trunk of a tree which is the base of a tree. In this case the main reposetory is the trunk. From this trunk multiple branches can grow and split aswell as merge to form a tree. This of course is just a visual comparison of the different developers working on their local copy of the reposetory and merging the changes to the main reposetory.
A branch in Git is essentially a separate line of development that allows you to work on new features or modifications to existing files without affecting the main file or project. When you create a branch in Git you create a copy of the main file or project at a particular point in time. You can then make changes to this copy without affecting the main project. Once you are satisfied with the changes you've made you can merge your branch back into the main reposetory which is often named the main or master branch.
Creating a branch is rather easy. You can use the commands such as git branch followed by the name of the new branch you want to create. For example, git branch my-branch will create a new branch called my-branch. You can then switch to this branch using the git checkout command: AN an example for that would be git checkout my-branch. Once you've made changes to a branch you'll want to merge those changes back into the main branch.
Merging
How do we combine all the branches?
Most of the time when a new feature or data is created during development it will be assigned to a new branch. To update the project we need to find a way to combine them at one point so everyone has an updated version of every single change made to the project. Luckily there is a function that allows us to to so, it is called a merge.
In Git a merge is the process of combining changes from one branch into another. Merging is an essential operation when working with branches as it allows developers to integrate their changes with other possible branches to the repository.
There are several different types of merges in Git, including:
-
Fast-forward merge: A fast-forward merge occurs when the changes made in one branch are applied directly to the branch being merged into, without any additional merging required. This happens when the two branches have diverged but there have been no additional commits on the branch being merged into.
-
Three-way merge: A three-way merge is required when the branches being merged have diverged and there are changes made on both branches that cannot be automatically merged. In this case, Git creates a new commit that combines the changes from both branches. Git uses the common ancestor commit of the two branches to determine which changes to apply, and then creates a new commit that includes the merged changes.
-
Recursive merge: A recursive merge is similar to a three-way merge, but it is used when the branches being merged have multiple independent branches. Git creates a new commit that combines the changes from all the independent branches, and then merges that commit with the main branch.
Merging can sometimes result in conflicts if changes on one branch conflict with changes on another branch. Conflicts occur when two changes have been made to the same part of a file and Git cannot automatically determine which change to use. In this case Git will stop the merge process and prompt the user to manually resolve the conflict.
Commit, Push and Pull requests
Okay, I now know about most of Git's basics, but how do I even update my branches?
In order to keep your branch updated there are three main commands to be used,git commit, git push and git pull. Let's start with the commit command to get a better understanding of what to do.
A commit is a snapshot of the changes made to the files in the repository at a specific point in time. When you make changes to the files in your local repository you need to commit those changes to record them in the repository's history. This is done using the git commit command, which creates a new commit with a unique identifier, a commit message, and a pointer to the parent commit.
After you have made a commit, you can push it to the main repository. This makes your changes available to other developers who are working on the same project. You can push your commits to a remote repository using the git push command. This command sends the commits from your local repository to the main repository where they can be accessed by other developers.
When working on a project with multiple developers, it's common to use branches to isolate changes and prevent conflicts. After you have made changes to a branch in your local repository, you can create a pull request to propose those changes to the main branch of the repository. A pull request is a request to merge the changes in your branch into another branch, usually the main branch of the repository.
To create a pull request you first need to push your branch to the remote repository. Then you can create a pull request on the remote repository's website or using the git pull-request command. In the pull request, you can review the changes that you have made and request that other developers review them as well. Once the changes have been reviewed and approved, the pull request can be merged, which means that the changes in your branch are merged into the target branch.
Cheatsheet
graph LR; A[Create a new branch] --> B(Modify files); B --> C(Stage changes); C --> D(Commit changes); D --> E(Push changes); E --> F(Make pull request); F --> G(Merge changes into main branch); G --> H(Deploy changes);
Basic Commands
git init
: Initializes a new Git repository.git clone <url>
: Clones a remote Git repository to your local machine.git add <file>
: Adds changes to a file to the staging area.git commit -m "<message>"
: Commits changes in the staging area with a message.git status
: Shows the status of the working directory and staging area.git log
: Displays the commit history.
Branching and Merging
git branch
: Lists all local branches in the current repository.git branch <name>
: Creates a new branch.git checkout <branch>
: Switches to the specified branch.git merge <branch>
: Merges changes from the specified branch into the current branch.
Remote Repositories
git remote
: Lists all remote repositories.git remote add <name> <url>
: Adds a new remote repository.git push <remote> <branch>
: Pushes changes to the specified remote repository and branch.git pull <remote> <branch>
: Pulls changes from the specified remote repository and branch.