Git common operation study notes

Git workflow diagram

insert image description here

Git work commands

  1. clone (clone): clone the code from the remote repository to the local repository
  2. checkout: Check out a repository branch from the local repository and make revisions
  3. add (add): commit the code to the staging area before committing
  4. commit (commit): Submit to the local repository. Save each historical version of the modification in the local repository
  5. fetch (fetch): From the remote repository, grab it to the local repository, without any merging action, and the general operation is relatively small.
  6. Pull (pull): Pull from the remote library to the local library, automatically merge (merge), and then put it into the workspace, which is equivalent to
    fetch+merge
  7. push (push): After the modification is completed, when the code needs to be shared with team members, push the code to the remote warehouse

Warehouse Staging Area Workspace Relationship

insert image description here

Related commands

  1. git add Add changes to one or more files in the workspace to the staging area
    Add all changes to the staging area: git add .

  2. git commit Submit the content of the staging area to the current branch of the local warehouse
    git commit -m 'comment content'

  3. The status of the modifications viewed by git status (staging area, workspace)

  4. git log [option] View commit records
    options --all
    show all branches
    --pretty=oneline display commit information as one line
    --abbrev-commit make the output commitId shorter
    --graph display in the form of a graph

  5. git reset --hard commitID version switch
    commitID can be viewed using git-log or git log command

  6. git reflog This command can see the commit record that has been deleted

branch

  1. View local branch
    Command: git branch

  2. Create a local branch
    Command: git branch branch name
    Switch directly to a non-existing branch (create and switch): git checkout -b branch name
    3.== *Switch branch (checkout) ==
    command: git checkout branch name
    We can also Switch directly to a non-existing branch (create and switch)
    command: git checkout -b branch name

  3. == *Merge branches (merge) ==
    Commits on one branch can be merged into another branch
    Command: git merge branch name

  4. Delete branch
    The current branch cannot be deleted, only other branches can be deleted.
    git branch -d b1 When deleting a branch, various checks need to be done
    .

  5. Conflict resolution
    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, then the conflict needs to be resolved manually . The
    steps to resolve the conflict are as follows:

     1.处理文件中冲突的地方
     2.将解决完冲突的文件加入暂存区(add)
     3.提交到仓库(commit)
    

branch usage policy

  • master (production) branch
    Online branch, main branch, small and medium-scale projects as the branch corresponding to the application running online;
  • The develop (development) branch
    is a branch created from the master. It is generally used as the main development branch of the development department. If there are no other parallel development
    requirements for different phases, it can be developed in this version. After the phase development is completed, it needs to be merged into the master branch. , ready to go online.
  • feature/xxxx branches Branches
    created from develop are generally developed in parallel in the same period, but are created when the branch is launched in different periods. After the research and development tasks on the branch are completed , they are
    merged into the develop branch.
  • hotfix/xxxx branches, branches
    derived from master, are generally used as online bug fixes. After the fixes are completed, they need to be merged into master, test,
  • develop branch.
    There are some other branches, which will not be described in detail here, such as test branch (for code testing), pre branch (pre-launch branch) and so
    on.
    insert image description here

remote warehouse

Add remote repository

git remote add <远端名称> <仓库地址>
  • Remote name: the default is origin, depending on the remote server settings
  • Warehouse address: Get this url from the remote server

View remote repository

git remote

push to remote repository

git push [-f] [--set-upstream] [远端名称] [本地分支名][:远端分支名]

If the remote branch name is the same as the local branch name, you can just write the local branch name

clone from remote repository

git clone <仓库地址> [本地目录]

If the local directory is omitted, a directory will be automatically generated

Fetch and pull from remote repository

grab

The fetch instruction is to fetch 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 fetch [remote name] [branch name]

Pull

The pull instruction is to pull the modifications of 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 current branches will be fetched and updated.

git pull [remote name] [branch name]

View the binding relationship between the local branch and the remote branch

git branch -vv

If there is no binding relationship , bind
the local branch master to the remote branch master

git push --set-upstream origin master:master

Using Git in IDEA

New warehouse

insert image description here

Initialize the local repository

insert image description here

select item

insert image description here

Enter remote warehouse item

insert image description here

Submit to local repository

insert image description here

push to remote repository

insert image description here

create branch

insert image description here
insert image description here

Off topic

This article is used by me to record my learning and review it later.
This article is well written. Part of this article is an excerpt from
Kisugi Takumi's Git notes here.

Guess you like

Origin blog.csdn.net/qq_47431361/article/details/122873717