This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

When it comes to Git, let’s take a look at version control and the version control tool 😋

Version control for individual development improvement iterations and team collaboration, version control tools should have the following capabilities: collaborative change, data backup, version management, permission control, history, and branch management. Next, enter the body:

Introduction of Git 📜

Git is a distributed version control tool developed by Linus, the father of Linux, who reportedly wrote it in just two weeks. The Git structure can be divided into a workspace for writing code, a staging area for temporary storage, and a local repository for historical releases. (Figure below)

Git and the code hosting center 🏠

The code hosting center is used to maintain remote libraries, which can be GitLab server in a LAN environment, or GitHub or code cloud in an extranet environment.

Git command line basic operations ✨

1. Local library operations

Local library initialization

Git init, set the signature. The signature is divided into project level/repository level (valid only in the current local library scope), system user level (valid only in the current operating system scope), the latter is set only in the former basis of the add a “–global” parameter.

Git init git config user.name Git config user.email Git config --globalUser. name Git config --globalUser. email Indicates the email addressCopy the code

Note: The signature is only for identification purposes. The name and email are not related to the username and email address registered on GitHub.

View the status of working area and temporary storage area

git status
Copy the code

Adds workspace new/changes to the staging area

Git add file nameCopy the code

(Side note: After “vim file name” operation, enter the file, click “I” to enter the editing state, and then edit the file freely. When exiting, press ESC and enter “:wq”.)

Commit the staging area contents to the local library

If you add the file name directly to git commit, you will open the corresponding file and enter the commit description in the first line.

Git commit -m Specifies the name of the commit description fileCopy the code

Viewing Historical Records

git log
Copy the code

You can add three parameters — pretty=oneline/ — oneline or use git reflog.

Forward and backward historical versions

  • Git reset –hard with a partial index value (available with git reflog)
  • Git reset –hard HEAD git reset –hard HEAD
  • Git reset –hard HEAD

Delete the file

The rm file nameCopy the code

Delete files and retrieve them

The premise is that the existing state of the file is committed to the staging area.

Git reset --hardCopy the code

The pointer position points to the history record if the file deletion operation has been committed to the local library, or to the HEAD if it has not.

Compare the file

Git diff file name// Compares workspace files with staging filesGit diff local library historical version file name// Compares workspace files with local library history
Copy the code

If git diff is not followed by a file name, it will compare as many files as there are in the directory.

Branch management

View branches:

git branch -v
Copy the code

Create a branch:

Git branch Specifies the branch nameCopy the code

Switch branches:

Git Checkout branch nameCopy the code

Merge branches:

Git merge creates a new branch name to resolve any conflicts in the merge: open the conflicting file, remove any special symbols in it, and change it to the version you want, then commit.

2. Remote library operations

Team collaborative development or individual iterative development

Start by logging in to GitHub to create a remote repository

Then copy the remote library address,

Go to the command line and use git remote add to create an alias for the remote repository address. Git push branch: Git push remote library alias name of the branch to push

The clone operation has three functions, including downloading the remote repository locally, initializing it, and creating the Origin remote alias. Note that the rest of the team must be invited to push through the project’s Settings->Manage Access ->Invite->Collaborator on GitHub.

Pull operation: The pull operation is equivalent to the fetch operation plus the merge operation, that is: “Git pull” = “git fetch” + “git merge” = “git fetch” Then check the contents of the drop down branches and perform the merge operation.)

Resolve co-development conflicts: If the changes are not based on the latest version of the GitHub remote library, the push operation cannot be performed and must be pulled first. If the pull is in a conflict state, follow the “branch conflict resolution” operation described earlier.

Collaboration across teams

People outside the team will log in to their GitHub account, enter the project address to search, and then click Fork. The project will be Fork to their own account, and then copy the project address under their own account. Go to the command line for “Git Clone project address” operation, and then push after modification. Once pushed, go to GitHub and click Pull Requests -> New Pull Request ->Create Pull Request.

The person in charge of project management on the team logs in to their account and clicks Pull Requests to see changes submitted by people outside the team, Click on the description and Files changed to view the description changed. Merge pull Request > Add Description >Confirm Merge