directory

 

Git tutorial

Git Installation and Configuration

Git Workflow

Git workspaces, staging areas, and repositories

Git create repository

Git Basic Operations

Git Branch Management

Git looks at the commit history

git log

git blame

Git label

Git Remote Repository (Github)

Git Gitee

Git server Setup


Git tutorial

Git is an open source distributed version control system designed to handle any project, small or large, with agility and efficiency.

Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.

Git is different from the common version control tools such as CVS and Subversion. It adopts the way of distributed version library without the support of server-side software.

Git is different from SVN

Git is not just a version control system, it is also a content management system (CMS), work management system, etc.

If you’re someone with a SVN background, you’ll need to do a bit of a mind-shift to accommodate some of the concepts and features Git offers.

Differences between Git and SVN:

  • 1. Git is distributed, SVN is not: This is the core difference between Git and other non-distributed version control systems such as SVN, CVS, etc.
  • 2. Git stores content as metadata, while SVN stores content as files: all resource control systems hide meta information of files in folders like.svn,.cvs, etc.
  • The Git branch is different from the SVN branch: the branch is not special in SVN at all, in fact it is just another directory in the repository.
  • 4. Git does not have a global version number, whereas SVN does: this is by far the biggest feature Git lacks compared to SVN.
  • Git’s content integrity is better than SVN’s: Git’s content storage uses the SHA-1 hash algorithm. This ensures the integrity of the code content and reduces damage to the repository in the event of disk failures and network problems.

Git Quick Start

Git command manual: git-scm.com/docs

 

Git Installation and Configuration

Git needs to be installed before you can use it. Git currently runs on Linux/Unix, Solaris, Mac, and Windows platforms.

The installation package for each Git platform can be downloaded from git-scm.com/downloads

Install on Windows platform

Installing Git on A Windows platform is also easy. There is a project called msysGit that provides the installation package. You can download the installation file from GitHub and run it:

Download the installation package at gitforWindows.org/

Once installed, you are ready to use the command-line Git tool (which already comes with an SSH client), as well as a graphical Git project management tool.

Go to “Git”->”Git Bash” in the Start menu. This will bring up a Git command window where you can perform Git operations.

Install on Mac platform

The easiest way to install Git on a Mac is to use the graphical Git installation tool:

Sourceforge.net/projects/gi…

The installation interface is as follows:

 

Git configuration

Git provides a tool called Git Config that is used to configure or read the corresponding workspace variables.

These environment variables determine the specific working mode and behavior of Git in each link. These variables can be stored in three different places:

  • /etc/gitconfigFile: A configuration on the system that is universally applicable to all users. If usegit configWhen using--systemOption to read and write to this file.
  • ~/.gitconfigFile: The configuration file in the user directory applies only to the user. If usegit configWhen using--globalOption to read and write to this file.
  • Configuration files in the current project’s Git directory (that is, in the working directory).git/configFile) : The configuration here is valid only for the current project. Each level of configuration overrides the same configuration on top, so.git/configThe configuration in/etc/gitconfigVariable with the same name in.

On Windows, Git looks for the.gitconfig file in the user’s home directory. The HOME directory is the directory specified by the $HOME variable, usually C:\Documents and Settings\$USER.

Git will also try to locate the /etc/gitconfig file as its root directory.

The user information

Configure the user name and email address of the individual:

$ git config --global user.name "sunjiaoshou"
$ git config --global user.email [email protected]
Copy the code

 

Git Workflow

The general working process is as follows:

  • Clone the Git resource as the working directory.
  • Adds or modifies files on cloned resources.
  • If someone else makes changes, you can update the resource.
  • Review the changes before committing.
  • Commit changes.
  • After the changes are made, if errors are found, the commit can be withdrawn and modified and committed again.

 

 

  •  

Git workspaces, staging areas, and repositories

