Git all things

0. Commonly used basic instructions

Summary:
Common commands:
git add . Add files to the temporary storage area
git commit -m 'Describe the modified content' Temporarily add to the local warehouse
git status Query the modified status
git log View log
git reflog Version rollback
git reset commitID - -hard One way to roll back to the previous version is
git reset --hard HEAD^ Another way is
git reset --hard HEAD^^ To roll back to the previous version
git branch View
branch git branch Branch name Create a certain branch
git checkout branch name switches to a certain branch
git checkout -b branch name directly switches to a non-existent branch (create and switch)
git merge merges branches
The current branch cannot be deleted, only other branches can be deleted
git branch -d b1 When deleting a branch, Need to do various checks
git branch -D b1 does not do any checks, forcefully delete
git remote View remote warehouse
git push [-f] [–set-upstream] [remote name [local branch name] [: remote branch name] ]
If the remote branch name is the same as the local branch name, you can only write the local branch
git push origin master. Generally, the default is this
git branch -vv to view the relationship between the local branch and the remote branch.
git fetch [remote name] [branch name] Fetch command to grab The fetch command is to grab all the updates in the warehouse to the local without merging. If the remote name and branch name are not specified, all branches will be fetched.
git pull [remote name] [branch name] The pull command is Pull the modifications from the remote warehouse to the local and automatically merge them, which is equivalent to fetch+merge. If the remote name and branch name are not specified, all the changes will be fetched and the current branch will be updated.

1.Basic configuration

tips:

  • Git GUI: the graphical interface tool provided by Git
  • Git Bash: command line tool provided by Git

1.1Basic configuration

1. Open Git Bash
2. Set user information

git config --global user.name "username"
git config --global user.email "email address"

View configuration information
git config --global user.name
git config --global user.email

1.2 Configure aliases for commonly used instructions (optional)

There are many commonly used instructions, and many parameters need to be entered every time. We can use aliases.
1. Open the user directory and create a .bashrc file
touch ~/.bashrc
2. There will be a .bashrc file in the user's computer. Enter the following content in the .bashrc file
Insert image description here

#Used to input git commit log
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'
#Used to output all files and basic information in the current directory
alias ll='ls -al '

1.3 Solve the GitBash garbled problem

1. Open GitBash and execute the following command

  • git config --global core.quotepath false
  1. Add the following two lines to the ${git_home}/etc/bash.bashrc file in the etc/bash.bashrc directory of the git installation directory.
  • export LANG=“zh_CN.UTF-8”
  • export LC_ALL=“zh_CN.UTF-8”

2. Get the local warehouse

To use Git to version control our code, we first need to obtain the local repository

  1. Create an empty directory anywhere on the computer as our local Git repository
    2) Enter this directory and right-click to open the GitBash window
    3) Execute the command git init
    4) If the creation is successful, you can see the hidden .git in the folder Table of contents
    Insert image description here

3.Basic operating instructions

There will be several states for file modification (add, delete, update) in the Git working directory, and the status of these modifications will change as we execute Git commands. How to use commands to control the transition between
Insert image description here
these states

  • 1.git add (workspace–>staging area)
  • 2.git commit (staging area–>local warehouse)

3.1 Check the modified status (status)

  • Function: View the modified status (temporary storage area. work area)
  • Command form: git status

3.2 Add the workspace to the staging area (add)

  • Function: Add modifications to one or more files in the workspace to the staging area
  • Command execution: git add single file name | wildcard
    * Add all modifications to the staging area: git add.

3.3 View the submission log (log)

Because we have configured the alias git-log before, it contains these parameters. All subsequent instructions can be used directly using the command git-log.

  • Function: View submission records
  • Command form: git log [option]
    * options
    * --all Display all branches
    * --pretty=online Display commit information as one line
    * --abbrev-commit Make the output commitld shorter
    * --graph Display it in the form of a graph

3.4 version rollback

  • Function: version switching
  • Command form: git reset --hard commitID
    * commitID can be viewed using the git-log or git log command
  • How to view deleted records
    *git reflog This command can see deleted commit records

3.5 Add files to ignore list

Generally, we will always have some files that do not need to be included in Git management, and we do not want them to always appear in the untracked file list. Usually they are automatically generated files, such as log files, or temporary files created during the compilation process. In this case case, we can create a file named .gitinore in the working directory (the file name is fixed), listing the file patterns to be ignored. Here is an example:
Insert image description here

