Learn git common commands and usage guide in one article

Article directory

0. Preface

Background: Recently, some friends who have just joined the company always have a series of problems in the process of using git, and many of the problems are low-level problems. So I think it is necessary to share a post and learn from everyone. After looking for the company's git management specification document, I found that the description of the document is not very easy to understand, and it has a certain discrepancy with the current management method. So I made one myself. I hope you can gain something and collect it.
insert image description here

1. Branch classification and management

Branch classification and management is a key version control practice that helps teams organize and manage the code development process.

1. Branch classification specification:

The following is an example of branch classification specification and naming in table form. Currently, git has no hard specification.These specifications are summed up by everyone in the process of long-term use, and each company may have differences, so there is no need to go online

branch type describe naming example
Main Branch Used to release stable versions and deploy to production environments mainmaster
Development Branch For overall development and integration of new features develop
Feature Branch Used to develop a single function or feature feature/user-authentication
feature/payment-integration
Release Branch Used to prepare the project for release release/1.0.0
release/2.3.1
Hotfix Branch (Hotfix Branch) Used to urgently fix problems in production environments hotfix/bug-fix
hotfix/security-patch

2. Best Practices

List a best practice of branch management for your reference only.

Using Git generally has at least two branches: master and develop
master: the production environment comes to the main branch to get data deployment, and you can also use hooks to automatically complete the
develop: development branch, developers check out this branch for development
Use auxiliary branches
feature: specific Functional development branch, which only interacts with the develop branch.
release: released version, which needs to be merged into the master branch after testing iterations.
hotfix: emergency bug fix branch, which needs to be merged into the master and develop branches at last.

insert image description here

3. Example of branch naming convention:

  • Use lowercase letters and dashes (-) as separators for branch names.
  • Use clear, concise names that describe the purpose and content of the branch.
  • Follow a consistent naming convention so team members can easily understand and identify branches.

4. Branch management method:

  • Use a version control system such as Git to track and manage branch creation, merging, and deletion.
  • Define a well-defined branching strategy and workflow, including branch creation, merging, and naming conventions.
  • Ensure that team members understand and abide by branch management regulations, and conduct necessary training and communication.
  • Regularly perform branch merging and cleaning, and delete branches that are no longer needed to keep the branch structure clear and maintainable.

2. Commit comment specification

Commit comments are important information recorded in the version control system for each code submission. In order to keep the commit history clear, readable and easy to understand, the following is a commonly used commit comment specification, which is used by most companies, and each company may have its own special definition or commit comment terminology,This is not an annotated answer. I hope you submit comments according to your company's specifications. If there is no specification, you can refer to the following

1. Submit the comment structure:

<类型><描述>
[可选] <正文>
  • <类型>: Indicates the type of submission, which can be one of the following:

    • New (feat): add a new function or function module
    • fix : to fix a problem or bug
    • Documentation (docs): update documentation or notes
    • Style (style): Adjust the code format or style without affecting the code logic
    • Refactoring : Refactoring code, neither fixing bugs nor adding new features
    • Test (test): add or modify test code
    • Chore : changes to the build process or auxiliary tools
  • <描述>: Briefly describe the purpose or changes of this submission.

  • [可选] <正文>: Optional, used to provide more detailed submission instructions, reasons for changes, solutions, etc.

There are also the following common classifications. Don’t worry about which one to use. The company has norms and follows the company’s norms. The company does not follow the norms it has learned.

  • build: Modify the submission of the project's build system (xcodebuild, webpack, glup, etc.)
  • ci: Modify the submission of the project's continuous integration process (Kenkins, Travis, etc.)
  • chore: changes to the build process or auxiliary tools
  • docs: document submission (documents)
  • feat: new features (feature)
  • fix: fix bugs
  • pref: submissions related to performance and experience
  • refactor: code refactoring
  • revert: Roll back an earlier commit
  • release: Release a new version
  • style: Code modification that does not affect program logic, mainly style optimization and modification
  • test: test related development
  • improvement: optimize and improve existing functions

2. Guidelines for Submitting Comments:

  • Use concise, unambiguous language to describe the purpose of the commit, and avoid overly vague or lengthy comments.
  • Use the simple present tense (such as "adds a feature") rather than the past tense to describe committed changes.
  • Follow a consistent comment style and conventions to ensure team members can easily understand and read the commit history.
  • For important submissions, more detailed descriptions can be provided in the main body, including solutions, reference documents, etc.

For example, let me give you an example. The following is a comment for the git code submission of a project. You can take a look at it.

  • feat:添加用户认证功能
  • fix:修复支付模块中的空指针异常
  • docs:更新README,添加安装说明
  • style:按照编码规范格式化代码
  • refactor:优化数据访问层,提升性能
  • test:添加API端点的单元测试
  • chore:更新部署脚本

In fact, I have also seen that some companies' git comment specifications have feat[模块名]:描述such a structure, and I think it is also an excellent practice with finer granularity.

3. git common commands

1. git pull core usage

insert image description here
In special cases, if we use IDEA pull code, we can operate according to the specific scene.
Parameter explanation:

  • --rebase: Use the rebase method to merge the code, and apply the local commit to the pulled code to keep the commit history clean.
  • -ff-only: Only fast-forward merge (Fast-Forward Merge) is performed. If the fast-forward merge cannot be performed, the operation will be aborted and an error will be prompted.
  • --no-ff: Disables fast-forward merges, creating a new merge commit regardless of whether fast-forward merges are possible or not.
  • --squash: Before merging the code, squash multiple consecutive commits into one commit, and do not automatically create a merge commit.
  • --no-commit: Merge commits are not automatically created after the code is pulled, allowing you to make changes or other actions to the code before continuing.
  • --no-verify: Skip the verification step of Git hooks when performing a commit.
# 使用 rebase 进行合并
git pull --rebase

# 只进行快进合并
git pull --ff-only

# 禁用快进合并,创建一个新的合并提交
git pull --no-ff

# 在合并代码之前,将多个连续的提交压缩为一个提交
git pull --squash

# 拉取代码后不会自动创建合并提交
git pull --no-commit

# 跳过 Git 钩子的验证步骤
git pull --no-verify

git pullThe command is used to pull the latest code from the remote warehouse and merge it into the current branch. The following are git pullsome common parameters of the command and examples of corresponding usage scenarios:

  1. git pull

    • Scenario: Under normal circumstances, using directly git pullcan pull the latest code from the remote warehouse and merge it into the current branch.
    • Example:git pull
  2. git pull origin <远程分支名>

    • Scenario: Use this parameter when you need to pull code from a specific remote branch.
    • Example:git pull origin main
    • Description: This will pull the latest code from the branch originof the remote repository named and merge it into the current branch.main
  3. git pull --rebase

    • Scenario: Use this parameter when you want to use rebase to merge code and keep the commit history clean.
    • Example:git pull --rebase
    • Description: This will pull the latest code from the remote repository and apply the commits of the current branch to the pulled code.
  4. git pull --no-commit

    • Scenario: Use this parameter when you want to pull the latest code from the remote warehouse, but don't want to create a merge submission for the time being.
    • Example:git pull --no-commit
    • Description: This will pull the latest code from the remote repository and merge it into the current branch, but will not automatically create a merge commit. You can make modifications to the code or perform other operations before proceeding.
  5. git pull --ff-only

    • Scenario: Use this parameter when you want to perform only Fast-Forward Merge without creating additional merge commits.
    • Example:git pull --ff-only
    • Note: If the remote branch can be directly fast-forwarded and merged into the current branch, then the fast-forward merge will be performed; if the fast-forward merge cannot be performed, the operation will be aborted and an error will be prompted.

2. git push command

  1. git push

    • Scenario: Under normal circumstances, using directly git pushcan push the commit of the current branch to the remote branch associated with it.
    • Example:git push
  2. git push origin <本地分支名>:<远程分支名>

    • Scenario: Push the commits of the local branch to the specified remote branch.
    • Example:git push origin feature-branch:remote-branch
    • Description: This will feature-branchpush the commits from the local branch named to remote-branchthe remote branch named .
  3. git push --force

    • Scenario: Forced push, used to overwrite the commit history of the remote branch.
    • Example:git push --force
    • Description: This will force push the commits of the local branch, overwriting the commit history of the remote branch. Use with caution as it may cause data loss or conflicts.
  4. git push --tags

    • Scenario: Push all local tags (tags) to the remote warehouse.
    • Example:git push --tags
    • Description: This will push all the local tags to the remote warehouse, so that the corresponding tags will be created in the remote warehouse.
  5. git push --set-upstream origin <本地分支名>

    • Scenario: Associate and push the local branch with the remote branch.
    • Example:git push --set-upstream origin feature-branch
    • Description: This will push the commits of the local branch to originthe remote repository named and associate the local branch with the remote branch.

1. Initialize a new warehouse

  • Command: git init
  • Example: Initialize a new Git repository in the current directory.
    git init
    

2. Clone a remote repository

  • Command: git clone <remote warehouse address>
  • Example: Clone the remote repository named origin to the local.
    git clone https://github.com/user/repo.git
    

3. Add files to the staging area

  • Command: git add <filename>
  • Example: Add file.txt to the staging area.
    git add file.txt
    

4. Add all modified files to the staging area

  • Command: git add .
  • Example: Add all modified files to the staging area.
    git add .
    

5. Submit the files in the temporary storage area to the warehouse

  • Command: git commit -m "commit comment"
  • Example: Submit the files in the temporary storage area to the warehouse and add comments.
    git commit -m "提交文件"
    

6. View the status of the current warehouse

  • Command: git status
  • Example: View the status of the current repository.
    git status
    

7. View commit history

  • Command: git log
  • Example: View the commit history of the current branch.
    git log
    

8. View the modification difference of the file

  • Command: git diff <filename>
  • Example: View the modification differences of the file.txt file.
    git diff file.txt
    

9. View branch list

  • Command: git branch
  • Example: View a list of all branches of the current repository.
    git branch
    

10. Create a new branch

  • Command: git branch branch name
  • Example: Create a new branch called feature.
    git branch feature
    

11. Switch to another branch

  • Command: git checkout <branch name>
  • Example: switch to branch feature.
    git checkout feature
    
  1. Create a new branch and switch to it
  • Command: git checkout -b <branch name>
  • Example: Create a new branch called bugfix and switch to it.
    git checkout -b bugfix
    

13. Merge the specified branch into the current branch

- 命令:git merge <分支名>
- 示例:将分支feature合并到当前分支。
   git merge feature

14. Delete branch

  • Command: git branch -d <branch name>
  • Example: delete branch feature.
    git branch -d feature
    

15. Undo the modification of the workspace

  • Command: git checkout – <filename>
  • Example: Undo changes to the file.txt file.
    git checkout -- file.txt
    

16. Undo the files in the temporary storage area

  • Command: git reset HEAD <filename>
  • Example: Unstage the file.txt file.
    git reset HEAD file.txt
    

17. Roll back to the previous commit

  • Command: git reset HEAD^
  • Example: Roll back to the last commit.
    git reset HEAD^
    

18. Roll back to the specified commit version

  • Command: git reset <commit hash>
  • Example: Fall back to the version whose commit hash is abc123.
    git reset abc123
    

19. Pull the update from the remote warehouse to the local

  • Command: git pull <remote warehouse name> <branch name>
  • Example: Pull updates from the master branch of the remote warehouse origin.
    git pull origin master
    

20. Push the local branch to the remote repository

  • Command: git push <remote warehouse name> <branch name>
  • Example: Push the current branch to the remote warehouse origin.
     git push origin feature
    

21. Force push local branch to remote repository

  • Command: git push -f <remote warehouse name> <branch name>
  • Example: Force push the current branch to the remote warehouse origin.
    git push -f origin feature
    

22. View the list of remote warehouses

  • Command: git remote -v
  • Example: View the list of remote warehouses associated with the current warehouse.
    git remote -v
    

23. Add a remote repository

  • Command: git remote add <remote warehouse name> <remote warehouse address>
  • Example: Add a remote repository named upstream.
    git remote add upstream https://github.com/user/repo.git
    

