Skip to content

Git Basics

berkaydoner edited this page Mar 29, 2021 · 1 revision

What is Git?

Git is an open-source Version Control System software that was developed by Linus Torvalds who is also the genius behind Linux kernel. Version Control System software products aim to provide an environment for software developers to collaborate without causing any conflicts or overwriting. Git is also a distributed system such that all users have their own repositories in addition to the main repository, which provides a nice backup system. Git is the most popular Version Control System software and definitely worth to learn.

Basic Concepts

Working Directory is where the files are first modified. After modifications, some files are moved to the staging area. Add is used to move files from this stage to stating area.

Staging Area is where only the modifications to be included in the next version resides. Commits are used to move files from this stage to the repository.

Repository is the main project directory under the ./git folder. It stores commits and branches to track the history. Note that, there are both local and remote repositories. Remote repositories are usually stored on a network. Local repository is changed through commits and remote repository is changed through push and pull commands.

File Storage

Branches are similar to the "branches" of a tree in a way that they are diverging from the main line. In Git, a branch represents a separate development environment from the main environment. It allows users to make changes in their repository without changing or breaking the main line. The default branch is called the master. At the end of the development of a new feature, a branch can be merged with other branches. Also, checkout command can be used to switch between branches.

Head is a pointer to the current branch.

Branch-Merge Time Cycle

Git Commands

Getting Help

git help <command>
Displays the manual page of the command.

Creating and Setting Up Repositories

git init
Initializes a new repository under the working directory.

git clone <repo-url>
Creates a copy of the given repository under the working directory.

Making Changes

git add <filename>
Propagates the file to the staging area. It can be used many times before commit to select the modifications to be included.

git commit
Commits all modifications in the staged area to the repository.

git stash
Saves uncommitted changes for later use. pop option is used to apply the saved changes.

git reset [commit]
Resets the head to the given commit. If it is used with --hard option, stating area and working tree are also reseted.

git revert [commit]
Creates a new commit that reverts the changes from the given commit. It does not modify the history.

Branch Operations

git branch
Lists all branches.

git branch <branch>
Create a new branch with the given name.

git merge <branch>
Merge the branch with the current one by applying the modifications.

git checkout <branch>
Switch to the given branch.

Comparison

git diff
Shows the difference between the staging area and the working directory.

git status
Shows the whole status of the working tree.

Communicating with the Remote Repository

git push
Apply the changes in the local repository to the remote repository.

git pull
Apply the changes in the remote repository to the local repository.

References

Clone this wiki locally