Common git-related knowledge and commands

Source: Internet
Author: User
Tags create directory

install git program

Contos

Yum Install–y Epel-release

Yum install–y git

Ubuntu

Sudo apt-get Install git

Install on Windows

Https://git-scm.com/download/win

The final step is required after Setup is complete

git config--global user.name "Qiang"//For example Qiang

git config--global user.email "zhiqiangwang@aliyun.com"//write to your mailbox

Create a version repository and push files

Mkdir/home/gitroot//Create a directory

Cd/home/gitroot//Enter directory

Git init//Initialize with command. Let this directory program a repository that git can manage

Ls–a//You can see the. git directory

Echo–e "QIANG" >1.txt//Create a file 1.txt

Git Add 1.txt//adding 1.txt to the Warehouse

Git commit–m "Add new file 1.txt"//add It's gotta be conmit to actually commit the file to the Git repository.

Echo–e "QIANG QIANG" >> 1.txt//Change 1.txt

Git Status//view the status in the current repository, such as whether there are any changes to the file

Git checkout--1.txt//to overwrite 1.txt in the warehouse with local 1.txt

Git diff 1.txt/Can compare 1.txt this time what content modified, compared to the version of the warehouse

deleting files

1. Git add test.txt

2. Git commit-m "Add test.txt"

3. RM test.txt

4. Git status

5. Git rm test.txt

6. RM ' Test.txt '

7. Git commit-m "Remove test.txt"

Upload all files in the current directory

1. Git Add.

2. Git commit-m "Updata all"

3. Git push

Changes to the version

Change 1.txt multiple times and make a git add,git commit operation

git log//can view all warehouse record operations that commit to a git repository

Git log–pretty=oneline a row of rows to display

With git log, you can view all the versions that were submitted in the past, so you can specify a fallback version based on this log

Git Reset–hard d03da70182c1e78d04df1d7eee2f6a972ae4f82b//Can fallback this version, here is a long string, can be abbreviated (first 5 characters)

Git Reflog//can show all versions

File recovery

Modify the status of the last commit when modifying the 1.txt discovery is not correct

Git checkout–1.txt//revert to the state of the last commit

If the 1.txt modification is complete, after saving, git add 1. txt but without git commit, and then want to fall back to the last commit is the state, you can use

Git Reset HEAD 1.txt

and then

Git Checkout–1.txt

File deletion

Echo "QIANG" >2.txt

Git Add 2.txt

Git commit–m "Add new File 2.txt"

Rm–f 2.txt

Git status

Git RM 2.txt

Git commit–m "Delete 2.txt"//delete 2.txt completely

To create a remote repository

1. First register a free warehouse https://github.com/or https://git.oschina.net/

2. Permission authentication, add key

Github:setting->ssh and GPG keys->new SSH key->title and key input in

Oschina: Profile->ssh Public key, add public key, title and public key entry

Generate Key pair: Ssh-keygen

Liunx:cat/root/.ssh/id_rsa

Cat/root/.ssh/id_rsa.pub//Public key

Windwos:cat C:/users/cmd/.ssh/id_rsa

Cat c:/users/cmd/.ssh/id_rsa.pub

Add successfully received message

3. Create a linked warehouse

3.1 Mkdir/home/gitroot//Create Directory

3.2 Cd/home/gitroot//Enter directory

3.3 Git init//Initialize with command. Let this directory program a repository that git can manage

3.4 git remote add Origin warehouse address

Git remote add Origin git@git.oschina.net:zhiqiangwang/qiang.git

3.4.1 origin is your repository alias can be changed casually, but please do not conflict with the existing warehouse alias

3.4.2 Warehouse address Generally speaking support

3.5 echo–e "QIANG" >qiang.txt//New Qiang.txt

3.6 git Add Qiang.txt

3.7 git commit–m "add Qiang.txt"

3.8 Git push–u Origin master//push local Qiang warehouse to remote Qiang

3.9 echo–e "QIANG QIANG" >>qiang.txt//Append Qiang.txt QIANG after QIANG

3.10 git add Qiang.txt

3.11 git commit–m "Update Qiang.txt"

3.12 git push//pushes the local Qiang repository to the remote Qiang again

Cloning a remote repository

Git Clone Git@git.oschina.net:zhiqiangwang/qiang.git

