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
- 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
- Commit to the Git repository
- You must write the submission information
git commit -m "Submit information"
Copy the code
- Note: If
git commit
, forget-m
- English input method, input
:q!
Press Enter - To perform
Git commit -m "commit information"
- English input method, input
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
- Find the target version to roll back to
git log
Copy the code
- use
git reset --hard ID
Return to target version
git reset --hard a956f2ae5
Copy the code
Return to the latest version
- use
git reflog
Find the id of the latest version
git reflog
Copy the code
- use
git reset --hard ID
Return to target version
git reset --hard 6f535f0
Copy the code
Open source licenses
- 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
- Initialize the local repository
git init
Copy the code
- 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
-
Create an empty repository on GitHub
-
Associate the local repository with the remote repository
-
Push the code to the remote repository
git push -u origin master
Copy the code
Git to strengthen
- 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
- 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:
- Modify the conflicting files to determine the fixed code
- perform
git add .
Commit the conflict-fixed file to the staging area - perform
Git 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
- 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
- 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
- new
Pull Request
- Merge function branch code requests into
master
branch - Boss Ait to do Code Review.
- After the code is tested by the reviewer, it is merged into
master
branch