Exercise: basic operations



  • Create a directory (git_test01) and open gitbash in the directory
  • Initialize the creation library
    git init

  • Create file file01.txt in the directory
    touch file01.txt
  • Add the modifications to the staging area
    git add.
  • Submit the modifications to the local warehouse. The submission record content is: commit 001. This is the comment content
    git commit -m 'commit 001'
  • View log
    git log

  • Modify the content of file01 to: count=1
    vim file01
  • Add the modifications to the staging area
    git add.
  • Submit the modifications to the local warehouse, and the submission record content is: update file01
    git commit -m 'update file01'
  • View log
    git log

  • Display commit records git-log in a streamlined manner

  • View commit records
    git-log

  • Find the commitID git reflog of the second to last submission
  • Version rollback
    git reset commitID --hard
  • Go back to the previous version
    git reset --hard HEAD^
  • Go back to the previous version
    git reset --hard HEAD^^

4 branches

Almost all version control systems support branches in some form. Using branches means that you can separate your work from the main development line to make major bug modifications or develop new features without affecting the main development line.

4.1 View local branches

  • Command: git branch

4.2 Create a local branch

  • Command: git branch branch name

4.3 Switch branches (checkout)

  • Command: git checkout branch name
    We can also directly switch to a non-existent branch (create and switch)
  • Command: git checkout -b branch name

4.4 Merge branches (merge)

Commits on one branch can be merged into another branch
with the command: git merge branch name

4.5 Delete branch

You cannot delete the current branch, you can only delete other branches.
git branch -d b1 When deleting a branch, you need to do various checks.
git branch -D b1 does not do any checks, force deletion.

4.6 Resolving conflicts

When there may be conflicts in the modification of files on two branches, for example, the same line of the same file is modified at the same time, this requires manual resolution of the conflict. The steps to resolve the conflict are as follows: 1. Process the conflict in the file
2.
Resolve Add the conflicting files to the temporary storage area (add)
3. Submit the conflicting content to the warehouse (commit)
and process the conflicting content as follows:
Insert image description here

4.7 Principles and processes for developing summary branches

Almost all version control systems support branches in some form. Using branches means that you can separate your work from the main development line to make major bug modifications and develop new features so as not to affect the main development line during development
. Generally, there are the following principles and processes for branch usage:

  • master (production) branch
    , online branch, main branch, branch corresponding to applications running online for small and medium-sized projects
  • The develop (development) branch
    is a branch created from master. It is generally used as the main development branch of the development part. If there are no other parallel development requirements for different online phases, development can be carried out in this version. After the stage development is completed, it needs to be merged into the master branch. , ready to go online
  • feature/xxxx branch
    is a branch created from develop. It is usually developed in parallel at the same time, but is created when different phases are launched. After the R&D tasks on the branch are completed, they are merged into the develop branch.
  • hotfix/xxxx branch
    is a branch derived from master. It is generally used for online bug repair. After the repair is completed, it needs to be merged into the master.test.develop branch.
  • There are also some other branches, which will not be detailed here, such as test branch (user code testing), pre branch (pre-launch branch), etc.
    Insert image description here
    Exercise: Branch Operation

  • [master]Create branch dev01
    git branch dev01
  • [master] switch to dev01
    git checkout dev01
  • [dev01]Create file file02.txt
    touch file02.txt
  • [dev01] Add the modification to the temporary storage area and submit it to the warehouse. The submission record content is: add file02 on dev
    git add .
    git commit -m 'add file02 on dev'

  • [dev01] Display commit records git-log in a streamlined manner

  • [dev01] Switch to the master branch
    gie checkout master
  • [master] Merge dev01 to the master branch
    git merge dev01

  • [master] Display the commit record git-log in a streamlined manner
    *[master] View file changes (file02.txt also appears in the directory)
    ll

  • [master] Delete the dev01 branch
    git branch -d dev01
  • [master] Display commit records in a streamlined manner

5. Git remote warehouse

