Git entry knowledge points

git code management area

Use git to manage projects and have three areas, namely work area, temporary storage area and storage area

  • Workspace: file directory
  • Staging area: list of files ready for version saving
  • Repository: List of files where the completed version is saved

Install

You can download it on the official website by yourself.
After installation, the first thing to do is to set your user name and email address, command:

git config --global user.name "用户名或者个人工号"

git config --global user.email “邮箱地址”  

View all global configuration information


git config --list --global

View the specified global configuration information


git config user.name

git config user.email

Get the git repository

There are two ways to get the git repository:

  1. Convert the local directory that has not been under version control to a git warehouse: git init
    Note: If you have a project directory that has not been under version control and want to use git to control it, you need to perform the following two steps
  • In the project directory, open "git Bash" by right mouse button
  • Execute the git init command to convert the current directory into a git warehouse
  • git remote add origin The URL of the remote warehouse
  • Submit the changes in the local warehouse to the main branch of the local warehouse, that is, the local warehouse has been modified , command: git add . git commit -m "commit information"
  • Push the main branch of the local warehouse to the main branch of the remote warehouse, command: git push -u origin master
    Note: origin is the alias of the remote warehouse address just added, the -u parameter means to set the local main branch to track the master of the remote warehouse branch. If you want to push other branches, you need to replace master with the corresponding branch name.
  1. Clone an existing git repository from another server: git clone (https: address)
  • If you encounter the fatal: protocol 'https' is not supported error, the solution: close the git-bash client, reopen it, execute the git clone url command again to clone, and clone successfully.

git common commands

  1. Download the github source code: git clone url
  2. Check the status of the local git warehouse, that is, check whether the files in the current workspace and temporary storage area have changed, and whether they are consistent with the local warehouse: git status
  3. Modify and submit to the temporary storage area: git add [modified file] - use git add by default to submit all modified files
  4. Modify and submit to the local warehouse: git commit -m [modified comment]
    Note: git add is saved in the temporary storage area and git commit is saved in the local warehouse
  5. Push to remote warehouse: git push
  6. Configure the account git config --local usr.name "xxx" git config --local usr.email "xxx" - so that you can see the relevant information of the individual contributor author in github (in a certain project, if you don’t want to use git The global author information set, we can set an author information only for this project in this project)

.gitignore - version tracking for ignored files

  1. Create a .gitignore file in the project root directory
  2. Open the file with a text compiler for configuration:
    –Each configuration has its own line
    –The content of each line can be: file/directory name, path or their pattern matching

Rules for pattern matching

  1. Empty lines do not match any files, so are often used as separators for readability
  2. # for comments, \ for escape
  3. * can match any character (0 or more times), ? matches any character only once

.gitinnore can only ignore untracked files, so it is invalid to write to .gitgnore after being included in version management. The
solution is
to delete the local cache first, and then add .gitignore

  • –git rm -r --cached
  • – Create a .gitignore file inside

Restore files from repository

Restoring files from the version library is divided into three situations

  1. Deleted (or modified), not added
  2. Deleted (or modified), added, not committed
  3. Deleted (or modified), and committed
    checkout commands are used for operations such as switching branches, restoring file versions, and creating new branches. :git checkout
  • Situation 1: Deleted (or modified), not added
    file_name restored from the temporary storage area: git checkout file_name
  • Situation 2: deleted (or modified), added, uncommitted and deleted (or modified), committed
    Check the modification in the warehouse to obtain the id number of the commit: git log
    recovery file: git checkout commit id number file_name

Branch Management in Repository Construction

What is a branch?

A branch can be understood as a copy of the current working directory

Why use branches?

Divide the project into multiple lines, such as making modifications on the development branch (development line), and then merging into the main line (product line) to ensure the stability of the main line. The develop/dev branch is used for development, and the main/master branch is used for
products

Common commands for branches

  1. Create a new branch: git branch [new branch name]
  2. Rename the name of the branch: git branch -m [old branch name] [new branch name]
  3. Delete branch: git branch -d [name of branch] where -d is used to delete the branch that has been merged, and -D is forced to delete the branch (regardless of whether it has been merged or not)
  4. Switch branch: git checkout [branch name]
  5. Create and switch branches: git checkout -b [new branch name]
  6. Branch view: git branch
  7. Merge of branches: git merge [name of branch to be merged]
  • When using the git merge command to merge two branches, if the same files or lines are modified differently in the two branches, it will cause conflicts. At this time, Git cannot automatically merge these changes, and conflicts need to be resolved manually. It can be done as follows:
  1. After executing the git merge command, if there is a conflict, Git will prompt which files have conflicts.
  2. Open the file containing the conflicts, and you will see tags in the file that look like this:
    <<<<<<< HEAD:file.txt
    // 当前分支中的修改内容
    =======
    // 要合并分支的修改内容
    >>>>>>> branch-a:file.txt
  1. Select the changes you want to keep as needed, delete unwanted marks, and save and close the entire file.
  2. Use the git add command to mark resolved conflicts as resolved.
  3. Finally execute the git commit command again to complete the merge commit.

