GIT is as easy as candy.

Tram Ho

Hello everyone. In this article I will do about GIT. With the goal that newbies will also become masters after this article

Git is distributed source code management software developed by Linus Torvalds in 2005, originally for the development of the Linux kernel. Currently, Git has become one of the most popular source code management software. Git is open source software distributed under the GPL2 public license.

Git is a Distributed Version Control System. Thanks to Git, code management and developer teamwork becomes simpler and more convenient.

I. Commands to work with the local repo on a default branch

1-git.drawio (1).png

1.1. git init . command

The git init command initializes a new Git repository (Git Repo) locally. At a directory of any project, executing the git init command will create a hidden directory .git and this directory contains the information of the local repo such as branches, config, HEAD, objects, refs, …
git init
image.png

All other git commands to be executed must first initialize the repository containing git information.
git init –bare
The command to add the –bare flag creates a repo that you can only access, store, but not edit, file, or commit directly in this repo. Usually it is created with the purpose to store as a Remote repo (Server repo), from there clone to another repo and from another repo push data to that repo.

1.2. The command git add

The git add command saves the changes to the Stagging area. It can be understood that git add is like pressing Ctrl + S to save a state when you type word, and you can undo the previous state with the git reset command or go to the completed state with the git commit command./ git add ebook.txt
The git add command will add the ebook.txt file to the Stagging area. If the ebook.txt file is added by git for the first time, a snapshot of the entire ebook.txt file will be created. The next time the snapshot is created, the content changes compared to the previous commit.

The command to add all files and folders in the current directory executes the command.
git add --all
git add .

1.3. command git status

The git status command displays different information (due to adding, deleting, modifying files) of the file. And a file can have 4 states: untracked, unmodified, modified, staged.

  1. Untracked When creating a new file and not performing git add for the first time. The file will be in this state if you check
  2. Unmodified As soon as the workspace has done the last commit will be in this state and you do git status
  3. Modified With modified is the state where you have changed without git add.
  4. Staged With modified shows when you have new changes and did git add.

git status -s
The command adds the -s flag to briefly display the status of the file. Useful when you are working with a lot of files at the same time. Now, before the changed files, there may be characters corresponding to the following information:

  • ‘ ‘ = unmodified (unchanged)
  • M = modified (with modification)
  • A = added (new file added)
  • D = deleted (file is deleted)
  • R = rename (rename file)
  • C = copied (file copied from another file)
  • U = updated but unmerged (updated, but not merged)

1.4. git commit command

The git commit command saves to the Git database all the content contained in the Stagging area (.index) and is accompanied by an informative text (log) describing the change of this commit compared to the previous commit. After commit, HEAD pointer automatically moves to this commit.
git commit -m "commit lan 1"
The command has the -m flag to add comments to commits.

git commit -a -m "commit lan 2"
The command using the -a flag is equivalent to performing the git add command to put the files being monitored for changes to staging and then automatically run git commit.

1.5. git reset command

When you want to go back to the previous state you can use the git reset command.

You can undo from the Stagging area to the Workspace with the git reset command.
git reset

You can undo from the local repo to Stagging with the command git reset –soft HEAD~1. Usually used to go back to the Stagging area to commit again. Will delete 1 latest commit. You can change the parameter ~1 to another number if you want to delete a large number of commits.
git reset --soft HEAD~1

You can undo from the local Repo to Workspace with the command git reset –hard HEAD~1. This command will also clear the status in the Stagging area. That means you will completely delete 1 commit.
git reset --hard HEAD~1

1.6. command git log

The git log command helps to review commit history information, to monitor project changes. The git log command has many parameters to output, formatting the information displayed in the desired way. You can configure how each commit information is printed when viewed, as well as can filter certain information.

Git log it lists commits in order from newest to oldest, each commit has information including: hash of commit, message line, commit creator and commit date.
git log
git log –oneline
git log -p
git log -p -2

1.7. git diff command

The git diff command can check file changes across regions, commits, or branches.

Check the file difference between the workspace and the Stagging area
git diff

Check the file difference between the Stagging area and the information store.
git diff –staged

Check the file difference between two commits (assuming there are 2 id hashes as shown below)
git diff 1d2f924 367d9c0

Check file difference between two branches
git diff master dev

II. Commands to work with local repo with multiple branches

2.1. git branch command

In fact, a project will use many branches to develop a software such as dev, sit, ida, prod branch.

Check out the current branch.
git branch

Create a new dev branch
git branch dev

Rename the current branch to rename
git branch -M rename

See a list of all local and remote git branch -a

2.2. command git checkout

The git checkout command is often used to switch branches, it can also restore files in the working directory from a previous commit.
git checkout dev

The git reset command will undo to a certain commit, then git checkout helps the user to undo the file to the previous commit state. Also git will create a temporary branch for these checkout commands. A new working branch can be created from a temporary branch with the git switch -c command.
Undo the file content to the specified commit time and still save the log so you can undo it in reverse
git checkout HASH ebook.txt

