Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Git for version control

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Git for version control". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Git for version control".

What is Git?

Git is version control software that allows you to collaborate with other programmers. The biggest problem solved by Git is that it helps developers track different versions of the code base they are working on.

Before the invention of version control systems, it was difficult for different developers to synchronize their work.

How to install Git

You can download and install Git from the official website.

If you use it on Windows, be sure to add Git to the environment variable Path.

In Linux, you can install it directly from the terminal using the following command:

Sudo apt-get install git

After the installation is complete, you can check that Git is installed correctly by executing the following command:

Git-version

If you get the currently installed version, the installation is successful and you are ready to begin.

Basic knowledge of Git

Now let's learn the basics of Git through some small examples.

First, let's discuss how to create a single real source for the code.

In Git, the place where code is stored is called repository.

How to initialize an empty Git repository

The first step in using Git in a project is to initialize the Git repository. You can initialize the Git repository using the following command:

Git init

This command creates a subdirectory .git in the current directory. It will hold all internal Git metadata, such as submission history.

How to temporarily save changes in Git

We need to create some files to temporarily store the changes and commit them (which I'll discuss below). Let's create a test.txt and insert some text into it.

Staging means telling Git which files you are going to submit (add) to the repository. It is useful when you are working and want to submit a single file.

We are now ready to make changes. You can list the files to be temporarily saved separately, as follows:

Git add test.txt

Once we have executed this command, Git knows that test.txt is ready to commit.

Alternatively, you can temporarily save all files using the following command:

Git add.

It is useful when you have a pile of files and do not want to type each file name.

How to commit changes in Git

Committing changes creates a snapshot of the code base at a given point in time. You can return to this snapshot later or share it with your teammates so that they can accumulate from your progress.

Keep in mind that only files that are temporarily saved for submission are included. If you don't store anything temporarily, you won't be able to submit it.

After temporarily saving our changes, it's time to commit them. To commit changes, use:

Git commit-m ""

Once we have implemented git commit, we have completed the changes to the code base.

Commenting code is a good virtue, as is writing submission messages to Git. In many ide with integrated git, you cannot submit without filling in the submission information, which can also help you quickly locate where you have changed and what changes have been made. Trust me, writing a submission will give you a clearer understanding of what you are doing.

How to view logs in Git

You may want to view a log of project changes. You can do this using the following command:

Git log

As you can see, there are 2 submissions. The first shows that we have created a new file, and the second describes the changes to it.

Keep in mind that Git does not automatically track your changes. You should manually temporarily store and submit them.

How to reset and resume a submission in Git

If you make a mistake in the submission, you may want to undo the change.

There are two ways to undo changes:

Reset

Revert

Git reset (Reset)

The general syntax of the reset command is as follows: git reset HEAD~

The most common types of resets are:

-- soft: cancel the commit and keep the changes

-- hard: uncommit and delete changes

If we want to uncommit changes from Git but keep local changes to the code, we use the following command:

Git reset-soft HEAD~1

It is useful when you accidentally temporarily save some files that are not submitted.

After resetting, you can temporarily save the necessary changes and commit them.

Git reduction

You may also notice that each submission is associated with a hash value.

You can also use hash values to undo specific submissions:

Git revert 8a11c5095f2dcd70b0bc8c66061a1368558a3abf

This is different from a reset because it allows you to undo changes made in a particular commit.

When we decompose the command, we find that git revert adds an additional commit when you restore the changes.

How to use Git branches

Git allows you to create different branches. These branches allow you to separate the scope of the code version (for example, bug fixes, development, production, and so on-- all different branches).

To create a new branch, use the following command:

Git checkout-b

To switch to an existing branch, remove the-b flag and use the existing branch name instead of the new branch name:

How git checkout merges Git branches

After switching in a branch, you may want to update the main branch with the code of another branch. To do this, first move to the branch you want to update and use the following command:

Git merge

If all goes well, this will create a merge commit in the target branch and add all commits there.

How to resolve conflicts in Git

When merging branches, it is possible to update the same part of the same file in each branch.

In this case, a conflict occurs because Git does not know which changes to keep and which changes to discard. So Git creates a collision message and prompts you to manually select which branch is correct.

The conflict message outlines where the conflict occurred and the current and incoming changes.

After deciding how to resolve the conflict, you need to add a commit to resolve the conflict.

How to use a remote Git repository

To collaborate with others, you need to deal with remote repositories. We will focus on using Git in partnership with GitHub, but you can use any other similar site, such as GitLab or gitee.

To collaborate with others, you need to create an account on the website. You are now ready to contribute to open source projects on GitHub.

Not long ago, I found a small problem with the open source Hugo theme Papermod used in my programming blog.

The repair is very simple, and I want to contribute to the project to improve it.

Contribution steps:

Find the repository you want to contribute. Or, if you work in a corporate environment, your company may provide a repository for you to use.

Fork repository. Now you have a copy of the repository.

3. Copy the clone link found here:

4. Execute the following command:

Git clone

A copy of the repository will be created on your computer. Adding upstream remote is just a nickname for the source repository, using:

Git remote add

Now you can use the basics of Git that you have learned to modify the code.

After committing the changes, you can update the source repository with the following command:

Git push origin

Now you can create a Pull Request in the source repository, and the maintainer will review and merge your code.

Thank you for your reading, the above is the content of "how to use Git for version control". After the study of this article, I believe you have a deeper understanding of how to use Git for version control, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report