This article has participated in the third “topic writing” track at the Diggings Creators Camp. For more details, check out diggings program | Creators Camp.

Introduction of Git

Git is an open source distributed version control system that can handle versioning from small to very large projects efficiently and quickly. Is an open source version control software developed by Linus Torvalds to help manage Linux kernel development. It’s fast, it’s perfect for managing large projects, and it has an incredibly nonlinear branching management system.

Git Workflow Example

Workspace: it is the place where the development changes are usually carried out. It is the latest content that is currently seen. During the development process, it is also the operation of the Workspace

Index: When the git add command is executed, the files in the workspace will be moved to the staging area. The staging area marks the contents in the current workspace that are managed by Git. When the code needs to be submitted after completing a requirement or function, the first step is to submit the code to the staging area through Git add.

Repository: it is located on your own computer and commits the contents of your staging area to a local Repository through Git Commit.

Remote: the server used to host code. The contents of the Remote repository can be modified by local repositories distributed in multiple locations in a collaborative relationship. After the local repository has modified the code, the code is synchronized to the Remote repository using git push commands.

Basic Git operations

Configure Git and initialize repository

  1. Configure Git account information
git config --global user.name "userName"
git config --global user.email "userEmail"
Copy the code
  1. Create a new directory (repository) and initialize it
mkdir demo
cd demo
git init
Copy the code

Clone Remote warehouse

// Clone a remote repository to a local directory. By default, create a folder with the same name as the repository nameGit Clone code repository addressCopy the code

Create a branch

// 1. Check the warehouse status
git status

// 2. Create the dev branch and select it
git checkout -b dev
Copy the code

Create a file, commit, roll back, and view logs

// 0. Create the 'readme.md' file in the 'dev' branch of the demo folder
// 1. Check the warehouse status: at this time, a message should be displayed indicating that a file needs to be added to the file cache
git status
 
// 2. Add the modified file to the file cache
git add readme.md
 
// 3. Check the repository status: a message is displayed indicating that a file needs to be committed
git status
 
// 4. Add files from the file cache to the repository (i.e. update the change point to the corresponding branch)
git commmit -m "Add readme.md file"
 
// 6. Modify the file and repeat the preceding operations
// The branch of the current empty file is activated because of the commit
git branch
git status
git add readme.md
git commit -m "Readme. md has been modified"
git status
 
// 7. Code rollback: If the previous commit error, you can use the file rollback operation to return to the state of the last commit
git reset --hard HEAD~1
 
// 8. Remove any changes to the file cache after the rollback
git checkout readme.md
git status
 
// 9. To modify multiple files at a time, run the following command in batches
git add .
git checkout .
 
// 10. After a number of commits and updates have been committed, you can use the git log command to view the operation history
git log
 
// 11. Git reflog allows you to view all operations on all branches
git reflog
 
// 12. Check the Git log in webstorm
File -> git -> show History -> log
Copy the code

A local Git repository associates with a remote repository

// 1. The local repository is associated with the remote repository
git remote add origin http://jcode.cbpmgt.com/git/weus-git-demo.git
 
// 3. Pull the remote branch code
git pull orign dev
 
// 4. Modify the code and update it to the local
git add .
git commit -m "Modify code description"
 
// 5. Upload local code to remote branch
git push orign dev
 
// 6. You can directly use git pull and git push to operate remote branch code
git pull
// Change the code
git add .
git commit -m "Change point description"
git push
 
// 7. Push local code to remote master branch requires permission
// 8. After associating the remote repository, it is better to change the name of the local folder to be the same as that of the repository
Copy the code

There are parts of the code in the remote repository that do not allow directly overwriting the local code

// 1. Update all remote branches to the local
git fetch  
 
// 2. The updated content is merged with the local
git merge
 
// 3. Merge 错 误 : fatal: refusing to merge suggested histories
git pull origin master --allow-unrelated-histories
 
// 4. Merge again
git merge
 
// 5. Merge error: Please, commit your changes before you merge
git add .
git commit -m "Submit information"
git merge
 
// 6. Pull remote branch code
// In this case, the local and remote branch code is the same
git pull
 
// 7. Update local code to remote branchGit push orign Remote branch nameCopy the code

Resolving code conflicts

Scenario: Create two directories to simulate code conflicts, which are more common when multiple people are working together.

// 1. Clone remote branch code to another local directory
git clone http://jcode.cbpmgt.com/git/weus-git-demo.git
 
// 2. Modify the contents of the readme.md file and push to the remote branch
git status
git add .
git commit -m "Code Content Modification Description"
git push
 
// 3. Go to the original project folder and modify the same line as the text in Step 2
git status
git add .
git commit -m "Code Content Modification Description"
 
// 4. Pull the remote branch code
git pull
 
// 5. The conflict error is reported and the conflict is resolved
// Modify the code manually according to the conflict
git add .
git commit -m "Modify code description"
 
// 6. Pull the code again after resolving the conflict
git pull
 
// 7. Push the latest code after resolving the conflict
git push
Copy the code

Praise support, hand stay fragrance, and have glory yan, move your small hand to make a fortune yo, thank you for leaving your footprints.

Past excellent recommendation

Front-end performance optimization combat

Talk about regular expressions

Obtain the file BLOB stream address to download the function

Git

How do I use Git at work

Git implements automatic push

Interview recommendations

Front ten thousand literal classics – basic

Front swastika area – advanced chapter

More wonderful details: personal home page