Git learning Summary (notes), git learning summary notes

Source: Internet
Author: User

Git learning Summary (notes), git learning summary notes

Note: This article is based on Liao Xuefeng's Git tutorial. I am very grateful to Liao for his selfless dedication. This article serves only as my learning notes. If you need to complete git learning, please stamp the previous link ^_^

1. What is git?
1. What is git?
"Git is the world's most advanced distributed version control system (none)" -- Liao Xuefeng
2. Why Learning git?
Git is an efficient and convenient version control tool, which can greatly improve work efficiency in actual development ~~

Ii. Install git

1 sudo apt-get install git

 

3. Create a version Library

1 [mkdir <Database Name>] 2 [cd <Database Name>] 3 git init # initialize the current directory as a git Repository

 

Iv. Version Control
1. Add the file to the version Library

1 git add <filename> # add the file to the temporary storage area. Note that the file must be placed in the repository directory (or subdirectory) next 2 git commit-m "Submit description" # submit the content of the temporary storage area to the current Branch

2. view the workspace status

1 git status # view the workspace modification record 2 git diff <filename> # If git status prompts a change record, this command can view the modification content

3. Version Switching

1 # HEAD points to the current version 2 git reset -- hard commit_id # Roll Back To The Version 3 git log [-- pretty = oneline] specified by commit_id # view the most recent commit log, add parameters in "[]" to view 4 git reflog # view command history in the format of "version number commit log"

4. Undo Modification

1 git checkout -- <file> # undo workspace modification 2 git reset HEAD <file> # undo changes to the temporary workspace and return the workspace modification status

5. delete an object

1 git checkout -- <file> # Replace the version in the repository with the version in the workspace. 2. git rm <file> # delete files in the temporary storage zone.

 

5. remote warehouse
1. Create an SSH Key:

1 # Go To. ssh directory (not created on your own): 2 ssh-keygen-t rsa-C "email@example.com" # create an SSH Key, note that fill in the correct email address 3 # log on to GitHub, open the "Account settings", "SSH Keys" page, "Add SSH Key", enter "Title", and copy the content of id_rsa.pub to the Key text box, "Add Key"

2. Add a remote database

1 # log on to GitHub and "Create a new repo" to Create a new repository. 2. git remote add origin git @ server-name: path/repo-name.git # associate the local repository to the remote repository 3 git push [-u] origin master # push the latest changes, add the-u parameter for the first push

3. Clone from remote database

1 git clone git@server-name.com: path/name. git # clone a remote Library to the current directory

 

Vi. Branch Management
1. Create and merge branches
What is the role of a branch: when a task in a development project is completed, the new branch can be used, and then merged to the master branch to delete the new branch, in this way, you can manage projects securely, because operations on a branch only modify the content contained in the branch, which is safer.

1 git branch <name> # create branch 2 git checkout <name> # Switch branch 3 git checkout-B <name> # create and switch 4 git merge <name> # merge branch current branch 5 git branch-d <name> # Delete branch 6 git log -- graph # view branch merge graph

2. Branch Management Policy
Fast forward mode: This mode is the default mode for Branch merge. In this mode, branch information is lost after the branch is deleted. This mode is not recommended in actual development.

1 git merge -- no-ff-m "XXX" B-name # "-- no-ff" parameter indicates disabling the Fast forward mode

Basic Principles of branch management:
① The master branch is only used to release a new version;
② All team members work on the dev branch, and everyone works on a Subbranch of the dev branch;

3. bug Branch

1 git stash # Save the current "job site" 2 git stash apply # restore the site but not delete the stash content 3 git stash drop # Delete the stash content 4 git stash pop # restore the site delete stash content 5. git stash list # view stash content

Bug fixing policy: Create a new branch to fix the bug, merge and delete it.
Steps: save the current "work site" --> Create and switch to the bug Branch --> fix the bug --> switch to the upper branch of the bug Branch --> merge and delete the bug Branch --> switch back to the original work Branch --> restore "work site ".
4. feature Branch
New feature policy: Create a new branch and merge and delete it.

1 git branch-D <name> # forcibly delete an unmerged branch

5. multi-person collaboration

1 git remote-v # view remote database information 2 git pull # capture the latest commit to the local repository 3 git push origin branch-name # push local branches to the remote repository 4 git checkout- B branch-name origin/branch-name # create a local branch and the branch corresponding to the remote branch 5 git branch -- set-upstream branch-name origin/branch-name # create a local branch and remote branch branch Association

Working Mode of multi-person collaboration:
① Try to use git to push origin branch-name to push your modifications;
② If the push fails, because the remote branch/branch ratio is updated to the local branch, you need to use git pull to capture it locally and then try to merge it.
③ If a merge conflict exists, the conflict is resolved and submitted locally;
④ Use git to push origin branch-name after there is no conflict or the conflict is resolved

VII. Tag Management
1. Create tags

1 git tag # view all tags 2 git show <tagname> # view tag details 3 git tag <tagname> [commit id] # create a new tag. The default value is HEAD, you can also specify a commit id4 git tag-a <tagname>-m "xxx" # specify the tag information as "xxx" 5 git tag-s <tagname>-m "xxx "# use PGP to sign tags

2. Operation tags

1 git tag-d <tagname> # delete a local tag 2 git push origin -- tags # push all local tags not pushed 3 git push origin <tagname> # push a local tag 4 git push origin: refs/tags/<tagname> # delete a remote tag

 

8. use GitHub
On GitHub, you can use any Fork open-source repository and have the read and write permissions of the Fork repository. You can push the pull request to the official repository to contribute code.

9. Custom Git
1. Ignore special files
① When ignoring some files, you need to write. gitignore and add the file name to be ignored in the file;
② The. gitignore file should be placed in the version library and version management can be performed on. gitignore;
2. Configure aliases
① Git config [-- global] alias. new-name old-name # Use new-name as the alias of old-name, and add -- global to modify the current user. If this parameter is not added, only the current repository is modified;
② In addition to using the config parameter, you can also modify the configuration file to configure the alias: the configuration file of Each repository is placed in. in git/config, the current user's configuration file is placed in the Home directory. gitconfig;
3. Build a Git Server
① Install git: sudo apt-get install git
② Create a git User: sudo adduser git
③ Create a certificate Logon: Collect all the user public keys to be logged on, that is, id_rsa.pub. Import all the public keys to the/home/git/. ssh/authorized_keys file with one row;
④ Initialize Git Repository: first select a directory as the Git repository. Assume It is/srv/sample. git: Enter sudo git init -- bare sample in the/srv directory. git, and change the repository owner to git: sudo chown-R git: git sample. git
⑤ Disable shell login: put a line like this in the/etc/passwd file: git: x: 1001: 1001 :... :/home/git:/bin/bash changed to git: x: 1001: 1001 :... /home/git:/usr/bin/git-shell;
6. clone a remote Repository: git clone git @ server:/srv/sample. git

Conclusion: git contains a lot of parameters and usage, but it is often used less often. You should be able to use it after more exercises.

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.