Published on

Git 101: All the Knowledge You Need to Become a Software Engineer

Authors

To save time, you can immediately refer to this GitHub Repository!

Version Control Concept

Version Control is a supporting tool that acts as the guardian of the project. Version Control helps track every change in the source code, ensuring that every change, no matter how small, is recorded.

Version Control allows programmers to revert the old state of the software source code to any version in the past. These supports of Version Control make the software development process of programmers much easier and safer.

Why is Version Control Needed?

  • Version Control helps store the history of the source code clearly and transparently. All the smallest changes in the project are identified.
  • It makes the process of multiple people collaborating on the same project more efficient and easier.
  • When an incident occurs, Version Control can help programmers revert the source code state to a safer version in the past without much effort.

Differentiating Git and GitHub

  • Git is the name of a Version Control software most used by programmers today.
  • GitHub is a platform that provides services for storing, managing, and deploying projects managed by Git
  • Besides GitHub, we can also use the services of GitLab, Bitbucket, or build our own host to manage our projects through Git.

Client Software Supporting Git Usage

There are many client software supporting Git usage. Paid or free.

Depending on the purpose of use, the amount of money you have, and the habits of each programmer, you can choose the appropriate software.

Recommendation: Use Git Desktop as your Git Client and GitHub as the host to store your projects.

Basic Concepts in Git

  • git config is a git command line used to get or set the configuration of git on your (programmer's) machine. These configurations can be your name and email.
git config --global user.name "dantech0xff"
git config --global user.email "dantech0xff@github.com"
  • git init is a git command line used to initialize a Local Repository
git init
  • git add is a git command line used to add one (or more) changes to the staged state to prepare for a commit
git add path-to-file-changes // add only 1 file
git add . // add all changes
  • git commit is a git command line used to create a commit to the repository with the files that are in the staged state. Each commit can be understood as the smallest unit to create the log (history) of a repository.
git commit -m"commit message"
  • git push is a git command line used to create a request to the remote repository. This request will check and confirm whether the commits in the local are approved to be written to the remote or not? If there is an error during this process, we must update the status of the branch before pushing.
git push
  • git pull is a git command line used to create a request to the remote repository. This request will pull down all the latest changes of the current branch you are standing on. Then apply these changes (if any) to your local repository. In reality, there will be 2 types of pull: pull rebase and pull merge. Details will be visually instructed during the learning process.
git pull
  • git checkout is a git command line used to switch the branch you are pointing to on the local repository.
  • git checkout is also a concise command to help you create a new branch.
git checkout  // switch branch to
git checkout -b  // create a new branch  and switch
  • git merge is a git command line used to merge any branch to the branch you are pointing to. This is a command you rarely use directly! Usually, you will operate on Git Desktop software to merge 2 branches. Or create a Merge Request / Pull Request and then handle the merge operation on the GitHub platform.
// assume you have a branch with the name is feature-01
// you're staying on a random branch
// you want to merge feature-01 to master branch

git checkout master
git merge feature-01

Advanced Concepts in Git

  • git fetch is a command used to send a request to the remote repository to receive the latest information of the repository. This latest information (commits, branches, ...) will not be applied immediately to the local repository but will be cached in a hidden folder. This is a command often used to check what changes the repo has after a period of time. Based on the information returned, we can choose a branch to checkout and perform other necessary operations.
git fetch master // fetch branch master
git fetch origin master:master // fetch branch master & update it to local without switch branch
  • git push --force is a dangerous command. You should only use it when it is really necessary and you understand its function. Like the name git push --force, when performing a push operation from local to repo, git with option —-force will skip the step of checking whether the local needs to sync with the remote first or not. All changes in the local will overwrite the remote.
git push --force-with-lease
  • git rebase is a difficult command to use. Most of the time, rebase will be used through Git Client. git rebase is used when 2 branches have a common base of original commits and are branching in 2 different directions. The context is usually a feature branch separated from the master branch, after a period of development, the feature branch now has n commits. However, because a lot of time has passed, the master branch now has m new commits. To help the process of merging feature back to master be convenient, we have a solution that is to rebase master to feature before merging feature into master. At this time, it can be imagined that the feature branch will have n commits located on master + m commits
git rebase master

Good luck. Remember to share if you find the article useful!