Home  >  Article  >  Backend Development  >  Git common knowledge and commands

Git common knowledge and commands

巴扎黑
巴扎黑Original
2016-11-07 10:16:351036browse

Install the git program

ContOS

                                                                                             git

Installation on Windows

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

After the installation is completed, the final step of setting is required

        git config --global user.name "Qiang" // Such as Qiang

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

Create a version repository and push the file

Mkdir /home/gitroot //Create a directory

Cd /home/gitroot //Enter the directory

Git init //Use Command initialization. Let this directory program a warehouse that can be managed by git

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

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

Git add 1.txt / /Add 1.txt to the repository

Git commit –m “add new file 1.txt” //After adding, you must commit to actually submit the file to the git repository

Echo –e “QIANG QIANG”> > 1.txt //Change 1.txt

Git status //Check the status of the current warehouse, such as whether there are any changed files

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

Git diff 1.txt / You can compare what has been modified in 1.txt this time, compared with the version in the warehouse

Delete the file

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. remove test.txt"

Upload all files in the current directory

1. git add .

2. git commit -m "updata all"

3. git push

Version changes

many times Change 1.txt and perform git add, git commit operations

Git log //You can view all warehouse record operations submitted to the git warehouse

Git log –pretty=oneline Display line by line

You can view past submissions through git log All versions, so you can specify a certain version to roll back according to this log

Git reset –hard d03da70182c1e78d04df1d7eee2f6a972ae4f82b //You can roll back this version. Here is a long string, which can be abbreviated (the first 5 characters)

Git reflog //Yes Show all versions

File recovery

When you modify 1.txt and find that the modification is wrong, you want to modify the status of the last submission

Git checkout –1.txt//Restore to the status of the last submission

If 1 The .txt modification is completed. After saving, git add 1.txt is created but there is no git commit. If you want to go back to the state of the last commit, 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”//Completely delete 2.txt

Create a remote warehouse

1. Register a free warehouse first https:/ /github.com/ or https://git.oschina.net/

2. Permission authentication, add key

github:Setting->SSH and GPG keys->new SSH key->Enter title and key

oschina: Profile->SSH Public Key->Add Public Key->Enter title and public key

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 and receive email

3. Create a create link 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 warehouse that can be managed by git

3.4 git remote add origin warehouse address

git remote add origin git@git.oschina.net:zhiqiangwang/Qiang.git

3.4.1 origin is the alias of your warehouse. Change it as you like, but please be sure not to conflict with the existing warehouse alias

3.4.2 Warehouse addresses are generally supported

3.5 Echo –e “QIANG”>Qiang.txt //Create a 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 the local Qiang warehouse to the remote Qiang

3.9 Echo –e “QIANG QIANG”>>Qiang.txt //Append QIANG QIANG after Qiang.txt

3.10 git add Qiang.txt

3.11 git commit –m “update Qiang.txt”

3.12 git push //Push the local Qiang warehouse to remote Qiang again

个 一 a remote warehouse

git clone git@git.oschina.net: zhiqiangwang/qiang.git

branch management

git Branch wang

git checkout Wang Switch the wang branch

Branch merge

Git merge wang //Merge the wang branch to master

Common problems with merging

1. If both the master branch and the wang branch edit Qiang.txt, a prompt will appear when merging Conflict, please resolve the conflict first before continuing the merge

2. The way to resolve the conflict is to edit Qiang.txt under the master branch and change it to the content of Qiang.txt in the wang branch, then submit Qiang.txt and merge

3. If the changed content of the master branch is what we want, we can edit the content of Qiang.txt, change it to what we want and submit it, switch to the wang branch, and then merge the master branch to the wang branch. The branch name following Merge must be the latest branch

4. Git branch –d wang //Delete the branch

5. Git branch –D wang /If the branch is not merged, force deletion

Principles of using branches

The Master branch is very important. This branch is only used when releasing code online

The Dev branch is specially used for development. The dev branch will be merged into master before important releases are released online

Developers should only be dev On the basis of branching into a personal branch, develop the code in the personal branch, and then merge it into the dev branch

The command to merge the bob branch in the dev branch is

Git checkout dev //Switch the dev branch first

Git merge bob

Keep it on site

Edit wang.txt

in the wang branch and go to the zhi branch to fix the bug, so you need to git add wang.txt

and then git stash to keep it on site

and then switch to the zhi branch to fix it. After fixing the bug, switch back wang branch

Git stash list can view the site we have retained

Git stash apply restore the site

Git syash apply stash@{0} restore to the specified site

Remote branch

git remote –v //View Remote library information

git ls-remote origin//View the remote branch

Push the local branch wang to the remote branch wang and establish an association

a. The remote already has the wang branch and has been associated with the local branch wang and the local branch has been switched to wang

git push

b. The remote already has the wang branch but it is not associated with the local branch wang and the local branch has been switched to wang

git push -u origin /wang

c. The remote does not have the wang branch and the local branch has been switched to wang

git push origin wang: wang

Delete the local branch

git branch –d|-D wang

Delete the 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 rename, otherwise, use -m to rename.

Tag management

The tag class is based on the snapshot function. It gives a tag to a repository, records the status at a certain moment, and can also restore the status at any time

1. git checkout master First switch to the master

2 . git tag v1.0 Tag master v1.0

3. git tag View all tags

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

5. git tag v0.9 d03da//Tag historical commits

6. git tag –a –m “tag just v1.1” d03da//You can describe the tag

7. git tag –d v0.8 Delete tag

8. git push origin v1.0//Push to the specified tag remotely

9. git push --tag origin//Push all tags

10. git tag –d//Delete local tags

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

Build a git server

1. Yum install git

2. Add a git user and set the shell to /usr/bin/git- shell, the purpose is to prevent git users from logging in remotely

Useradd –s /usr/bin/git-shell

3. Cd /home/git

4. Create the authorized_keys file, change the owner, and the combined permissions, use 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 600 .ssh/authorized_keys

10. Define the directory /data/gitroot to store the git repository

Mkdir /data/ gitroot

Cd /data/gitroot

11. Git init –bare sample.git //Create a bare warehouse. The bare warehouse has no workspace. Because the git warehouse on the server is purely for contribution, users are not allowed to log in to the server. Change the workspace, and the git warehouse on the server ends with .git

12. Chown –R git.git sample.git

The above operations are done on the git server. Usually the git server does not allow developers to log in to modify Code, it just acts as a server, just like github, it is usually done on our own PC

Clone the remote warehouse on the client

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

You can enter the sample directory at this time. This is where we clone the remote warehouse. Enter here for development, and then push to the remote


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn