Git study notes,
Git Tutorial:
I. Basic git operations-submit and publish
Create a local version Library: git init
Add a file: git add
Submit a file to the local version Library: git commit-m description
View the status of the current document: git status
View the modified content before add: git diff file name
View submitted logs: git log
View the command history: git reflog
Ii. git Version Control
The current version of git is HEAD, and the previous version is git reset -- hard HEAD ^.
Git returns to any version: git reset -- hard commit -- id
Discard unadded version: git checkout -- file
Readme.txt has not been put into the temporary storage zone after modification. Now, undo the modification and return to the same status as the version database;
One is that readme.txt has been added to the temporary storage area and is modified again. Now, undo the modification and return to the status after it is added to the temporary storage area.
Scenario 1: When you change the content of a file in the workspace and want to discard the modification, run git checkout -- file.
Scenario 2: When you not only disrupt the content of a file in the workspace, but also add the file to the temporary storage area, you want to discard the modification in two steps. The first step is to use the command git reset HEAD file, return to scenario 1. Step 2: perform operations based on scenario 1.
Scenario 3: If you want to cancel the submission when you have submitted an inappropriate modification to the version library, refer to the version rollback section, provided that the submission is not pushed to the remote database.
View the remote database information: git remote-v
Delete: if a file is deleted in the Workspace
1. If you are sure you want to delete it, use git rm file to delete it from the version library and commit
2. If you accidentally delete the file, you need to restore the file from the version Library: git checkout -- file
3. Connect to the remote warehouse:
1. Generate the key: ssh-keygen-t rsa-C "18500524969@qq.com", which can be found in the user directory.
2. Register a github account and add the key to gitHub.
3. connect to a remote database
1. No local repository is created
Echo "# learngit"> README. md
Git init
Git add README. md
Git commit-m "first commit"
Git remote add origin https://github.com/jiangerOne/learngit.git
Git push-u origin master
2. If you have created a local repository
Git remote add origin https://github.com/jiangerOne/learngit.git
Git push-u origin master
4. Clone from remote database
Git clone git@github.com: jiangerOne/javaProject. git
4. branch operations:
1. Basic branch operations: the operations of a branch do not affect the data of other branches. To synchronize, merge the branches.
View the current branch: git branch
Create a new branch: git branch dev
Switch to dev: git checkout dev
Git checkout-B dev = git branch + git checkout dev
Merge a branch to the current branch: git merge dev
Delete branch: git branch-d dev
2. Branch conflict:
When a branch commits the changed content and the other branch also commits the content that changes the same row, the branch merge conflicts.
Solution: First, you can use git log -- graph.
3. Branch Policy
When merging branches, the -- no-ff parameter can be added to merge them in normal mode. after merging, there are branches in history,
We can see that we have done merging, but fast forward cannot see that we have done merging.
In actual development, branch management should follow several basic principles:
1. First of all, the master branch should be very stable, that is, it is only used to release new versions, and usually cannot work on it;
2. Where can I work? All work is on the dev branch. That is to say, the dev branch is unstable. At some point,
For example, when v10 is released, the dev branch is merged to the master and Version 1.0 is released on the master branch;
3. Everyone you and your friends work on the dev branch. Everyone has their own branch. You can merge them on the dev branch from time to time.
4. Do not submit the master branch when merging branches. Merge
4. Create a bug Branch:
1. When you work on dev, you suddenly find a bug, but dev is not finished yet. You need to save the dev environment.
Run to the master node to create a bug branch and fix the bug.
1. Save the current status of dev: git stash
2. Switch to the master branch to create a bug Branch:
Git checkout master
Git checkout-B bug -- 01
3. Fix the bug, merge the bug branches, and delete the bug branches.
Git merge bug -- 01
Git branch-d bug -- 01
4. Go back to dev, view stash, delete stash, and continue to work.
Checkout dev
Git stash list
Git stash apply recovery
Git stash pop recovery and Deletion
5. Force delete a new branch that has not been merged: Develop a new feature. It is best to create a new branch;
If you want to discard a branch that has not been merged, you can use git branch-D <name> to forcibly delete it.
6. multi-person cooperation:
However, it is not necessary to push local branches to a remote device. Which branches need to be pushed and which do not?
The master branch is the master branch, so it must be synchronized from time to time;
The dev branch is a development branch. All members of the team need to work on it, so they also need to be remotely synchronized;
Bug branches are only used to fix bugs locally, so there is no need to push them to the remote computer unless the boss wants to see if you have fixed several bugs each week;
Whether the feature branch is pushed remotely depends on whether you work with your partner to develop it.
7. multi-person Cooperation Branch:
View the remote database information and use git remote-v;
If a new local branch is not pushed to a remote server, it is invisible to others;
Use git to push origin branch-name from the local branch. If the push fails, use git pull to capture remote New commits;
Create the branch corresponding to the remote branch locally and use git checkout-B branch-name origin/branch-name. The name of the local branch should be the same as that of the remote branch;
Create an association between a local branch and a remote branch. Use git branch -- set-upstream branch-name origin/branch-name; to remotely crawl the branch and use git pull. If there is a conflict, you must first handle the conflict.
5. Tag management:
1. The main function of a tag is to better identify the version.
2. Create a tag
Command git tag <name> is used to create a new tag. The default value is HEAD. You can also specify a commit id;
Git tag-a <tagname>-m "blablabla..." can specify tag information;
Git tag-s <tagname>-m "blablabla..." You can use PGP to sign tags;
Command git tag to view all tags.
Git show tag allows you to view tag Information
3. delete a tag:
Command git push origin <tagname> to push a local tag;
Command git push origin -- tags can push all local labels not pushed;
Command git tag-d <tagname> to delete a local tag;
Command git push origin: refs/tags/<tagname> to delete a remote tag.
Vi. Custom git
Upper color
Git config -- global color. ui true
Configuration alias:
Git config -- global alias. st status