– use the Git
Git configuration
Set the signature
The project (repository) level is only valid for the current local repository
Git config user.email [email protected]Copy the code
The system user level is valid only for the current login operating system user
git config --global user.name tom git config --global user.email [email protected] Copy the code
Priority: Project level > System level
Information save location: ~/. Gitconfig file
When we have files that don’t need to be uploaded to the online repository, such as the NPM package (node_modules) in our project, it is very important in our project, but it also takes up a lot of memory, so we generally don’t need to add NPM package when using Git management. Gitignore file is created in the project and node_modules/ is written to it. Git will automatically ignore all the.gitignore writing rules for node_modules
Git has four key areas
Git operation process includes four key areas: workspace, Index, local Repository and Remote.
- Workspace A place on the local computer where project files are stored
- The staging area is where files are temporarily stored, usually with the add command to add workspace files to the staging area
- Local repository
Commit the staging area to the master branch with a commit command. This means that a version has been made and the code has been committed to the local repository. Git servers on remote repository lines, such as GitLab, Github, and Gitee, are a remote repository
Git operation instructions
I. Work area
1. Create a warehouse
Configure projects in your workspace with Git for management and initialize projects with Git
Git init Note: The generated.git directory stores local library files. Do not delete themCopy the code
Pull items from remote warehouse:
git clone <url>
Copy the code
2. Submit documents to the staging area
Submit all files in your workspace to the staging area
git add .
Copy the code
Submits the workspace specified file to the staging area
git add <file 0> <file 1> ..... <file n>Copy the code
Submits the workspace specified folder to the staging area
git add [dir]
Copy the code
3. Cancel the deletion
Undo and discard workspace changes. Return the file to its last git commit or git add state.
git checkout --<file>
Copy the code
Delete both workspace and staging files
git rm <file1> <file2>
Copy the code
To cancel files that have been temporarily stored in the staging area:
git reset HEAD <file>...
Copy the code
3. View information
Query the status of all files in the current workspace
git status
Copy the code
Compares the difference between the current file and the staging area in the workspace
git diff
Copy the code
Ii. Temporary storage area
1. Submit files to the version library
Commit the changes
git commit -m [message]
Copy the code
Submits the file specified in the staging area to the current branch in the local repository
git commit [file1] [file2] ... -m [message]
Copy the code
2. View information
Compares the difference between the staging area and the previous version
git diff --cached
Copy the code
Compares the differences between the file specified in the staging area and the previous version
git diff <file-name> --cached
Copy the code
Viewing Historical Records
Git reflog # git reflog # git reflog # git reflog # git reflog # -- Pretty =oneline #Copy the code
3. Branch management
Create a branch
git branch <branch-name>
Copy the code
Switch from the current branch to another branch
git checkout <branch-name>
Copy the code
Delete the branch
git branch -d <branch-name>
Copy the code
Specifies the branch to be merged into the current branch
Git mergeCopy the code
Displays all branches of the local repository
git branch
Copy the code
4. Conflict resolution
- Step 1: Edit and delete special marks
<<<
= = =
- Step 2: Modify to a satisfactory position, save and exit
- Step 3: Add to cache
Git add file name
- Step 4: Commit to the local library
Git commit -m
Note: must not be followed by a file name
Local warehouse
Get remote warehouse information
Git fetch < remote host name >Copy the code
Push a branch of a local repository to a remote repository
git push [remote-name] [branch-name]
Copy the code
View details about the remote repository
git remote show origin
Copy the code
Retrieves updates from a branch of the remote host and merges them with the specified branch of the local host
git pull
Copy the code
Branches in Git
Git HEAD and master
In Git, we can think of HEAD as a pointer to our development branch. As shown in the figure below, when we are currently in the Master branch, HEAD points to the Master branch.
At this point, when we checkout to the dev branch, the HEAD pointer points to the dev pointer.
git checkout dev
git branch
* dev
master
Copy the code
When we make a development commit on dev, dev points to the latest commit of the current branch, while the master branch remains as committed as before
When we merge dev onto master, Git points master to dev’s current commit to complete the merge
git checkout master
git merge dev
Copy the code
Git fetch + git rebase = git pull
Git fetch + git merge (git rebase) = git pull
When the location of the online master is submitted along with someone else’s code file. Online master will no longer correspond to local orgin/master. The local master is also not in the same branch as the online master
The target
To solve this problem, we need to change the local development branch to the following
We can either use Git pull to fetch a branch update from a remote host and merge it with a local branch, i.e. iterating the remote repository version directly to the local repository and the remote repository associated library, or we can use the following methods to accomplish our goal. The problem with Git pull is that it hides all the details of the process so that you don’t have to know the difference between the various types of branches in Git and how to use them. Of course, most of the time this is fine, but when there is a problem with the code, don’t rush to find what went wrong. Git Fetch is recommended because local working directories are updated by remote branches without confirmation
Git fetch + git rebase
After git fetch, the local workspace will not automatically generate an editable copy, and the captured results will be stored in the Repository, namely version A1 and A2 in the figure
When you merge git into git rebase, the same effect will occur, and the branch master will not be merged