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 realize synchronization between Git remote Warehouse and Local Warehouse by Linux Operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how to achieve Linux operation Git remote warehouse and local warehouse synchronization", in the daily operation, I believe that many people in how to achieve Linux operation Git remote warehouse and local warehouse synchronization problems have doubts, editor consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "how to achieve Linux operation Git remote warehouse and local warehouse synchronization" doubt is helpful! Next, please follow the editor to study!

Create a local warehouse and synchronize to the remote

Git is a very powerful version management tool. For the rest of the day, I'm going to talk about the basics of git: how to create a local git repository and synchronize your local code remotely. This tutorial works on mac, and if your system is Linux, the method is the same; if your system is windows, you only need to call up the bash window of git, and the subsequent process is the same as Linux.

First, you need to install git, so ignore this step and install it yourself.

Second, assuming you haven't created a working directory yet, create a directory first, called StudyGit.

Third, the current StudyGit directory is only a local directory and has nothing to do with git, so how can we associate this directory with git? Just go to the StudyGit directory and run the following command:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git init

Initialized empty Git repository in / Users/yanyaozhen/Coding/Practice/StudyGit/.git/

The prompt has indicated that we have initialized an empty local git repository. If you look at all the files under StudyGit, you will find a hidden .git file, which is very important, and version control depends on it. At the same time, if you want to deploy an application to a production environment, remember not to deploy this directory to the server, because this is a hacker's favorite!

4. next, let's take a look at a command that can be said to be the most frequently used:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git status

On branch master

Initial commit

Nothing to commit (create/copy files and use "git add" to track)

Yes, it is "git status". This command is used to check the status of the current git, for example, in the above example, we have just created an empty git repository, so when we run this instruction, git will prompt us that there is currently nothing to commit and that we can use the "git add" command to track changes in files after creating or copying files.

Now let's create a file. We create a text file called "a.txt" with the following contents:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ cat a.txt

Aaa

At this point, let's run git status again. The operation is as follows:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git status

On branch master

Initial commit

Untracked files:

(use "git add..." To include in what will be committed)

A.txt

Nothing added to commit but untracked files present (use "git add" to track)

This will prompt us to have an untracked file called a.txt. We want to add this file to version control, so we run the following command:

The code is as follows:

Git add a.txt

This command means to add the a.txt we specified to git control. The add command can have no parameters (under windows, it can be without parameters, and under mac, you need to specify the current directory, that is, "."). If there are no parameters, it means that all files in the directory are added to the git repository. At this point, let's run "git status" again:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git status

On branch master

Initial commit

Changes to be committed:

(use "git rm-- cached..." To unstage)

New file: a.txt

Prompt us to have changes that need to be submitted. At this point, the a.txt file exists in the so-called "staging area", and the files in the staging area can actually be submitted to the git repository. Some students may say, if I don't want to submit this file, how can I remove the file from the temporary storage area? In fact, the answer is in the prompt just now:

Use "git rm-- cached..." To unstage .

So, let's run this command:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git rm-cached a.txt

Rm 'a.txt'

When you run git status again, you'll find that you're back to where you were when the file wasn't committed.

6. When our files have been added to the temporary storage area through "git add", we can actually submit the files to the git warehouse. As follows:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git commit-m "submit a.txt"

* Please tell me who you are.

Run

Git config-global user.email "you@example.com"

Git config-global user.name "Your Name"

To set your account's default identity.

Omit-- global to set the identity only in this repository.

Fatal: unable to auto-detect email address (got 'yanyaozhen@macbookpro. (none)')

An ou, reported an error. Check the reason given for the error and found that it was because email and user name were not set, so git wouldn't let me submit it. Why does git force these two configurations to be set before they can be submitted? Because git has to know who submitted it, if you don't even know who submitted it, how can you do version management, right? So let's set it up according to the prompt:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git config-global user.email "youremail@example.com"

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git config-global user.name "yourusername"

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$

All right, let's submit the following:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git commit-m "submit a.txt"

[master (root-commit) dab07d0] submit a.txt

1 file changed, 1 insertion (+)

Create mode 100644 a.txt

It is found that it can be submitted successfully, prompting a file to be modified (that is, our a.txt) and a line to be inserted (our only line "aaa").

Let's run git status again:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/Practice/StudyGit$ git status

On branch master

Nothing to commit, working directory clean

Remind us that we currently have nothing to submit and that the current working directory is clean because we have submitted it to git.

Well, at this point, git can fully track the file that has been submitted, and it can record any changes to the file in the future. For example, if someone secretly changes something to the project, the project manager must be able to find out who modified which files according to the git version record.

Now we can submit the file to the remote server.

When we want to submit the content from the git repository to the remote server, we must first become trusted users of the remote server (of course, the server does not allow everyone to push the content, which is a mess). At this point, we need to generate a public key locally, and then put the public key in the account list of the remote server. The command to generate the public key is as follows:

The code is as follows:

Ssh-keygen-t rsa-C "youremail@example.com"

If you are a windows user, then the directory where the public key is generated is C:\ Users\ username .ssh, in which there are two files, id_rsa and id_rsa.pub, in which the content of id_rsa.pub is the public key we need. If you are a Linux or mac user, there will also be a .ssh directory under the user's home directory with private and public keys. Suppose we now want to use github as the remote push server address, then we first put the public key in the github settings- > SSH keys list (click create a ssh key,title as you like, copy and save the entire contents of the public key).

Now we can add a remote repository to the github for the StudyGit project. For add new repository on github, I also use StudyGit here (other names are also acceptable). After it is built, it is shown as follows:

You can see that one ssh address is git@github.com:onlyanyz/StudyGit.git, and this is our remote git repository address, and we will push the file here in a moment.