Git workspaces, staging areas, and repositories

  • Workspace: the directory you see on your computer.
  • Staging area: stage or index. Git /index. This is why the staging area is sometimes called an index.
  • Git repositories: The workspace has a hidden directory. Git, which is not a workspace but a Git repository.

The following diagram shows the relationship between workspaces, staging areas in repositories, and repositories:

  • The workspace is on the left and the version library is on the right. The area marked “index” in the repository is stage/index, and the area marked “master” is the directory tree represented by the master branch.
  • We can see that the HEAD is actually a cursor that points to the master branch. So you can use master instead of HEAD in the commands shown here.
  • The objects area in the image is the Git object library, which is actually located in the “.git/objects” directory and contains the various objects and content created.
  • When you run git add on a workspace modified (or newly added) file, the staging directory tree is updated and the contents of the workspace modified (or newly added) file are written to a new object in the object library, whose ID is recorded in the staging file index.
  • When a Git commit is performed, the directory tree of the staging area is written to the repository (the object repository) and the master branch makes updates accordingly. The directory tree that the master points to is the directory tree of the staging area at commit time.
  • When the git reset HEAD command is executed, the staging directory tree is rewritten and replaced by the directory tree pointed to by the master branch, but the workspace is not affected.
  • When you run git rm –cached

    , the files are deleted directly from the staging area, leaving the workspace unchanged.
  • When you execute git checkout. Or git checkout —

    , the workspace files are replaced with all or specified files in the staging area. This operation is dangerous and clears changes in the workspace that have not been added to the staging area.
  • When you execute git checkout HEAD. Or git checkout HEAD

    , you replace the staging area and files in the workspace with all or part of the files in the master branch that the HEAD points to. This command is also dangerous because it clears uncommitted changes not only in the workspace but also in the staging area.

Git create repository

You can use an existing directory as your Git repository.

git init

Git init is the first command used to initialize a Git repository. Git init is the first command used to initialize a Git repository.

After the git init command is executed, the git repository generates a.git directory that contains all the metadata for the resource, leaving the other project directories unchanged (unlike SVN, which generates.svn directories in each subdirectory, Git only generates.git directories in the root directory of the repository).

Method of use

Using the current directory as your Git repository, we just need to initialize it.

git init
Copy the code

After this command is executed, a.git directory is generated in the current directory.

Use our specified directory as a Git repository.

git init newrepo
Copy the code

After initialization, a.git directory is created in the newrepo directory, where all git data and resources are stored.

If you have several files in your current directory that you want to version control, you need to tell Git to start tracking these files with the git add command, and then commit:

C $git add README $git commit -mCopy the code

git clone

We use Git Clone to copy projects from an existing Git repository (similar to SVN Checkout).

The command for cloning a warehouse is in the following format:

git clone <repo>
Copy the code

If we need to clone to the specified directory, we can use the following command format:

git clone <repo> <directory>
Copy the code

 

Parameter Description:

  • Repo: Git repository.
  • Directory: indicates a local directory.

For example, to clone the Ruby Git repository Grit, use the following command:

$ git clone git://github.com/schacon/grit.git
Copy the code

After the command is executed, a directory named grit is created in the current directory, which contains a.git directory for storing all downloaded version records.

If you want to define the name of the new project directory, you can specify the new name at the end of the command above:

$ git clone git://github.com/schacon/grit.git mygrit
Copy the code

configuration

Git Settings use the git config command.

Display git configuration information:

$ git config --list
credential.helper=osxkeychain
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
Copy the code

Edit git configuration files:

$git config -e #Copy the code

Set user information when submitting code:

$ git config --global user.name "runoob"
$ git config --global user.email [email protected]
Copy the code

If the –global parameter is removed, it is only valid for the current repository. If the –global parameter is removed, it is only valid for the current repository.

 

Git Basic Operations

Git’s job is to create and save snapshots of your project and compare them with subsequent snapshots.

I’ll cover the commands for creating and submitting snapshots of your project.

