Git Basics
What is Git?
Git is a Version Control System - VCS widely used in source code management and software development. Git provides programmers with a repository containing the entire system's change history.
Git was created by Linus Torvalds in 2005, and quickly became one of the most popular and important version management tools globally.
Some of the best Git hosting services today include Github, Gitlab, Bitbucket...
How does Git work?
Git considers stored information as a set of snapshots – capture the entire content of all files at a certain time.
When the user runs a command, Git will "capture" the information and create a snapshot and a reference to that snapshot. To achieve high efficiency, Git only stores files if the file has changed, for files that have not changed, Git will only store a link to the file in the previous version that it stored.
With this way of working, Git will make archiving easier and not waste a lot of time reviewing unnecessary information.
Why does use Git?
- Git helps track source code changes over time, control revision history.
- Git allows multiple developers to work on a project together, helping to limit conflicts when they edit the source code together.
- When encountering errors or unexpected changes, Git allows reverting to the previous version.
- Git allows developers to work offline, remotely or distributedly.
- Git is open source, widely used and free.
- Git makes dividing and arranging tasks easier and more effective, allowing you to proceed multiple tasks at the same time.
Important terms in Git
Repository
Repository (also known as Repo) is the place where all the source code and change history of the project are stored.
Repository has 2 types:
- Remote Repository: Placed on git server, capable of sharing with many people.
- Local Repository: Placed on personal computers, reserved for users.
Branch
Branch is an independent working area of the repository.
When creating a repository, a main branch (master/main) will be created. Corresponding to each task, we will create a sub-branch and work on it. Changes in the child branch do not affect the main branch, which allows multiple tasks to be proceeded at the same time in the same repository with little conflict between each other. We can also merge branches together.
Similar to Repository, Branch is also divided into 2 types:
- Remote Branch: Saved on git server, can be shared.
- Local branch: Saved on personal computer, can be linked to remote branch or not.
Some other terms
| Term | Description |
|---|---|
| Commit | A record that stores the changes made in the repo, marked with information and commit codes. |
| Check out | Switch between specific branches or commits. |
| Fetch | Fetch new information from the remote repository but do not automatically combine it with the local repository. |
| Fork | Create a copy of a public repository in your git account to contribute or fix bugs. |
| Head | Points to the current commit in the working branch and is usually the last commit on the branch. |
| Index | The intermediate area between the working directory and the repository, where files wait to be committed. |
| Master | The default name of the main branch in a repository. |
| Merge | Incorporate the changes from another branch into the current branch. |
| Origin | The default name of the remote repository when you clone or copy the repository. |
| Pull | Incorporate the changes from remote repository into local repository and merge automatically. |
| Push | Push the changes from local repository to remote repository. |
| Rebase | Move the current chain of commits to the beginning of another branch to create a more linear history. |
| Remote | Remote is a copy of the repository on another computer or server. |
| Stash | Temporarily stores uncommitted changes for transfer to other work. |
| Tags | Mark a specific point in the commit history for easy access later. |
| Upstream | Remote repository you clone from or monitor for changes. |
Basic git commands
| Command | Description | Syntax |
|---|---|---|
| git config | Configure user or repository information. | git config [–-global] user.name "Name" git config [–-global] user.email "Email" |
| git init | Initializes a new repository in the current directory. | git init |
| git clone | Copy a repository from remote to local machine. | git clone remote_repository_URL |
| git status | Displays the status of files in the working directory and staging area. | git status |
| git add | Put files from working directory into staging area. | git add file_name # add all files git add . |
| git commit | Create a commit from files in the staging area. | git commit -m “commit message” |
| git push | Push commits from local repository to remote repository. | git push remote_name branch_name |
| git pull | Pull new commits from the remote repository to the local repository. | git pull remote_name branch_name |
| git branch | Manage branches in the repository. | # see branch list git branch # create branch git branch branch_name # delete branch git branch -d branch_name |
| git checkout | Switch between specific branches or commits. | git checkout branch_name git checkout commit_hash |
| git stash | Temporarily stores uncommitted changes for later work. | git stash # apply temporary changes git stash apply |
| git merge | Merge changes from another branch into the current branch. | git merge branch_name |
| git reset | Resets the state of HEAD or staging area according to a specific commit. | # retain changes in staging area git reset –soft commit_hash *# undo changes * git reset –hard commit_hash |
| git remote | Manage remote repositories linked to the local repository. | git remote add remote_name URL_remote # show list of remotes git remote -v |
Things to note when working with Git
To use Git effectively, you need to note:
- Use Git Cheat Sheets to remember commands
- Commit continuously and regularly
- Check before committing to Git
- Write clear notes when committing
- Each different task should be created and done on different branches.