Some time ago, the whole project was migrated from SVN to Git, so I had to learn basic git operations. It’s a shame that it doesn’t feel like a world anymore. Other companies have been using Git to manage their code for a long time, and we’ve just started switching.

Git is a repository management tool. More and more companies are switching from SVN to Git. What is the overall process of Git?

  • 1. Workspace

Frankly, it is a local editing code tool IDE.

  • 2. Staging area: staging area

That is, after you execute add

  • 3. Local repository: version or local repository

After you commit, the code goes to the local repository, meaning that each ckeckout you pull is from the repository, not the remote repository.

  • A remote repository of knowledge

After a push, the code from the local repository is pushed to the remote repository.

This picture is from the resource network, I feel this picture is a good illustration of the whole git process. From the above flow chart, the relationship between commands and the conversion relationship between warehouses can be seen more intuitively.

In fact, this diagram will be more intuitive after learning the basic commands, mainly to give you a general idea of git workflow, and then understand the following commands will be more directional.

Note: All of the following commands are operating under Windows, Linux has a slight difference

  • 1. Copy code from the cloud

git clone https://your_repository_url.git

A lot of people do this, some even stop there, hahaha.

  • 2. Submit code

To submit the code after modification, run the following command.

git add filepath//. Represents all files
git commit -s "Log"
git push origin your_branch // Commit to your_branch
Copy the code

This is a necessary step for many students to start a project on Github.

  • 3. Pull the code again

If you want to abandon native code, you can use either of the following commands:

git checkout .// This is local code that cannot be retrieved from the remote repository if you commit.
git fetch . // Pull the remote repository code.
Copy the code

. Represents all folders. If you want to specify a file, go to. Change to the file path. This command overwrites the local code. If you don’t want to overwrite the code and just want to update the latest server code, you can use the following command.

git pull origin your_branch // Update and merge your_branch code
Copy the code
  • 4. Delete the folder
git rm your_file -r -f
Copy the code

Once deleted, you need to go through the submission process, otherwise the code won’t sync to the cloud.

  • 5. Conflict management

Conflict resolution is the most common in projects unless you are working alone. Although it will automatically help you merge, there are always some things that cannot be fused, so you need to handle them manually:

git stash // Temporarily store the code that has been edited locally
git pull origin your_branch // Pull the latest code from the your_branch
git stash pop // Restore the local code
Copy the code

After resolving the conflicting code, submit it. Otherwise, the code will be synchronized to the cloud.

In order to develop a feature or verify a problem, we need to develop a branch at zero time without affecting the whole process. We can use the following command.

git branch // View the local branch, marked with * as the current branch
git branch -a // View all branches
git branch your_branch // Create your_branch
git branch -d your_delete_branch // Delete your_delete_branch
Copy the code

Wouldn’t it be nice to be able to continue development after the functionality is integrated into the mainline without interfering with the mainline flow?

  • 7. Switch branches

Switching branches is also a very common command. In actual development, it is possible to open branches in order to verify their ideas or do some functions, so switching between branches is necessary.

git switch your_another_branch // Switch to your_another_branch
git clean -f -xd // Delete local files that do not belong to the branch
git pull origin your_another_branch // Update and merge your_another_branch
Copy the code

There’s another way to cut branches

git checkout your_branch
Copy the code

In fact, a very common scenario is that two branches of code may have a slightly different number of files. What if?

git clean -f -xd 
Copy the code

The extra files will be removed and only the code for the current branch will remain.

  • 8 submodule

Step 1: Add the SubModule

git submodule add https://***.git local_folder
Copy the code

If local_folder is not specified, it is the name of the repository by default. If local_folder is specified, the pulled down code will be stored in the local_folder folder in the project root directory. Step 2: Update the SubModule

git submodule init / / initialization
git submodule update // Update submodules
git submodule sync // Same step module
Copy the code

Step 3: Delete the SubModule

git submodule -d your_submodule
Copy the code

If the subModule is changed, you need git subModule Stash to hold the change before pulling the latest code, then pull the latest code through git subModule Update, and then restore the previous data through git Stash pop. It’s the same process as pulling code before.

Pit: Submodule submissions are very open to head pointer problems, especially for novices. Let me start with the reasons for this problem.

Each submission will generate a commitID, which is actually md5 value, but it is already in the local repository, but the SubModule has not yet pushed, it is likely that the push failed. This is where your main project pushes your code successfully. You go to the remote repository and find that there is a problem with the head pointer to the subModule.

When submitting code to the SubModule, pay attention to two points: 1. The subModule must be pushed successfully. 2.🐷 project should also be pushed successfully.

And you might think, well, I think I’ve submitted it successfully, is there any verification mechanism? Well, there is.

You go back to the main project and use the status command to check the status of the subModule changes. If there are changes, the subModule has not been successfully submitted, otherwise it has been successfully submitted.

  • 9. View logs
git log
git log -p// View the details of each commit
Copy the code

10. View the locally modified file

git status
Copy the code

11. Merge code

If you want to merge the code from branch A to branch B, you first need to switch to branch B and merge it using the following command

git switch B
git merg A
Copy the code

12. Roll back the version

git reset commit_id
Copy the code

Commit_id can be viewed in the Git log.

Learning Git is a hands-on process. If you have a problem with it, don’t delete the code and pull it back. You’ll never learn.

When we encounter problems, we need to analyze where the problems are and how to solve them. Now the Internet is so developed, many problems have ready-made solutions. Over time, you’ll be able to master a lot of Git skills. This is the best way to learn Git and the only way to verify how well you’re doing.

Welcome to exchange the use of Git.