Git clone, Git push, Git add, Git commit, Git checkout, Git pull.

Description:

  • -Jenny: Well, I’m working in a workspace.
  • Staging area: staging area/cache area
  • Local repository: or local repository
  • A: Remote repository

A simple procedure:

$ git init    
$ git add .    
$ git commit  
Copy the code
  • Git init – Initializes repository.
  • Git add. – Add files to staging area.
  • Git commit – Add staging content to the repository.

Create warehouse command:

Git repository commands are listed in the following table:

Submission and Modification

Git’s job is to create and save snapshots of your project and compare them with subsequent snapshots.

The following table lists the commands for creating and submitting snapshots of your project:

Commit log

Remote operation

Git Branch Management

Almost every version control system supports branching in some form. Using branches means you can detach from the main line of development and continue working without affecting the main line.

Some call Git’s branching model a killer feature that sets Git apart from the version control system family.

Create branch command:

git branch (branchname)
Copy the code

Switch branches command:

git checkout (branchname)
Copy the code

 

When you switch branches, Git replaces the contents of your working directory with the last committed snapshot of that branch, so multiple branches don’t require multiple directories.

Merge branch command:

git merge 
Copy the code

You can merge multiple times into the unified branch, or you can choose to delete the merged branch directly after the merge.

To start, let’s create a test directory:

$ mkdir gitdemo $ cd gitdemo/ $ git init Initialized empty Git repository... $touch README $git add README $git commit -m '新 版本' [master (root-commit) 3b58100] 新 版本 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 READMECopy the code

Git Branch Management

List the branch

List branch basic commands:

git branch
Copy the code

With no arguments, Git Branch lists your local branches.

$ git branch
* master
Copy the code

What this example means is that we have a branch called master and that branch is the current branch.

When you execute git init, git creates the master branch for you by default.

If we want to create a branch manually. Run git branch (branchname).

$ git branch testing
$ git branch
* master
  testing
Copy the code

Now we can see that there is a new branch of Testing.

When you create a branch in this way after the last update was committed, Git will restore your working directory to what it looked like when you created the branch, if you commit an update later and then switch to the Testing branch.

Next we’ll show you how to switch branches. We use Git Checkout (branch) to switch to the branch we want to change.

$ ls README $ echo 'sunjiaoshou' > test.txt $ git add . $ git commit -m 'add test.txt' [master 3e92c19] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt $ ls README test.txt $ git checkout testing Switched to branch 'testing' $ ls  READMECopy the code

When we switch to the Testing branch, the new file we added, test.txt, is removed. They reappear when I switch back to the Master branch.

$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt
Copy the code

You can also use git checkout -b (branchname) to create a new branch and immediately switch to that branch to operate in that branch.

$ git checkout -b newtest Switched to a new branch 'newtest' $ git rm test.txt rm 'test.txt' $ ls README $ touch PHP $git add. $git commit -am 'removed test. TXT, add sunJIAOslo.php '[newtest c1501A2] removed Test. TXT, add sunjiaos.php 2 files changed, 1 deletion(-) create mode 100644 sunjiaoshou.php delete mode 100644 test.txt $ ls README sunjiaoshou.php $ git checkout master Switched to branch 'master' $ ls README test.txtCopy the code

As you can see, we created a branch, removed some of the test.txt files from the branch and added runoob.php, then switched back to our main branch, the deleted test.txt file came back, and the newly added runoob.php did not exist in the main branch.

Using branches to slice up work allows us to work in different development environments and switch back and forth.

Delete the branch

Delete branch command:

git branch -d (branchname)
Copy the code

For example, we want to delete the testing branch:

$ git branch
* master
  testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master
Copy the code

Branch merge

Once a branch has independent content, you will eventually want to merge it back into your main branch. You can merge any branch into the current branch using the following command:

git merge
Copy the code

 

$ git branch
* master
  newtest
$ ls
README        test.txt
$ git merge newtest
Updating 3e92c19.. c1501a2 Fast-forward runoob.php |0
 test.txt   | 1 -
 2 files changed, 1 deletion(-)
 create mode 100644 sunjiaoshou.php
 delete mode 100644 test.txt
$ ls
README        runoob.php
Copy the code

In the example above we merged the newtest branch into the main branch, and the test.txt file was deleted.

After merging, you can delete branches:

$ git branch -d newtest
Deleted branch newtest (was c1501a2).
Copy the code

When deleted, only the master branch is left:

$ git branch
* master
Copy the code

Merge conflicts

Git merges changes as well as simply adding and removing files.

$ git branch
* master
$ cat sunjiaoshou.php
Copy the code

First we create a branch called change_site and switch to that we change the sunjiaoshou.php content to:


      
echo 'sunjiaoshou';
? >
Copy the code

Create a change_site branch:

$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim runoob.php
$ head -3 runoob.php

      
echo 'runoob';
? >
$ git commit -am 'changed the sunjiaoshou.php'
[change_site 7774248] changed the sunjiaoshou.php
 1 file changed, 3 insertions(+)
Copy the code

Submit the changes to the Change_site branch. Now if we switch back to the master branch we can see the contents revert back to where we left them (empty file, no code) and we’ll modify the sunjiaoshou.php file again.

$git checkout master Switched to branch 'master' $git sunjiaoshou. PHP $vim sunjiaoshou sunjiaoshou.php <? php echo 1; ? > $ git diff diff --git a/sunjiaoshou.php b/sunjiaoshou.php index e69de29.. Ac60739 100644 -- a/ sunmodule.php +++ b/ sunmodule.php @@ -0,0 +1,3 @@ +<? php +echo 1; +? > $git commit -am 'file changed' [master c68142b]Copy the code

These changes are now logged into my “master” branch. Next we merge the “change_site” branch.

$ git merge change_site Auto-merging runoob.php CONFLICT (content): Merge conflict in sunjiaoshou.php Automatic merge failed; Fix conflicts and then commit the result. $cat sunjiaoshou. PHP # php <<<<<<< HEAD echo 1; ======= echo 'sunjiaoshou'; >>>>>>> change_site ? >Copy the code

We merge the previous branch into the master branch, a merge conflict occurs, and we need to manually modify it.

$ vim sunjiaoshou.php $ cat sunjiaoshou.php <? php echo 1; echo 'sunjiaoshou'; ? > $ git diff diff --cc sunjiaoshou.php index ac60739,b63d7d7.. 0000000 - a/sunjiaoshou. PHP + + + b/sunjiaoshou PHP @ @ @ - 1, 3-1, 3 + 1, 4 @ @ @ <? php +echo 1; + echo 'sunjiaoshou'; ? >Copy the code

In Git, you can use Git add to tell Git that a file conflict has been resolved

$ git status -s
UU sunjiaoshou.php
$ git add sunjiaoshou.php
$ git status -s
M  sunjiaoshou.php
$ git commit
[master 88afe0e] Merge branch 'change_site'
Copy the code

We have now successfully resolved the conflicts in the merge and submitted the results.

 

Git looks at the commit history

Git commit history

git log

After committing some updates with Git, or cloning a project, you can review the commit history by using Git log.

For the previous section, use git log to list the historical commit records as follows:

  • Git log – View historical commit records.
  • Git blame

    – Displays a list of historical changes to a specified file.
$ git log commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master) Merge: c68142b 7774248 Author: sunjiaoshou <[email protected]> Date: Fri May 3 15:55:58 2019 +0800 Merge branch 'change_site' commit c68142b562c260c3071754623b08e2657b4c6d5b Author: sunjiaoshou <[email protected]> Date: Fri May 3 15:52:12 commit 777424832 2019 + 0800 to modify code e714cf65d3be79b50a4717aea51ab69 (change_site) Author: sunjiaoshou <[email protected]> Date: Fri May 3 15:49:26 2019 +0800 changed the runoob.php commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00 Author: sunjiaoshou <[email protected]> Date: Fri May 3 15:35:32 2019 +0800Copy the code