Undo the file content to the specified commit and delete the log commit, which means you will not be able to return to the file state in the deleted commit.
git checkout HASH

2.3. git switch command

The git switch command performs a branch switch similar to checkout and can create a new branch.

Switch to branch
git switch dev

Create new branch, activate branch starting from a commit with code HASH
git switch -c sit HASH

Quick creation from last commit
git switch -c sit

2.4. git restore command

git restore command to restore the files of the working directory.
git restore ebook.txt

Usage is same as git checkout for recovery case.

2.5. git merge command

The git merge command merges two branches together, the command will merge both file information and .git storage information.

Merge branch local dev into branch master
git checkout master
git merge dev

Basically merge commands will easily lead to conflict cases. So before merge you need to understand and perform pre-check commands like git status, git log, git fetch,…

2.6. Handling conflicts in branches

2.6.1. Confict when merging two branches with different roots
When you want to merge two branches and the same part of the same file is changed, a merge conflict occurs. This is because Git can’t figure out which version to use. When this happens, it will stop before committing the merge to resolve that conflict.

Running git status will show the files that need to be resolved. In the event of a conflict, Git will edit the contents of the affected files with visual indicators that mark both sides of the conflicting content. These visual markers are:
<<<<<<< Mark a conflict, the conflict starts after this line.
======= Split your changes with those in another branch.
>>>>>>> End of conflicting lines.

This error will come out and will make it impossible to checkout, switch to another branch and report
image.png

The fix would be to do git add file that is giving the error and git commit again. Then you can go back to work as usual, can checkout, switch branch.

2.6.2. Confict when pushing to remote repo when executing on the same branch
The reason is that there are many users working on the same file. This leads to a difference between the commit status. When pushing the system will report different data.

The way to handle it is to do git pull to get all the data, edit the data as desired, git add back, commit and git push again.

III. Commands to work with remote repo (Github)

To do this you must have a Github account and create a repo first. I have created a cached repo on github.

3.1. git remote command

In fact, there will be two types of repo, the local Repo is usually understood as the personal computer repository including the project directory and the .git directory where the information is stored. And the second type is the remote Repo, which is usually public repositories like Github, Gitlab, …

The git remote command allows you to create, view, and delete connections to other repos (usually a public Repo – Github).

Create a link
git remote add origin https://github.com/daihoangg/cache.git
The command creates a connection to github with a shortname of origin. Since affiliate links are often quite long, typing and memorizing is difficult, creating an alternate shortname is necessary to reuse many times in push or fetch commands.

Displays information about created remotes including shortname and corresponding link of remotes
git remote -v

Command to display information of link origin
git remote show origin

Remove a link with the shortname origin created above
git remote rm origin

3.2. git clone command

The git clone command allows to clone a remote repo to a local repo.

Clone command over ssh
git clone git@github.com:daihoangg/cache.git

Clone command via https
git clone https://github.com/daihoangg/cache.git

3.3. The command git fetch

image.png The command git fetch git pull updates the data from the remote repo. Git fetch will only download the repository on the remote Repo without downloading the file or working directory to the local. This command is intended to check and track updated commits on the server from branches without changing the work flow on the local machine.

Your repository is named origin, download all its information from the remote:
git fetch origin master
git fetch --all

3.4. command git pull

The git pull command retrieves all information on the remote Repo including file, folder and .git information repository. git pull
git pull origin
git pull origin/dev
git merge origin/master

You can rewrite the commit history of the working branch, update the entire remote branch that is its base, then execute. git pull --rebase origin

3.5. command git push

The git push command pushes new commits in the local repo to the remote repo. When pushing, you can specify push to specific branches with these flags:

  • –all push all local branches to remote repo
  • –tags push all local tags to remote repo
  • –delete deletes a branch above the remote repo
  • -u push and create an upstream (the upload stream corresponds to the local branch), or use for the first time pushing to the remote repo.

git push -u origin dev
git push origin dev
git push origin –all
git push origin --delete beta

3.6. git tag command

Git tag is a name used to mark a point in the commit history when it is considered important or needs attention.

Show tags
git tag
git tag -l
The execution command uses the -l or –list flag to display information.

Create a new Tag to mark the last commit
git tag -a tag1 -m "tao tag lan 1"

You can tag any commit by getting the hash of that commit
git tag -a tag2 -m "tao tag lan 2 cho commit 9f" 9fceb02

View information about commits tagged with 1
git show tag1

Update tag on remote repo
git push origin tag1

Update all tags
git push origin --tags

Deleting a tag needs to be deleted both locally and remotely (if the tag has been pushed).
git tag -d tag1
git push --delete origin tag1

Thank you all for reading my post and when you’re done, please give me your feedback. The following article is better thanks to your feedback. If you find this post useful, give me an upvote. I thank you.

Share the news now

Source : Viblo