24. Remove a remote repository

  • Command: git remote remove <remote warehouse name>
  • Example: Remove the remote warehouse origin.
    git remote remove origin
    ```
    
    

25. Create a label with an annotation

  • Command: git tag -a <tag name> -m "tag comment"
  • Example: Create a tag named v1.0 with an annotation.
    git tag -a v1.0 -m "版本1.0发布"
    ```
    
    

26. View the list of tags

  • Command: git tag
  • Example: View a list of all tags in the current repository.
    git tag
    ```
    
    

27. View tab details

  • Command: git show <tag name>
  • Example: View details for tag v1.0.
    git show v1.0
    ```
    
    

28. Push local tags to remote warehouse

  • Command: git push <remote warehouse name> <label name>
  • Example: Push the local tag v1.0 to the remote warehouse origin.
    git push origin v1.0
    

29. Push all local tags to the remote repository

  • Command: git push --tags
    Example: Push all local tags to the remote warehouse.
    git push --tags
    

30. Delete local tags

  • Command: git tag -d <tag name>
  • Example: Delete local tag v1.0.
    git tag -d v1.0
    

31. Delete remote tag

  • Command: git push <remote warehouse name> --delete <label name>
  • Example: Delete the tag v1.0 of the remote warehouse origin.
    git push origin --delete v1.0
    ```
    
    

32. Using Git to configure aliases

  • Command: git config --global alias.<alias> <command>
  • Example: configure the git status command to alias st.
    git config --global alias.st status
    

33. View remote warehouse information

  • Command: git remote show <remote warehouse name>
  • Example: View the details of the remote warehouse origin.
    git remote show origin
    

34. Create and switch to a new branch

  • Command: git checkout -b <branch name>
  • Example: Create a new branch called feature and switch to it.
    git checkout -b feature
    

35. Rename branch

  • Command: git branch -m <old branch name> <new branch name>
  • Example: Change the old branch name feature to new-feature.
    git branch -m feature new-feature
    

36. View the submission records of the remote warehouse

  • Command: git log <remote warehouse name>/<branch name>
  • Example: View the commit records of the master branch of the remote warehouse origin.
    git log origin/master
    

37. Pull the specified branch from the remote warehouse to the local

  • Command: git fetch <remote warehouse name> <remote branch name>:<local branch name>
  • Example: Pull the develop branch from the remote warehouse origin to the local feature branch.
    git fetch origin develop:feature
    

38. View remote branch list

  • Command: git branch -r
  • Example: View a list of all branches of a remote repository.
    git branch -r
    

39. Reset the specified branch to a specific commit version

  • Command: git reset <commit hash> --hard
  • Example: reset the current branch to the version whose commit hash is abc123.
    git reset abc123 --hard
    

40. Fix merge conflicts

  • Command: git merge --abort
  • Example: In the process of merging, if a conflict occurs, use this command to abandon the merging and restore to the state before the merging.
    git merge --abort
    

41. Modify the comments of the last commit

  • Command: git commit --amend
  • Example: Modify the comment of the last commit.
    git commit --amend
    

42. Temporarily store the current modification

  • Command: git stash save
  • Example: Temporarily store current modifications.
    git stash save
    

43. View stored modification list

  • Command: git stash list
  • Example: View a list of stored modifications.
    git stash list
    

44. Revert the last saved modification

  • Command: git stash apply
  • Example: restore the last saved modification to the workspace.
    git stash apply
    

45. Revert the specified stored changes

  • Command: git stash apply <stash@{number}>
  • Example: restore the modification of storage number 0 to the workspace.
    git stash apply stash@{
          
          0}
    

46. ​​Delete the last saved modification

  • Command: git stash drop
  • Example: Delete the last stored modification.
    git stash drop
    

47. Delete the specified stored modification

  • Command: git stash drop <stash@{number}>
  • Example: Delete stored modification with number 0.
    git stash drop stash@
    
    
    

4. Reference documents

Git complete command manual address: http://git-scm.com/docs
PDF version command manual: github-git-cheat-sheet.pdf

Guess you like

Origin blog.csdn.net/wangshuai6707/article/details/132085760