We can use the –oneline option to view a concise version of the history.

$git log --oneline $git log --oneline d5e9fc2 (HEAD -> master) Merge branch 'change_site' c68142b (change_site) changed the sunjiaozuo.php c1501A2 removed test.txt, add sunjiaozuo.php 3e92C19 add test.txt 3b58100 First version submissionCopy the code

What this tells us is the history of the project.

We can also use the –graph option to see when branches and merges occurred in history. The following is the same command to enable topology options:

* d5e9fc2 (HEAD -> master) Merge branch 'change_site' |\ | * 7774248 (change_site) changed the runoob.php * | c68142b Modify the code | / * c1501a2 removed test. TXT, add runoob. PHP * 3 e92c19 add test. TXT * 3 b58100 version submitted for the first timeCopy the code

Now we can see more clearly when work forks and when it merges.

You can also use the –reverse parameter to display all logs in reverse.

$git log --reverse --oneline 3b58100 first version submitted 3e92c19 add test.txt c1501A2 removed test.txt, add runoob.php 7774248 D5e9fc2 (HEAD -> master) Merge branch 'change_site'Copy the code

Git log –author if you only want to find the commit log of a specified user, you can use git log –author.

$ git log --author=Linus --oneline -5 81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory 3bb7256 make "index-pack" a built-in 377d027 make "git pack-redundant" a built-in b532581 make "git unpack-file" a built-in 112dd51 make "mktag" a  built-inCopy the code

If you want to specify a date, you can execute several options: –since and –before, but you can also use –until and –after.

For example, if I wanted to see all commits in a Git project from three weeks ago and after April 18, I could do this (I also used the –no-merges option to hide merge commits) :

$git log --oneline --before={3. Weeks. Ago} --after={2010-04-18} --no-merges 5469e2d git 1.7.1 -- rc2 d43427d Documentation/remote-helpers: Fix typos and improve language 272a36b Fixup: Second argument may be any arbitrary string b6c8d2d Documentation/remote-helpers: Add invocation section 5ce4f4e Documentation/urls: Rewrite to accomodate transport::address 00b84e9 Documentation/remote-helpers: Rewrite description 03aa87e Documentation: Describe other situations where -z affects git diff 77bc694 rebase-interactive: silence warning when no commits rewritten 636db2c t3301: add tests to use --format="%N"Copy the code

You can view the git log command at git-scm.com/docs/git-lo…

git blame

To view changes to a specified file, use the git blame command.

git blame <file>
Copy the code

The git blame command displays the list of changes, as shown in the following example:

$git blame README ^ d2097AA (Tianqixin 2020-08-25 14:59:25 +0800 1) # +0800 2) # wechat official number: E-commerce programmerCopy the code

Git label

If you reach an important stage and want to remember that particular commit snapshot forever, you can tag it with a Git tag.

For example, we want to release a “1.0” version for our Runoob project. Git tag -a v1.0 git tag -a v1.0 git tag -a v1.0

The -a option means “Create an annotated label”. This can be done without the -a option, but it does not record when or by whom the tag was typed, nor does it ask you to annotate the tag. I recommend always creating annotated tags.

$git tag -a v1.0Copy the code

When you run git tag -a, Git opens your editor and lets you write a tag comment just like you would for a commit.

Now, notice that when we execute git log –decorate, we can see our tags:

* d5e9fc2 (HEAD -> master) Merge branch 'change_site' |\ | * 7774248 (change_site) changed the sunjiaoshou.php * | C68142b modifying code | / * c1501a2 removed test. TXT, add sunjiaoshou. PHP * 3 e92c19 add test. TXT * 3 b58100 version submitted for the first timeCopy the code

If we forget to tag a submission and publish it, we can append tags to it.