5.1 Commonly used hosting services (remote warehouse)

  • GitHub (Address: https://github.com/) is a hosting platform for open source and private software projects. Because it only supports Git as the only version library format for hosting, it is named GitHub.
  • Code Cloud (Address: https://gitee.com/) is a domestic code hosting platform. Since the server is in China, Code Cloud is faster than GitHub.
  • GitLab (address "https://about.gitlab.com/") is an open source project for warehouse management systems. It uses Git as a code management tool and builds web services on this basis. It is generally used in enterprises. Build a git private server for internal networks such as schools

5.2 code cloud

Insert image description here
After the warehouse is created, you can see the warehouse address, as shown in the figure below:
Insert image description here
Insert image description here

5.3 Configure SSH public key

Generate SSH public key

  • ssh-keygen -t rsa
  • Keep pressing Enter. If the public key already exists, it will automatically overwrite
    the Gitee setting account public key.
  • Get the public key
    cat ~/.ssh/id_rsa.pub
    Insert image description here
  • Verify whether the configuration is successful
    ssh -T [email protected]

5.4 Add remote warehouse

This operation is to initialize the local library first and then connect it to the created remote library.

  • Command git remote add <remote name> <warehouse path>
    1. Remote name, the default is origin, depends on the remote server settings
    2. Warehouse path, get this URL from the remote server
    3. For example: git remote add origin git @gitee.com:yue-haichao/develop-tools.git

5.5 View remote warehouse

  • Command:git remote

5.6 Push to remote warehouse

  • Command: git push [-f] [–set-upstream] [remote name [local branch name] [: remote branch name]] If the remote
    branch name and the local branch name are the same, you can only write the local branch
    git push origin master
    Supplement:
    1. if means forced coverage
    2. --set-upstream pushes to the remote end and establishes an association with the remote branch.
    git push --set-upstream origin master
    3. If the current branch is already linked to the remote end For branch association, you can omit the branch name and remote name.
    git push will push the master branch to the associated remote branch.

5.7 The relationship between local branches and remote branches

To view the relationship, we can use the git branch -vv command

5.8 Clone from remote repository

If there is already a remote repository, we can directly clone it locally

  • Command: git clone <warehouse path> []local directory]
    The local directory can be omitted, and a directory will be automatically generated.
    Insert image description here

5.9 Crawl and pull from remote repositories

The remote branch is the same as the local branch. We can perform the merge operation. We just need to download all the updates in the remote warehouse to the local first and then perform the operation.

  • Fetch command: git fetch [remote name] [branch name]

    The fetch command is to fetch all updates in the warehouse locally without merging them.
    If the remote name and branch name are not specified, all branches will be fetched.

  • Pull command: git pull [remote name] [branch name]

    The pull command is to pull the modifications of the remote warehouse to the local and automatically merge them. It is equivalent to fetch+merge.
    If the remote name and branch name are not specified, all the changes will be fetched and the current branch will be updated.

  1. Make a commit in the local warehouse test01 and push it to the remote warehouse

These are the two problems I have
Insert image description here
Insert image description here
Insert image description here

5.10 Resolving merge conflicts

Within a period of time, users A and B modify the same file. At this time, a merge conflict will occur. After user A modifies the code locally, it will be pushed to the remote warehouse first. At this time, user B modifies the code locally and submits it to the local warehouse. It also needs to be pushed to the remote warehouse. At this time, user B is later than user A, so it is necessary to pull the submission of user A in the remote warehouse first, and then push it to the remote branch after merging it, as shown in the following figure: that is to say, git
merge This command is combined with gir pull to make
the remote branch the same as the local branch...


Exercise: Remote warehouse operation
1.

  • [git_test01]Add remote warehouse

git remote add origin + the address copied by ssh on code cloud

  • [git_test01] Push the master branch to the remote warehouse and bind the association with the master branch of the remote warehouse

git push --set-upstream origin master


2. Clone the remote repository to local

  • Clone the remote repository to the local git_test02 directory

git clone ssh address git_test02 on code cloud

  • [git_test02] Display commit records in a streamlined manner

git-log


3. Push local modifications to the remote warehouse

  • [git_test01] Create file file03.txt

touch file03.txt

  • [git_test01] Add the modification to the temporary storage area and submit it to the warehouse. The submission record is add file03

git add
git commit -m ‘add file03’

  • [git_test01] Push the modifications of the master branch to the remote warehouse

git push origin master


4. Update the changes in the remote warehouse to the local one

  • [git_test02] Modify the remote warehouse and then pull it to the local

git pull

  • Display commit records in a streamlined manner

git-log

  • View file changes

ll

6. Use Git in ideas

After IDEA is opened, click Settings to search for git. Select the Git installation path to configure the git installation path.
Click the Test button. Now the execution is successful, indicating that the configuration is successful.
Most of them are executed by pressing keys. Idea weakens the concept of cache area.
First pull to synchronize the code, and then push.

6.1 Initialize the local warehouse

Insert image description here
Insert image description here

6.3 Submit to local warehouse

Submit to local warehouse, click
Insert image description here

Push to remote warehouse
Insert image description here

6.4 Clone the remote repository to local

Insert image description here
Insert image description here
IDEA commonly used GIT operation entrance
Insert image description here
Insert image description here
Insert image description here
Reference: Dark horse programmer Mr. Zhang Meng

Guess you like

Origin blog.csdn.net/tyloonulinuli/article/details/123127964