Git (four): Git commonly used commands

1. Local library initialization

#初始化本地库
git init

Note: .gitThe subdirectories and files related to the local library are stored in the directory. Do not delete or modify them randomly.

2. Set the signature

  • form
    用户名:tom
    Email地址:[email protected]
    
  • Role: distinguish the identities of different developers
  • Discrimination: The signature set here has nothing to do with the account and password for logging in to the remote library (code hosting center).
  • command
    • Project level/warehouse level: only valid within the scope of the current local library
       git config user.name test
       git config user.email [email protected]
       # 信息保存位置:./.git/config 文件
      
    • System user level: the range of users who log in to the current operating system
      git config user.name test
      git config user.email [email protected]
      #信息保存位置:~/.gitconfig 文件
      
    • Level priority
      • Proximity principle: project level takes precedence over system user level, and both of them use project level signatures from time to time;
      • If there is only a system user-level signature, the system user-level signature shall prevail
      • Neither is allowed

3. Basic operation

3.1 Status View

#查看工作区、暂存区状态
git status

3.2 Add

#将工作区的”新建/修改“添加到暂存区
git add [file name]

3.3 Submit

#将暂存区的内容提交到本地库
git commit -m "commit message" [file name]

3.4 View history

git log
  • Multi-screen display control mode:
    • Space to page down;
    • b Page up;
    • q exit;
git log --pretty=oneline

git log --oneline

git reflog

HEAD@{How many steps does it take to move to the current version}

3.5 forward and backward

  • Operation based on index value [recommended]
    git reset --hard [局部索引值]
    #git reset -- hard a6ace91
    
  • Use ^symbol: only back
    git reset --hard HEAD^
    #一个 ^ 表示后退一步,n个表示后退n步
    
  • Use ~symbols: You can only retreat
    git reset --hard HEAD~n
    #表示后退n步
    

3.6 Comparison of the three parameters of the reset command

  • --soft :parameter
    • Only move the HEAD pointer in the native library
  • --mixed :parameter
    • Move the HEAD pointer in the local library
    • Reset staging area
  • --hard:parameter
    • Move the HEAD pointer in the local library
    • Reset staging area
    • Reset workspace

3.7 Delete files and retrieve them

  • Prerequisite: Before deleting, the state of the file when it exists is submitted to the local library.
  • operating:git reset --hard [指针位置]
    • The delete operation has been submitted to the local library: the pointer position points to the historical record
    • The delete operation has not been submitted to the local library: the pointer position uses HEAD

3.8 Compare file differences

#将工作区中的文件和暂存区进行比较
git diff [文件名]

#讲工作区中的文件和本地库历史记录比较
git diff [本地库历史版本] [文件名]

#不带文件名比较多个文件

4. Branch Management

4.1 What is a branch

In the version control process, multiple lines are used to advance multiple tasks at the same time.
Insert picture description here

4.2 Benefits of branching

  • Simultaneously promote the development of multiple functions in parallel to improve development efficiency
  • In the development process of each branch, if one branch fails to develop, it will not have any impact on other branches. Delete the failed branch and start again.

4.3 Branch operation

#创建分支
git branch [分支名]

#查看分支
git branch -v

#切换分支
git checkout [分支名]

#合并分支
#第一步:切换到接收修改的分支(被合并,增加新内容)上
git checkout [被合并分支名]
#第二步:执行merge命令
git merge [有新内容分支名]
  • Conflict resolution
    • Manifestations of conflict
      Insert picture description here
  • Conflict resolution
    • Step 1: Edit the file and delete special symbols
    • Step 2: Modify the file to a satisfactory level, save and exit;
    • The third step: git add [file name]
    • The fourth step: git commit -m "log information"
      • Note: Commit must be able to carry a specific file name at this time

Guess you like

Origin blog.csdn.net/houwanle/article/details/112548712