For example, suppose we published commit 85FC7e7 (last line of the example above), but forgot to label it at that time. We can also now:

$git tag -a v0.985fc7e7 $git log --oneline --decorate --graph * d5e9fc2 (HEAD -> master) Merge branch 'change_site' | \ | * 7774248 (change_site) changed the sunjiaoshou. PHP * | | c68142b modified code / * c1501a2 removed test. TXT, add Sunjiaoshou.php * 3e92C19 add test.txt * 3b58100 (tag: v0.9) First version submittedCopy the code

If we want to view all the labels we can use the following command:

$git tag v0.9 v1.0Copy the code

Command to specify label information:

git tag -a <tagname>-m "Runoob.com tag"Copy the code

PGP signature label command:

git tag -s <tagname>-m "Runoob.com tag"Copy the code

 

Git Remote Repository (Github)

Git does not have a central server like SVN.

Git commands are executed locally if you want to share your code with Git or collaborate with other developers. You need to put the data on a server that other developers can connect to.

This example uses Github as a remote repository,

Adding remote Libraries

To add a new remote repository, specify a simple name for future reference in the following format:

git remote add [shortname] [url]
Copy the code

This example uses Github as an example as a remote repository. If you do not have Github, you can register at github.com/.

Since transfers between your local Git repository and GitHub repository are encrypted over SSH, we need to configure authentication information:

Use the following command to generate an SSH Key:

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

The following [email protected] is changed to the email address you registered on Github. After that, you will be asked to confirm the path and input the password. In this case, we just use the default enter.

SSH folder will be generated under ~/. Go to id_rsa.pub and copy the key inside.

$ ssh-keygen -t rsa -C "[email protected]" Generating public/private rsa key pair. Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Your public key has been saved in/ Users/tianqixin/.ssh/id_rsa /Users/tianqixin/.ssh/id_rsa.pub. The key fingerprint is: SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI [email protected] The key's randomart image is: +---[RSA 3072]----+ |E*+.+=**oo | |%Oo+oo=o. . | |%**.o.o. | |OO. o o | |+o+ S | |. | | | | | | | +----[SHA256]-----+Copy the code

Go back to Github and go to Account => Settings.

 

Select SSH and GPG keys from the left, then click on the New SSH Key button,title set the title, you can fill in anything, paste the key generated on your computer.

 

The following information is displayed

To verify success, type the following command:

$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no/[fingerprint])? Yes # Input yes Warning: Permanently added 'github.com,52.74223.119.' (RSA) to the list of known hosts.
Hi tianqixin! You'Ve successfully authenticated, but GitHub does not provide shell accessCopy the code

The following command indicates that we have successfully connected to Github.

Then login and click “New Repository” as shown below:

Create git Repository runoob-git-test in the Repository name, keep the default Settings, and click on the “Create Repository “button.

 

If the vm is successfully created, the following information is displayed:

 

The above information tells us that we can clone a new repository from this repository or push content from the local repository to the GitHub repository.

Now let’s follow GitHub’s prompt and run the command in the local repository:

$mkdir runoob-git-test $CD runoob-git-test $Git init $Git add readme. md $Git add $Git [master (root-commit) 0205aab] add readme.md file 1 file changed, 1 Insertion (+) Create mode 100644 README. Md [email protected]:tianqixin/runoob-git-test.git $ git push -u origin masterCopy the code

Please copy the following command according to where you successfully created the new repository on Github, not according to the command I provided, because our Github user name is different, the repository name is also different.

If you want to go back to Github repository, you can see that the file has been uploaded to Github:

View the current remote library

To see which remote repositories are currently configured, run the following command:

git remote
Copy the code

The instance

$ git remote
origin
$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)
Copy the code

When executed with the -v argument, you can also see the actual link address for each alias.

Extract remote warehouse

Git has two commands to extract updates to remote repositories.

1. Download new branches and data from remote repository:

 

