Tổng hợp những lệnh Git thông dụng nhất



Bài viết này chia sẻ về những quy trình và những câu lệnh git thông dụng nhất


I. Basic procedure

  1. Go to the main branch and pull the newest source code to your computer

    - git pull origin main

  2. From the main branch, create a new branch following the standard

    - git checkout -b [branch_name]

  3. Modify the source code

  4. List all of the files changed

    - git status

  5. Add a change in the working directory to the staging area

    - git add [file_name]

  6. Add a commit following the standard

    - git commit -m"content of commit"

  7. Push the code to the remote repositories

    - git push origin [branch_name]

  8. Create the pull request on the website and add the reviewer

  9. Merge the PR


Additionally, you need to use some small process below

II. Optional

  a) Rebase the branch(used to update the change from the main branch to your branch)

    1. Go to the main branch and pull the newest source code
      - git checkout main -> git pull origin main
    2. Go to your branch on the local
      - git checkout [your_branch_name]
    3. git rebase main
    4. git push origin -f +[your_branch_name]

    5. Additionally, if you want to cancel the rebase
        - git rebase --abort

  b) Cherry-pick(picking a commit from a branch and applying it to another branch)

    1. Go to your branch on the local
      - git checkout [your_branch_name]
    2. Find the ID of the commit which you want to cherry-pick to your branch
      - git log --oneline [branch_name_cherry_pick]
    3. Cherry-pick
      - git cherry-pick -x [commit ID]
    4. Push the code to the remote repositories
      - git push origin [your_branch_name]

    5. Additionally, if you want to cancel the cherry-pick
      - git cherry-pick --abort


  c) Revert commit(revert one commit out of one branch)

    If you merged your PR to the main branch, and you need to revert your commit from the main branch
      1. From the main branch, create a new branch
        - git checkout -b [branch_name]
      2. Find the ID of the commit which you want to revert
        - git log --oneline [branch_name]
      3. Revert commit
        - git revert -m 1 [commit ID]
      4. Push the code to the remote repositories
        - git push origin [branch_name]
      5. Create the pull request on the website and add the reviewer
      6. Merge the PR

    If you haven't merged your PR yet
      1. Go to your branch on locally
        - git checkout [your_branch_name]
      2. Find the ID of the commit which you want to revert
        - git log --oneline [branch_name]
      3. Revert commit
        - git revert -m 1 [commit ID]
      4. Push the code to the remote repositories
        - git push origin [branch_name]


  d) Squash commit on a branch(before you merge your PR to main branch, if you have more than one commit, you need to squash the commit)

    1. Go to your branch on local
      - git checkout [your_branch_name]
    2. View the history of commits on your branch
      - git log --oneline
    3. squash commit
      - git rebase -i HEAD~n
        n: number of commit which you need to squash(if your branch has four commit => n=4)

    4. Modify the commit(use the command of vim)
      - the first commit(old commit) is selected as "pick"
      - The rest commit is selected as "s"
    5. Rename the commit
    6. Push the code to the remote repositories
      - git push origin -f +[your_branch_name]

    7. If there is an error, "has no upstream branch."
      - git branch -u origin/[your_branch_name]
      - git push origin -f +[your_branch_name]

  e) Resolve conflict when rebasing on the local

    1. Find the files that have a conflict on the terminal of git
    2. Open the files that have a conflict and modify the content of the file to fix the conflict
    3. Add a change in the working directory to the staging area
      - git add [file_name]
    4. continue rebase
      - git rebase --continue
    5. Push the code to the remote repositories
      - git push origin -f +[your_branch_name]

III. Common commands

  1. Fetch remote branch

    - git fetch origin develop/23q4


  2. Rename branch

    - step1: git checkout [old-name]

    - step2: git branch -m [new-name]

    - step3: git push origin :[old-name] [new-name]

    - step4: git push origin -u [new-name]


  3. Delete branch

    - local:  git branch -D [your_branch_name]

    - remote: git push origin --delete [your_branch_name]


  4. Go to the previous commit

    - git reset --hard [commit_sha]


  5. Move the change to stash and checkout to another branch

    - git stash: Move the change to stash

    - git stash apply: Move the change from stash to your branch

    - git stash list: check the commit in stash


  6. Modify commit on local

    - git commit --amend


  7. List all of submodule

    - git submodule status

    - cat .gitmodules


  8. Modify/delete submodule

    - git config --edit --global


  9. See list of ignore file

    - git status --ignored


  10. remove untracked files

    - git clean -i

    - git clean -fd

    - git reset --hard

  11. Compare the difference between two branch

    - git diff [main_branch] [your_branch_name]

Đăng nhận xét

0 Nhận xét