Git Basics

Git user configuration (low frequency)

Configuring a User Name

git config --global user.name "User name"
Copy the code

Configure your email

git config --global user.email "Email"
Copy the code

Viewing global Configuration

git config --global -l
Copy the code

Three areas of Git

  • The workspace
  • The staging area
  • warehouse

Four states of Git files

  • Untracked not tracking
  • Unmodified Unmodified
  • Modified has been Modified
  • Staged is temporary

Initialize the Git repository

git init
Copy the code

View the current repository file status

git status
git status -s
Copy the code

Commit the file to the Git repository

  1. To the staging area
  • Untracked files are added to the staging area
  • The modified file is added to the staging area
Git add file name.Copy the code
  • Tips: “.” Git add. is the current directory, so git add. is to add all files from the current directory that need to be added to the staging area

  1. Commit to the Git repository
  • You must write the submission information
git commit -m "Submit information"
Copy the code
  • Note: Ifgit commit, forget-m
    1. English input method, input:q!Press Enter
    2. To performGit commit -m "commit information"

Skip the staging area and commit directly to the Git repository

git commit -am "Submit information"
Copy the code

Note: **git commit -am** can only commit tracked files, untracked files, must first **git add.**

Viewing the Commit Log

View details about all logs

git log
Copy the code

View the last five commit logs

git log- 5Copy the code

Log information is displayed in a single line

git log --oneline
Copy the code

Version back

  1. Find the target version to roll back to
git log
Copy the code
  1. usegit reset --hard IDReturn to target version
git reset --hard a956f2ae5
Copy the code

Return to the latest version

  1. usegit reflogFind the id of the latest version
git reflog
Copy the code
  1. usegit reset --hard IDReturn to target version
git reset --hard 6f535f0
Copy the code

Open source licenses

  1. GPL

Infectious open source protocol, as long as the GPL use of the project, the modification of the derivative project, must be open source and not commercially available. 2. MIT’s very friendly agreement for commercial projects, with almost no restrictions, except that the author’s source can be noted when using the project.

Local repository pushed to GitHub

  1. Initialize the local repository
git init
Copy the code
  1. Submit all files locally to the repository
  • Note:**git commit -am**Only tracked files can be submitted. Untracked files must be submitted first**git add .**
git add .
git commit -m "Submit information"
Copy the code
  1. Create an empty repository on GitHub

  2. Associate the local repository with the remote repository

  3. Push the code to the remote repository

git push -u origin master
Copy the code

Git to strengthen

  1. There is always a bug when submitting an upload
  • Repository not initialized when code was uploaded

  • The repository was initialized, but no code was committed

  • The user name or password is incorrect

    • Username for gitee.com: indicates the username

    • Password for gitee.com: Enter the password

  • If an incorrect password is entered for the first time, the password is automatically recorded. How do I clear the recorded password?

git config --system --unset credential.helper
Copy the code

Git push command

Function: Push local repository to remote (code not committed to repository will not be pushed)

Do not directly git push the new code

You should commit to a local repository before pushing to remote

Features:

Only the first push to the remote repository needs to use git push-u Origin master

In the future, you can use git push directly

Use SSH to submit the code

Example Generate an SSH secret key

ssh-keygen -t rsa -b 4096 -C "GitHub or Code Cloud registered email address"
Copy the code

Configure the SSH key on GitHub or the code cloud

  1. The SSH public key file is found
C:\ user directory \.ssh\id_rsa.pubCopy the code

Git clone command

Download the remote warehouse to the local and clone the whole warehouse

git cloneThe address of the repository (either HTTPS or SSH)Copy the code

Tips: When you download code using the **git Clone ** command, the protocol you select determines the protocol you will use to submit the code. Therefore, cloning using SSH is recommended.

branch

Rule: Do not commit code directly to the master branch. You need to create a new branch and complete the function before merging it into the master branch.

Basic branch operations

View all branches

git branch
Copy the code

Create a branch

Tips: Always create a branch based on the master branch

Git Branch nameCopy the code

Switch branch

Git Checkout branch nameCopy the code

Create and switch branches

Git checkout -b Branch nameCopy the code

Merging branches

Git Checkout Master Git mergeCopy the code

Delete the branch

Git branch -d Specifies the name of a branchCopy the code

**Tips: Forcibly delete a branch (use with caution) ****git branch -d Branch name **

Conflict resolution

Conflict: When two branches modify the same file at the same time, a conflict occurs when merging, and the latter resolves the conflict, so be sure to commit and merge the code in time.

When a conflict occurs, the resolution process is as follows:

  1. Modify the conflicting files to determine the fixed code
  2. performgit add .Commit the conflict-fixed file to the staging area
  3. performGit commit -m "commit information"Commit the conflict-fixed content to the repository

Key words: conflict

Push the local branch to remote

You only need to run git push command in the function branch, the terminal will give the corresponding prompt, copy and paste run, and then push to the remote

$ git push
fatal: The current branch pay has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin pay
Copy the code

The git pull command

Function: Pull the remote repository code to the local, from the server to obtain the latest code

When you’re developing, you do git pull first thing in the morning

Extra-curricular extension

  1. Change the repository commit mode from HTTPS to SSH

Git remote is a command to view and configure a Git remote repository. Check the remote repository address associated with the current repository. Delete the associated remote repository address

git remote -v
Copy the code
Git Remote Remove Repository nameCopy the code
Git Remote Add Repository name Repository addressCopy the code
  1. The real Git development process

Rule: The Master branch cannot commit code directly, so the Git merge command is meaningless

  • Create function branch, write code, submit to function branch
  • Push the feature branch to the remote repository
  • newPull Request
  • Merge function branch code requests intomasterbranch
  • Boss Ait to do Code Review.
  • After the code is tested by the reviewer, it is merged intomasterbranch