git fetch
Copy the code

After this command is executed, you need to execute git merge to remote branch.

2. Extract data from the remote repository and try merging it into the current branch:

git merge
Copy the code

Git fetch git merge Git fetch git merge git fetch

Suppose you’ve configured a remote repository and you want to fetch updated data, you can first tell Git to fetch data that it doesn’t have by executing git fetch [alias]. You can then execute git merge [alias]/[branch] to merge any updates on the server (assuming someone pushed to the server at this time) into your current branch.

Next, click on “readme. md” on Github and modify it online:

Then we update it locally.

$ git fetch origin remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:tianqixin/runoob-git-test 0205aab.. febd8ed master -> origin/masterCopy the code

Above information “0205aab.. Febd8ed master -> origin/master” indicates that the master branch has been updated, we can use the following command to synchronize the update to the local:

$ git merge origin/master Updating 0205aab.. febd8ed Fast-forward README.md | 1 + 1 file changed, 1 insertion(+)Copy the code

View the contents of the readme.md file:

$cat README. Md # Git test #Copy the code

Push to remote repository

Command to push your new branch and data to a remote repository:

git push [alias] [branch]
Copy the code

The above command pushes your [branch] branch to the [branch] branch on the [alias] remote repository, as shown in the following example.

$git add SUNJIAOshou -test. TXT $git commit -m "add to remote" master 69e702d] add to remote 1 file Changed, 0 insertions(+), 0 deletions(-) create mode 100644 SUNjiaoshou -test.txt $git push origin master #Copy the code

Go back to our Github repository and see that the file has been submitted:

Delete the remote repository command

To delete a remote repository you can use the following command:

Git remote rmCopy the code

The instance

$ git remote -v origin [email protected]:tianqixin/runoob-git-test.git (fetch) origin [email protected]: tianqixin/runoob - git - test. Git (push) # add warehouse origin2 $git remote add origin2 [email protected]:tianqixin/runoob-git-test.git $ git remote -v origin [email protected]:tianqixin/runoob-git-test.git (fetch)  origin [email protected]:tianqixin/runoob-git-test.git (push) origin2 [email protected]:tianqixin/runoob-git-test.git (fetch) Origin2 [email protected]: tianqixin/runoob - git - test. Git (push) # delete warehouse origin2 $git remote rm origin2 $git remote - v origin [email protected]:tianqixin/runoob-git-test.git (fetch) origin [email protected]:tianqixin/runoob-git-test.git (push)Copy the code

Git Gitee

We all know that the speed of accessing Github in China is relatively slow, which affects our use.

If you want to experience the speed of Git flying, you can use the domestic Git hosting service — Gitee (gitee.com).

Gitee provides a free Git repository and integrates features such as code quality inspection and project presentation. For collaborative team development, Gitee also provides project management, code hosting, document management services, free for small teams of less than 5 people.

Let’s learn how to use Gitee.

Since the transport between our local Git repository and Gitee repository is encrypted over SSH, we need to configure the authentication information.

1, We first inGiteeAfter registering and logging in, upload your SSH public key.

We have already generated our SSH public key in Git Github, so we just paste the contents of the ~/.ssh/id_rsa.pub file in the user’s home directory into Gitee.

Select “SSH Public Key “, fill in a title that is easy to identify, and paste the contents of the.ssh/id_rsa.pub file in the user’s home directory:

The following figure shows the successful addition:

Next we create a project.

Click the + sign in the upper right corner to create a new warehouse:

Then add the warehouse information:

The following information is displayed:

Let’s look at the connection information:

 

 

It is best to keep the project name consistent with the local library.

Git remote add > git remote add > git remote add > git remote add > git remote add

git remote add origin [email protected]:imnoob/sunjiaoshou-test.git
Copy the code

Git push and Git pull are now available!

Git remote add git remote add

git remote add origin [email protected]:imnoob/sunjiaoshou-test.git
fatal: remote origin already exists.
Copy the code