Note: When Git finds that a piece of data contains changes in the commit history of both sides, it will not be able to automatically merge it. This situation is called a version conflict, and Git needs human intervention to adjust before continuing to merge.
Branch article link: https://blog.csdn.net/All_In_gzx_cc/article/details/125482617

Local library and remote library

  1. In the local library, make a local library association for the file to be saved
  • –git init
  • –git add .
  • –git commit -m “**”
  1. In gitee or github, create a remote library with the same name as the local library file
  2. Association between local library and remote library
  • The name of the associated remote library is specified as the origin command: git remote add origin https://github.com/Gooddz1/git-test2.git
  • Code push command: git push -u origin main
    Note: Push the main branch of the local library to the remote library origin; -u: upload and merge the local branch to the remote branch, and record it as the default. So when you submit again, you only need git push

A local library can be associated with multiple remote libraries, that is, github and gitee can be associated. It is worth noting that

  • When linking, the name of the remote library: cannot be set to origin, but can be set to gitee
  • When pushing, specify the remote library name and local library branch git push gitee main

Submit amendment – ​​when submitting commit, overwrite the previous diary to avoid too redundant diary

git commit --amend -m "***"

git diary

  1. Limit the number of log entries displayed: git log -n (n: the number of entries displayed)
  2. Simplify the display of the journal (only the id number and key information of the commit are displayed): git log --online
  3. Output statistics on file changes (statistics on modified files in each submission): git log --stat
  4. The content of the output file modification: git log --patch
    Note: For \No newline at end of file, it means that the last line of the file does not start a new line
  5. Diary filtering:
  • git log --after="2023-01-01" (display the diary after January 1, 2023, excluding the diary on January 1)
  • git log --before="2022-09-01" (display the diary from September 1, 2022, including the diary on September 1)
  • git log --before=" ** " --after=" ** "
  • git log --author=" ** " (filter based on the submitted user name or email address, where the email address does not need to be filled in)
  • git log --grep=" ** " (filter based on the submitted information, the submitted information does not need to be filled in completely)
  • git reflog (quote diary: records all submission information in the local library, can only be kept locally, cannot be pushed to the remote library, and can only be saved for 90 days by default. For example, for amend to submit changes, you can get commit through git reflog id to fall back to amend's code version)

Add, delete, push and other operations of remote library branches

  1. To view the information of the remote library corresponding to the local library, you can view the name of the remote library: git remote -v
  2. View the branches of the local and remote warehouses: git branch -a
    Note: remotes/origin/HEAD -> origin/main indicates that the Git repository is cloned from "origin" in remote tracking mode, and the current branch is tracking the remote master branch ( main branch).
  3. Push all local branches to the remote library: git push [remote library name] [branch name 1] [branch name 2]
    Note: git push [remote library name] --all: push all local branches to remote repository

How to set default branch in remote repository.

Take github as the column, open the github website of the code, and select settings. Then click branches and select the default branch in the default branch.

Delete the branch in the remote library (the default branch cannot be deleted):

  • Method 1: On the github website of the code, in the selection switch branches/tags, click view all branches. In a new page, manually delete unneeded branches.
  • Method 2: git push [name of the remote library] -d [name of the remote branch]
    Note: Method 1 is relatively simple, but it will cause the remote branch cached in the local library to be inconsistent with the actual remote branch. Method 2 is recommended

git-tag label management

git中tag

  • Definition: It is to mark the commit, which is equivalent to an alias.
  • Purpose: Through tag, when the development reaches a stage, in order to highlight the importance of this submission, it can be tagged. For example, tagging release nodes v1.0, v2.0, etc.
  • Significance: Marks a related commit phase for future reference. Mark the release node for project release.

Tag creation command:

  • Tag the latest commit: git tag [tag information]
  • Specify the commit tag: git tag [tag information] commit id number
  • Comment label: Specify the commit to label and add comment information: git tag [tag information] -a -m [comment information]
    Tag view command:
  • View tags in all commits: git tag
  • View detailed label information in commit: git show [label information]

Tag's push remote library command:

  • Push one or more commit commands with label information: git push [name of remote library] [label information 1] [label information 2]
  • The command to push all tags to the remote library: git push [name of the remote library] --tags
    Note: When git push pushes the code, the tag will not be pushed, and the tag needs to be pushed separately

tag's delete tag:

  • Delete local tags: git tag -d [tag information]
  • Delete remote tags: git push [name of remote repository] -d [tag info 1] [tag info 2]
    releases

github warehouse fork

fork: Quickly copy other people's warehouses in github, and contribute to the original warehouse through PR (pull request)
Steps:

  1. In the github URL where you want to copy someone's warehouse, click fork to quickly copy and create your own warehouse
  2. The address of the local git clone own warehouse
  3. Code modification, git add . git commit -m “**” git push
  4. You can contribute your modified code to the original warehouse through PR

Links for reference:

Guess you like

Origin blog.csdn.net/qq_42178122/article/details/130214168