Branch Management

Git Branch View Branches

Git Branch Wang to create a branch

Git checkout Wang switch Wang branch

Branch Merge

Git Merge Wang//merge Wang branch into master

Common problems with merging

1. If both the master branch and the Wang Branch edit the Qiang.txt, when the merge prompts a conflict, resolve the conflict before you can continue the merge

2. The conflict resolution method is to edit the Qiang.txt under the Master branch, change the contents of the Qiang.txt inside the Wang Branch, and then submit the Qiang.txt for merging

3. If the master branch changes what we want, you can edit the Qiang.txt content, change it to the one you want, then commit, switch to the Wang Branch, and then merge the Master branch to the Wang Branch. The branch name followed by the merge must be the newest branch.

4. Git branch–d Wang//delete branch

5. Git Branch–d Wang/If the branch is not merged, force delete

Principles of using branching

The master branch is very important and is used only when the code is published online.

The Dev branch is dedicated to development, and the Dev branch is merged into master before the important release line

Developers should only develop the code on the basis of dev, branching into personal branches, and then merging into the Dev branch

The merge Bob Branch command in the Dev branch is

Git Checkout dev//switch Dev branch first

Git Merge Bob

Onsite Retention

Edit Wang.txt in Wang Branch

Go to Zhi branch to fix the bug, so git add wang.txt is needed first

Then git stash keep the scene

Then switch Zhi Branch repair, after fixing the bug, in the cut back to Wang branch

Git Stash list to see the live we've kept

Git Stash Apply Recovery site

Git Syash Apply stash@{0} back to the specified site

Remote Branch

git remote–v//view Remote Library information

git ls-remote origin//View Remote Branch

Push local branch Wang to remote branch Wang and establish an association relationship

A. Remote already has Wang Branch and has associated local branch Wang and has switched to Wang locally

git push

B. Remote already have Wang branch but not associated local branch Wang and Local has switched to Wang

Git push-u Origin/wang

C. The remote has no Wang branch and the Local has switched to Wang

Git push Origin Wang:wang

Delete local Branch

Git branch–d|-d Wang

Delete Remote Branch

git push Origin:wang

Git branch-m | -M oldbranch newbranch Rename the branch, if the Newbranch name branch already exists, you need to use-m to force the rename, otherwise, use-M to rename.

Label Management

A tag class is a snapshot feature that labels a repository, records the state of a moment, or restores that state at any time.

1. Git checkout master first switch on master

2. Git tag v1.0 a tag to master v1.0

3. Git tag to see all the tags

4. Git log–-pretty=oneline–-abbrev-commit//View history commit

5. Git tag v0.9 d03da//tag for history commit

6. Git tag–a–m "tag just v1.1" d03da//tags can be described

7. Git tag–d v0.8 Delete tags

8. Git push origin v1.0//push to the specified label remote

9. Git push--tag origin//push all tags

git tag–d//Delete local tags

git push origin:refs/tags/v1.0 Delete remote tags

Build a git server

1. Yum Install git

2. Add the git user and set the shell as/usr/bin/git-shell to keep git users from logging in

Useradd–s/usr/bin/git-shell

3. Cd/home/git

4. Create a Authorized_keys file, change the owner, belong to the combination of permissions, to save the public key on the client machine

5. Mkdir.ssh

6. Ssh-keygen//Use this command to generate a key pair

7. Touch Authorized_keys

8. Cat/home/git/id_rsa.pub>>/home/git/.ssh/authorized_keys.

9. Chown–r git.ssh or Chown. Ssh/authorized_keys

10. Define the directory where the git repository is stored/data/gitroot

Mkdir/data/gitroot

Cd/data/gitroot

git init–bare sample.git//Create a bare repository without a workspace because the GIT repository on the server is purely contributing, so the user is not allowed to log on to the server to change workspaces, and the Git repository on the server ends with. git

Chown–r Git.git Sample.git

The above operation is done on the GIT server, usually git server does not log on to the developer of Western medicine to modify the code, he just serves as a server role, like GitHub, usually on our own PC to do

Cloning a remote warehouse on the client

Git Clone Git@ip:/data/gitroot/sample.git

At this point you can go to the sample directory, this is our clone remote repository. Go into this area to develop and push to remote

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.