Git remote -v git remote -v origin origin

git remote -v
origin    [email protected]:tianqixin/sunjiaoshou.git (fetch)
origin    [email protected]:tianqixin/sunjiaoshou.git (push)
Copy the code

 

As you can see, the local library has been associated with origin’s remote library, and that remote library points to GitHub.

We can delete the existing GitHub remote library:

git remote rm origin
Copy the code

Re-associate Gitee’s remote library (note that the path must be filled with the correct user name) :

git remote add origin [email protected]:imnoob/sunjiaoshou-test.git
Copy the code

At this point, let’s look at the remote library information:

git remote -v
origin    [email protected]:imnoob/sunjiaoshou-test.git (fetch)
origin    [email protected]:imnoob/sunjia0sou-test.git (push)
Copy the code

You can now see that Origin has been associated with Gitee’s remote library.

You can push local libraries to Gitee with git push.

Can a local library be associated with both GitHub and Gitee?

The answer is yes, because Git itself is a distributed version control system that can be synchronized to one or two other remote libraries.

Git’s default name for the remote library is Origin. If you have multiple remote libraries, you will need to use different names to identify the different remote libraries.

Again using the sunjiao-test local library as an example, let’s first delete the associated remote library named Origin:

git remote rm origin
Copy the code

Then, associate GitHub’s remote library first:

git remote add github [email protected]:tianqixin/sunjiaoshou-git-test.git
Copy the code

Note that the name of the remote library is github, not Origin.

Then, associate Gitee’s remote library:

git remote add gitee [email protected]:imnoob/sunjiaoshou-test.git
Copy the code

Also note that the name of the remote library is Gitee, not Origin.

Git remote v git remote v git remote v

git remote -v
gitee    [email protected]:imnoob/sunjiaoshou-test.git (fetch)
gitee    [email protected]:imnoob/sunjiaoshou-test.git (push)
github    [email protected]:tianqixin/sunjiaoshou.git (fetch)
github    [email protected]:tianqixin/sunjiaoshou.git (push)
Copy the code

To push to GitHub, use the following command:

git push github master
Copy the code

To push to Gitee, use the command:

git push gitee master
Copy the code

This way, our local library can synchronize with multiple remote libraries at the same time:

 

 

Git server Setup

Our remote repository uses Github. Github public projects are free, and from 2019, Github private repositories can be used without limit.

Of course, we can also set up a Git server as a private repository.

Next, we will take Centos as an example to set up Git server.

1. Install Git

$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git
Copy the code

Next we create a git user group and user to run git services:

$ groupadd git
$ useradd git -g git
Copy the code

2. Create a certificate for login

Collect all need to log in to the user’s public key, public key in id_rsa. Pub file, our public key into the/home/git /. SSH/authorized_keys file, a line of one.

If there is no file to create it:

$ cd /home/git/
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys
Copy the code

Initialize Git repository

Git repository: /home/gitrepo/runoob.git

$ cd /home
$ mkdir gitrepo
$ chown git:git gitrepo/
$ cd gitrepo

$ git init --bare runoob.git
Initialized empty Git repository in /home/gitrepo/sunjiaoshou.git/
Copy the code

Git creates an empty repository. Git repositories on servers usually end with.git. Then change the repository owner to git:

$ chown -R git:git sunjiaoshou.git
Copy the code

4. Clone warehouse

$git clone [email protected]: / home/gitrepo/sunjiaoshou git Cloning into 'runoob'... warning: You appear to have cloned an empty repository. Checking connectivity... done.Copy the code

192.168.45.4 is the Git server IP address. You need to change it to your own Git server IP address.

This completes our Git server installation.

 

 

Recommended reading

Git simple tutorial: https://blog.csdn.net/weixin_41937552/article/details/106313322

Welcome to follow my public account: e-commerce programmer, reply keyword “Git common command” to obtain Git common command atlas