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

Remote

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, https://github.com/user/repo.git

For example:

$ git remote add origin https://github.com/user/repo.git
# Set a new remote

$ git remote -v
# Verify new remote
> origin  https://github.com/user/repo.git (fetch)
> origin  https://github.com/user/repo.git (push)

Branch

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>

Status

$ git status

Diff

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

Commit

$ git add .

or

$ 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

Log

$ 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

blame

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

$ git blame <file_path>

Push

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

Tag version

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

Or

$ 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

Merge

Use to join 2 or more commit from difference branch

$ git merge <branch_to_merge>

Fetch

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

$ git fetch origin <branch_on_remote_repo_to_fetch>

Pull

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>

Reset

Point local repo to old commit in history

$ git reset HEAD

Or

$ 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

Revert

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>

References

Top comments (0)