preface

Git repositories are central to project management. At present, online Github, code cloud, coding, etc., and offline GitLab, SCM Manager, etc., all need this tool to operate. Therefore, it is important to master the related knowledge of Git. This chapter is about the installation and basic use of Git. To use Git, you must first download It, and this is where the beautiful story begins.

1.Git installation and configuration

1.1 installation

Installation is very simple, in fact, is some fool operation, directly on his official website download it, I will not speak, download after the right mouse button will be a command line tool, as shown in the picture, the future Windows partners can operate in the command line. As for the MAC, I heard that after pressing the command line tool can be used!

1.2 configuration

1. Open the CLI, Enter ssh-keygen -t rsa -c “[email protected]”(your email address), and press Enter repeatedly.

$ ssh-keygen -t rsa -C "[email protected]"     
Copy the code

2. Add a public key

SSH. Open the id_rsa.pub file in a text editor and copy all the contents (Linux uses open ~/.ssh).

2. Log in to Github, click on your profile picture in the upper right, select Settings, click Add SSH key, and copy the contents of id_rsa.pub into it

SSH -t [email protected] is used to determine whether the configuration is successful, as shown in the following figure. Each configuration method is similar with minor differences

1.3 One machine controls multiple warehouses

Sometimes, your project exists under different warehouses; So, you have to configure a different warehouse, simple to say, look at the steps. For example, I’ll take Github and the code cloud as an example.

Enter CD /c/Users/HS/.ssh to create a directory (each person’s directory is not the same), it is easy to manage in the future.

1. Create two certificates

1Ssh-keygen -t rsa-c generate SSH certificates based on the email used by each repO and fill in your own email account in the code repository"[Your email address]"
2, according to the different Git repository, later need to configure the certificate for each repository, here I namedid_rsa_github
Enter file in which to save the key (/c/Users/HS/.ssh/id_rsa)3. Enter the password. Generally, Enter the password directlypassphrase (empty for no passphrase): Enter same passphrase again: Pub /c/Users/HS/.ssh/ id_rsa_github and id_rsa_githubCopy the code

As is shown in

2. Create a config file in the. SSH directory and edit the following information

#github Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_github user Hansen1994 #gitee Host gitee.com HostName gitee.com IdentityFile ~/.ssh/id_rsa_gitee user dream1994Copy the code

Then it’s ready to use

2. Workflow (dialogue with manager)

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — on the first day — — — — — — — — — — — — — — — — — — — — — — — —

Git init initializes the repository

Git add xxx. TXT/git add.

Git commit -m “SSS” -m

— — — — — — — — — — — — — — — — — — — — — — — the first day before coming off work added requirements (product manager) — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Me: Get to work Balabala, knock knock knock knock, and submit to the staging area, not to the local warehouse

Git add xxx. TXT commit the file to staging area

— — — — — — — — — — — — — — — — — — — — — — — the second day (product manager said yesterday before coming off work don’t demand, the customer has raised new requirements) — — — — — — — — —

Me: Oh, let’s start

Git reset HEAD returns to the workspace what was committed to the staging area yesterday (you can specify git reset HEAD filepathname)

Git checkout: delete all the code that you just wrote in your workspace and did not commit to the staging area. Use caution)

Me: Let’s continue with the product manager’s requirements for today (similar to the first day’s operation and finally into the local warehouse)

— — — — — — — — — — — — — — — — — — — — — on the third day (product manager, said the second day what all don’t, don’t demand) — — — — — — — — — — — — — — — — — — — —

Me: I’m so unlucky

Git log View the log of each update to find the original content number of the first day

‘git reset –hard

— — — — — — — — — — — — — — — — — — the fourth day (also don’t the product manager, said the local file) — — — — — — — — — — — — — —

Me: Ok, I am so poor

Git rm xxx. TXT

Git commit -m “delete XXX “clears the files in the local repository

3. Remote warehouse push code has not been established

Me: It all starts at zero

Git readme. md: git commit -m"xxx"- m to modify the name (take) the git remote add origin [email protected]: han1994 / dasadasa git to local and remote associated git push -u origin master to online push up Git pull Origin Master Downloads files from the remote repositoryCopy the code

4. If you want to create a remote repository, you need to create a remote repository

At this point, you have already written the code locally and want to associate it with the remote repository

Git remote -v view the correlation information git remote add origin [email protected]: han1994 / dasadasa git associated git push origin master pushCopy the code

5. Package management

If you’ve already written a version of the code, you can package it and label it

If we want to roll back in the future, we can use tags instead of branches! Git tag View all tags. Git tag name Create tag git tag -a name -m "comment" git tag -d name Delete tag git push origin name Release tagCopy the code

6. Branch management (multi-person collaboration)

Multiplayer development is basically writing your own features, you can create your own branch, don’t change on the main branch, and merge at the end!

6.1 Steps of multiplayer development

1. Git checkout -b [XXX] / git checkout [XXX]

2. Run the vi a.js command to modify the content (vi belongs to Linux. After exiting, you can run the git status command to check whether the content has been modified or run the git diff command to check the modified content).

Git add. // Add all the code you are currently modifying

Git commit -m “update part2”

Git push origin [XXX

——– Half a month later the function is completed (code merged into the main branch master——–)

Git checkout // go back to the master branch

Git merge [XXX] // merge the contents of XXX into the master branch

Git push origin master //

6.2 Solve the problem that code cannot be pushed up when two people change the same branch at the same time

Let’s say local development time when you’re coding and you’re supposed to make changes on the branch and you accidentally make changes on dev

  1. We can use it firstgit stashTo push their new code into the cache first
  2. throughgit pull origin devPull the code down
  3. performgit stash popRelease the temporary stuff and return it to this branch

Remember: you should not use Git checkout. It will ruin your work today.

6.3 Resolving Code Conflicts

If you pull down your code and there’s a conflict between two people’s code, let’s get together and see who made the right change, usually something like vscode will automatically figure out where there’s a conflict.

Git reset and Git Revert

They both mean to undo the code

Git reset

It can roll back a commit on a remote commit

Steps:

Git reset –hard HEAD~1 // Rollback the number after a version indicates the number of rollback, and 2 indicates the number of rollback

Git reset –hard [version number] //

Git push origin HEAD –force //

As shown in figure:

git revert

Git revert is used to “reverse” a version in order to undo changes made to that version. For example, we commit three releases (version 1, version 2, and version 3) and suddenly discover that version 2 doesn’t work (e.g. If you want to undo version 2 without affecting a commit to undo version 3, you can use git revert to reverse version 2 and create a new version 4 that retains version 3 but undoes version 2.

Steps:

Git revert -n git revert -n git revert -n

Git commit -m // commit to a new version of git

Git push // finally push to the remote branch

As shown in figure:

Git commands

8.1 Git Commands Table

8.2 Useful Linux Commands

  1. catFile name // View the file content
  2. Touch + File name// Create a new file
  3. Cp 【 Path 1 】 【 Path 2 】// Move the file from path 1 to path 2
  4. Vi the file// Create a new file and open it
  5. iInsert text
  6. esc + :wq// Exit and save the file contents