git and github build and use tutorial under linux

Posted by 2oMst on Thu, 13 Jan 2022 21:18:55 +0100

1: Building git and github under linux

1. Registered gihub account:

github official website: https://github.com/ You can create it yourself.
Log in, create repository, create a new warehouse, and create a test library test. The creation is complete.

2. linux Installation git environment

yum -y install git

3. Generate ssh key

The SSH keygen command generates the key, which can be directly defaulted, or you can choose your own encryption method, such as rsa encryption method

ssh-keygen -t rsa

Press enter all the time by default, and the generation is as follows
The production key is produced in the home directory In the ssh directory, view the public key:

cat ./ssh/id_rsa.pub

4. Add public key in github

Log in to github and add the public key address in the option setting > > SSH and GPG key > > add new SSH.
Just fill in the title

After adding, use ssh -T git@github.com Command to detect success.

ssh -T git@github.com

The following message indicates success.

[root@localhost ]# ssh -T git@github.com
Hi ! You've successfully authenticated, but GitHub does not provide shell access.
[root@localhost ]#

5. Configure username and email of git parameter

This is because Git is a distributed version control system, so every machine must report its home: your name and Email address.

git config --global user.name "your name"   //Configure user name
git config --global user.email "your email"    //Configure email 

After configuration, you can view relevant parameters

 git config -l

2: Git is uploaded from the local repository to GitHub

1. Create and enter a directory as a local library

Here, take the testdir directory as an example to create readme MD file as an example.

 mkdir testdir && cd testdir
 touch README.md

Initialize a local library

git init 

After initialization, a hidden folder will appear in this directory get

2. Add local libraries and push code files

Add files to local warehouse

git add README.md 

Submit to the local library and note that the change is still local at this time.

git commit -m "first commit"

Add an alias of the remote server, which is also the name of the local warehouse. The format is:
remote add alias git@github.com:github username / warehouse name git
Add an alias test here_ Readme, the user name is theonyu, and the warehouse name is test.

git remote add test_readme git@github.com:theonyu/test.git

Submit the local file (alias just now) to GitHub's test library. At this time, the local changes are updated to the GitHub service

git push -u test_readme master

Submitted successfully:
Check in github and the file has been uploaded successfully

Other command references are as follows:

git add README.md #Add files to local warehouse
git rm README.md #Delete in local warehouse
git commit -m "first commit" #Submit to the local library and note that the change is still local at this time.
git commit -a  ##Automatically update changed files, a can be understood as auto
git remote add xxx git@github.com:xxx/xxx.git  #Add an alias for the remote server.
git remote rm xxx   ##Delete alias for remote version Library
git push -u remotename master #Submit the local file to GitHub's remoname version library. At this time, the local changes are updated to the GitHub service

3: Synchronize github to local repository

In the github repository, you can see the code option on the right. There are three ways
Generally, ssh can be used in linux, such as the readme just uploaded MD download to local

git clone git@github.com:theonlyu/test.git

After downloading, the current directory exists as a folder with the warehouse name.

Download method difference reference:

git clone git://github.com:xxxx/test.git ## is cloned locally in gitreadonly mode and can only be read
git clone git@github.com:xxx/test.git  ##It is cloned locally by SSH and can be read and written
git clone https://github.com/xxx/test.git ## is cloned locally by https and can be read and written
git fetch git@github.com:xxx/xxx.git  ##Get local without merging
git pull git@github.com:xxx/xxx.git ##Get and merge content locally

4: Branch management of GitHub

1. Create branch

Example: create a branch in the local warehouse directory Txt is submitted as a branch.

git branch #Displays whether the current branch is master
git branch new-txt  #Create a branch named new TXT
git checkout new-txt  #Switch to new branch
touch branch.txt
git add branch.txt
git commit -a -m "added branch.txt"
git push test_readme new-txt  ##Submitting the branch to the remote server only submits the branch structure and content to the remote server without merging with the trunk.

In the upper left corner of github, you can see that the new TXT branch has been uploaded successfully, and the file also exists. The master does not have a new file because it has not been merged.

2. Merge branch

If the new feature branch is mature, I think it is necessary to merge it into the master

git checkout master  #Switch to new backbone
git merge new-txt  ##Merge branches into trunk
git branch #Displays whether the current branch is master
git push test_readme master #At this time, the new TXT code, test, is also merged in the trunk_ Readme is the warehouse name

Successfully merged in github, the new file branch Txt also exists.

3. Other commands:

#Update remote branch list
git remote update Warehouse alias --prune

#View all branches
git branch -a

#Delete remote branch
git push Warehouse alias --delete Branch name

#Delete local branch
git branch -d Branch name

Topics: Linux Operation & Maintenance git github