DEV Community

Nguyen Thien Ly
Nguyen Thien Ly

Posted on

Synthesis about git

Synthesis about git

New key word

  • synthesis: tổng hợp
  • nature: bản chất


To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at.

The git remote add command takes two arguments:

  • A remote name, for example, origin
  • A remote URL, for example,

For example:

$ git remote add origin
# Set a new remote

$ git remote -v
# Verify new remote
> origin (fetch)
> origin (push)


1. Branching off (copy)

$ git checkout -b <new_working_branch>

2. Listing branches

$ git branch

3. Switching branch

$ git checkout <branch_name>

4. Remove branch on local

$ git branch -D <branch_name>

5. Remove branch on remote

$ git push --delete origin <branch_name>


$ git status


show status change each file

$ git diff

If files have added (committed), use

$ git diff --cached

Note, add color to see easy and cool

$ git config --global color.ui true


$ git add .


$ git add -A

add . and add -A add all change into committed list.

just add specific file need to commit, use

$ git add <file_path>

Reset commit, not create new commit, use

$ git commit --amend -m "an updated commit message"
$ git commit --amend --no-edit


$ git log

Press [Enter] to scroll the list till the end or press q to quit

To search commit logs matching a specific pattern, use:

$ git log -g --grep=<pattern>
  • Add flag –oneline to show on line 1 commit.
  • Add flag –graph to show relationship parent-child


Check who is author of line in 1 file (and commit hash id, time)

$ git blame <file_path>


After to commit, push these changes on origin (remote repo)

$ git push origin <branch_to_push>

Use push to delete remote branch (note have ":")

$ git push origin :<branch_to_remove>


Tag version

$ git tag -a <tag_name> -m 'message'


$ git tag <tag_name>

Push tag on remote repo

$ git push origin <tag_name>

Delete tag

  • Delete in local first: $ git tag -d
  • Delete in remote: $ git push origin -delete


Use to join 2 or more commit from difference branch

$ git merge <branch_to_merge>


Use to update branch origin in local with branch similar on remote repo

$ git fetch origin <branch_on_remote_repo_to_fetch>


Nature is fetch before merge

$ git pull origin <branch_on_remote_repo_to_pull>

Or add flag -rebase to fetch before rebase

$ git pull --rebase origin <branch_on_remote_repo_to_pull>


Point local repo to old commit in history

$ git reset HEAD


$ git reset <commit_hash_id>

Note: Use $ git reflog to show list

Flag –hard: destroy all change

$ git reset --hard
$ git reset <commit_hash_id> --hard

After reset can update changes on remote repo to override history commit of this branch

$ git push --force origin <remote_branch>

Note: you should limit the use of operations that change the commit history


Used to reverse the changes of an old commit in history, essentially creating a new commit with the opposite change from the selected commit.

This is the safest way to revert changes because the commit history is completely unchanged, and the revert is also logged.

$ git revert <commit_hash_id>


Top comments (0)