Next, we need to associate the local git repository with the remote git repository, as follows: enter the local StudyGit root directory and execute the following command:

The code is as follows:

Git remote add origin git@github.com:onlyanyz/StudyGit.git

What this command means is that I gave the remote repository (that is, git@github.com:onlyanyz/StudyGit.git) a different name, origin (in fact, another name is fine, as long as you know that this is an alias, and then use an alias to operate the remote repository). After the operation, we can use the "git remote" command to see which remote repositories are associated with the current local warehouse (it lists the short names of each remote repository. If you have previously used the git clone command, after cloning a project, you can see at least one remote library called origin, which Git uses by default to identify the original repository you cloned.

After the remote warehouse is associated, we can push it. The command to push the local warehouse to the remote warehouse is as follows:

The code is as follows:

Git push origin master

The command is interpreted as follows:

The syntax of git push is: git push:

This syntax represents a remote branch that push the local branch name to the remote hostname. We can see that the statement just executed does not have a remote branch name (omitted). If the remote branch name is omitted, the local branch is pushed to the remote branch with which it has a "tracking relationship" (usually with the same name). If the remote branch does not exist, it will be created. So the statement just executed means that the local master branch is pushed to the master branch of the remote host.

There are some things to note about the git push command in the future:

a. If the local branch name is omitted, the specified remote branch is deleted because this is equivalent to pushing an empty local branch to the remote branch. For example:

The code is as follows:

$git push origin: master

This command is equivalent to the following directive: $git push origin-- delete master, which deletes the master branch of the remote host.

B.git push sometimes both local and remote branch names can be omitted, and sometimes even hostnames can be omitted, which is not detailed now.

c. If the current branch has a tracking relationship with multiple hosts, you can use the-u option to specify a default host so that you can use git push later without any parameters:

The code is as follows:

$git push-u origin master

OK, now that we have pushed all the contents of the StudyGit directory to the remote, run git status to take a look:

The code is as follows:

~ / Coding/Practice/StudyGit$ git status

On branch master

Nothing to commit, working directory clean

The working directory is still clean, so you can continue the process of creating files locally-> submit to the staging area-> submit to the local warehouse-> push to the remote warehouse!

Clone the remote warehouse locally

Let's talk about a new way to play, that is, there is a remote warehouse first, followed by a local warehouse, that is, the remote warehouse is "clone" to the local.

Suppose that other members of your team have built the warehouse on git and have also push the code. This remote git repository is also called "StudyGit" and has two files: a.txt and README.md. Now, you will also start to contribute code, so you first need to pull everything submitted by other team members to your local directory. At this time, you will use the "clone" command:

The code is as follows:

Git clone git@github.com:onlyanyz/StudyGit.git

As long as you execute this instruction, you can pull everything from the remote warehouse to the local working directory, and of course the generated local directory name is the same as the remote warehouse name.

If you check the status of the current local git repository now, it is as follows:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/StudyGit$ git status

On branch master

Your branch is up-to-date with 'origin/master'.

Nothing to commit, working directory clean

The command echo indicates that my local branch has been updated to the latest remote master branch. After that, we can add code and submit as described in the article "getting started with git".

Now, let's take a look at the git project that just went from clone to local. Now there are two files, as follows:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/StudyGit$ ll

Total 16

-rw-r--r-- 1 yanyaozhen staff 21B 11 19 00:04 README.md

-rw-r--r-- 1 yanyaozhen staff 4B 11 19 00:04 a.txt

Next, if classmate An adds another file b.txt to the warehouse on github, there are now three files in the github remote warehouse (note that the files in the local warehouse are different from the remote warehouse).

Next, we continue our development work locally. If we create a new file "c.txt", now let's add the "c.txt" file to the temporary storage area, and then commit it to the local warehouse. At this time, we want to remotely push the work we have just done, as follows:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/StudyGit$ git push origin master

To git@github.com:onlyanyz/StudyGit.git

! [rejected] master-> master (fetch first)

Error: failed to push some refs to 'git@github.com:onlyanyz/StudyGit.git'

Hint: Updates were rejected because the remote contains work that you do

Hint: not have locally. This is usually caused by another repository pushing

Hint: to the same ref. You may want to first integrate the remote changes

Hint: (e.g., 'git pull...') Before pushing again.

Hint: See the 'Note about fast-forwards' in' git push-- help' for details.

An ou, you have made a mistake. Students who know a little English can see the problem from the prompt, because our remote has been updated. When we push to remote, we must first merge the remote changes locally before we can submit my changes again. So, the following order came out:

The code is as follows:

$git fetch origin master

This instruction means to update the latest version from the master master branch of the remote origin repository to the origin/master branch.

Then we compare the difference between the current local master branch and the origin/master branch:

The code is as follows:

$git log-p master..origin/master

The echo result of the execution details the differences between the two branches.

Then, we need to merge the content on the origin/master branch into the local master branch:

The code is as follows:

Git merge origin/master

After the execution of the instruction, we may be asked to enter the reasons for the merger, and after filling in, we will succeed in the merger. At this point, we can push again:

The code is as follows:

Yanyaozhen@macbookpro:~/Coding/StudyGit$ git push origin master

Counting objects: 5, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (4amp 4), done.

Writing objects: 100% (5 bytes), 543 bytes | 0 bytes/s, done.

Total 5 (delta 2), reused 0 (delta 0)

To git@github.com:onlyanyz/StudyGit.git

6b3662f..6036a05 master-> master

Push is successful, and now you can go to github to see the b.txt file we created locally!

At this point, the study on "how to realize the synchronization of Linux operation Git remote warehouse and local warehouse" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report