Git- Basics
Git- Git
Basic introduction
Git is an open source distributed version control system designed to handle project version management from very small to very large projects efficiently and quickly.
Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.
features
From the perspective of the average developer, Git has the following features:
1. Clone the database from the server
2. Create branches in your own machine and modify the code
Commit code on a branch you created on a single machine
4. Merge branches on a single machine
5. Create a new branch, fetch the latest version of code from the server, and merge it with your main branch.
6. Generate patches and send them to the lead developers.
7. Looking at the lead developer’s feedback, if the lead developer sees a conflict between two normal developers (a conflict that they can work together to resolve), he will ask them to resolve the conflict first and then one of them will submit it. If the lead developer can work it out on his own, or if there are no conflicts, pass.
8, the general method of conflict resolution between developers, developers can use the pull command to resolve the conflict, after resolving the conflict to submit patches to the main developer.
Git operation method
1. Configure the current user information
1. Check the configuration list to determine whether github has been bound before and to which user:
Git config --listCopy the code
2. If not, configure it and check the configuration list again:
git config --global user.name "wwml725"
git config --global user.email "[email protected]"
git config --list
Copy the code
Note:
Git is a distributed management system, so each machine must fill in its own name and email address.
The –golbal parameter of the git config command indicates that all git repositories on the machine will use this configuration, but you can also specify different user names and Email addresses for each repository. How to specify? More on that later)
Git common operation commands
1. Create a directory and access the directory
Mkdir fitst // Create a directorycdFirst Access the directory mkdirtest && cd test
Copy the code
2. Display the location of the current environment
pwdDisplays the location of the current environmentCopy the code
3. Initialization
Git init // Initialize the repository automatically create a master of ls -al to view all files in this folder (including hidden ones) rm -rf. git if the initialization error, delete the.git folderCopy the code
4. Create a file and view the file content
TXT file cat index. TXT // View the contents of the fileCopy the code
5. Run the vi command to insert content into the file
Vi index. TXT I (input content) esc + : + w+ q // Save and push out q! // Forcibly exitCopy the code
6. Write content to the file
echo1 > index. TXT // Write contents in the file to cover all contents insideecho2 >> index.txt // Add the contents to the end of the original fileCopy the code
7. Delete files
TXT // Delete the file rm -rf text // Delete the folderCopy the code
3. Work area – Temporary storage area – History area
This is all based on one branch master or another sub-branch Dev1
1. Check git status
git status
Copy the code
- Red: Work area.
- Green: Temporary storage area.
2. Push to temporary storage area
Git add. // Push all files in your workspaceCopy the code
3. Push it to the historical area
git commit -m"Comment""
Copy the code
4. Joint submission
Copy the code
Note:
Git add. Push files from your workspace to staging.
Git commit -m “comment” to push files from staging to history
3. We can add different files multiple times or add all files at once. Commit can commit multiple added files at once.
4, git commit if -m is not added, then the editing page will pop up, you can enter your comment, then press ESc to exit editing mode, enter :wq! Exit the page.
4. Code comparison and rollback
This is essentially looking at the difference between workspace, staging, and history files.
Git status git rm --cached 1. TXT git reset 1 // Roll back a file from staging to workspace using git statusCopy the code
Git diff --cached git diff --cached git diff --cached git diff master git diff --cached git diff master git diffCopy the code
Delete the file
Git rm --cached 1. TXT // Delete the workspace file but not the staging fileCopy the code
git logGit reflog git reflog git reflogCopy the code
Both git log and Git reflog are used to view versions of branches, the latter being able to view deleted versions.
5. Branch management
Git’s default master branch is: master
1. View branches of the current project
Git branch git branch dev Creates a sub-branch devCopy the code
1, you can create multiple sub-branches, all of which are copied from the master branch and modified from the master branch. Create branch dev1 on dev, same as dev. 3. Commit a file to a branch, and the file belongs to that branch. 4, under normal circumstances, the development project, there will be a main branch, in the main branch to establish a small branch for development, if the error will not affect the main branch. 5. Commit the file to the branch after git add commit.
#####2. Switch branches
Git checkout -b dev //Copy the code
3. Merge branches
Git merge branch nameCopy the code
- 1. By default, master is the trunk, and merge branches are in the trunk
Git merge branch name
. - 2. After the merge, the merged branch needs to be deleted.
- 3. After the merge, upload the merged file to the
github
.
4. Deal with conflict
Case 1: fast-forward
- There are no updates to the trunk
- The branch submits new code
- Delete branches after merge
Case 2: When you merge multiple branches, the merged contents may conflict
- Manually resolve the conflict and submit the latest result. 1. Delete >>>>> == <<<<< and retain the final result
Consider: merge branches branch conflict causes and solutions?
1, master== master==
Git checkout dev1== git checkout dev1== git checkout dev1== git checkout dev1==
Git merge dev1== git merge dev1== git merge dev1== git merge dev1== git merge dev1== git merge dev1== git merge dev1== git merge
4. At this point, the conflict has already occurred and we need to resolve the conflict manually
5. Once you’re done, commit all code on the main branch again.
5. Delete branches
- Subbranches are removed after code merge
git branch -D dev
Copy the code
Commit the trunk branch
By default, our code is stored in the workspace and does not belong to any branch. The file belongs to a particular branch only if it is committed to a branch.
Three, the git – remote
1. Bind the key
Start with a Github account. Since transport between your local Git repository and Github repository is encrypted over SSH, you need to set this up:
Step 1: Create an SSH Key.
Check if there is any in the user’s home directory.
If yes, check whether the id_rsa and id_rsa.pub files are in the directory. If yes, skip to the next step.
If not, open Shell (window open git bash) and create SSH Key:
ssh-keygen -t rsa -C "[email protected]"
Copy the code
You need to change your email address to your own, press Enter, and use the default. Since this Key is not used for military purposes, there is no password required.
Pub and id_rsa.pub are the Key pairs of the SSH Key. Id_rsa is the private Key and cannot be leaked, while id_rsa.pub is the public Key and can be safely shared with anyone.
Step 2: Log in to GitHub, open “Account Settings”, and “SSH Keys” page:
Next, click “Add SSH Key”, fill in any Title and paste the contents of the id_rsa.pub file in the Key text box:
- View the shortcut to the key and type it on the command line
cat ~/.ssh/id_rsa.pub
Copy the code
2. Commit to the remote repository
1. Create a folder
Readme.md: This is a README file that describes what the code does and how to use it
.gitignore: Write the name of the file to be ignored in this file and it will not be submitted to Github
Git cannot commit empty folders
2. Commit to the history section of the branch
git add .
git commit -m"Comment""
Copy the code
3. View the remote repository
git remote -v
Copy the code
4. Associate the remote warehouse
Add Commit and push it to branches first, then to Github
git init
git commit -m "first commit"Git remote add origin making address (for example: https://github.com/wwml725/first.git git push -u origin master)Copy the code
Push an existing repository on a branch from the command line:
git remote add origin https://github.com/wwml725/first.git
git push -u origin master
Copy the code
After the warehouse association, push again
git push origin master -u
Copy the code
Git push== git push== git push==
5. Pull all content from a branch to local
Git pull origin masterCopy the code
3. Create a static site on Github
- 1. Set up a branch of GH-Pages under the current project
- 2. Push the content you need to publish to the GH-Pages branch
- Create a repository on Github
- 4. Push content from branches to github repository
- Github gives us an online address
git checkout -b gh-pages
git add .
git commit -m 'Add static page address'
git push origin gh-pages
Copy the code
- In Settings, you can find the url + the file name (the file will be displayed by default)
4. Change the Github address
Note: After the address is updated, the local repository is reconfigured, but there is always a problem: the following errors often occur:
remote: Permission to wwml725/first.git denied to wangwenghui. fatal: unable to access 'https://github.com/wwml725/first
Copy the code
Solution: Go to the control panel —- account security — credence manager —- at the bottom of the image delete —- from the repository and redo the changes, add, commit, which will again require logging into Git
Fourth, late supplement
- There is no parent-child relationship between branches
A project has a common branch, Develop
- The data on this branch can not be modified by anyone. A new branch should be created based on this branch, and development should be carried out on this branch. After coding is completed, it is determined that there is no problem with the data, and then this branch is merged with the common branch
Create a folder. 1. Gitclone(https://github.com/limin0428/earlyjoy) : 2 write their leader addresses.cdGit checkout -b // create the scaffold and switch to your own branch. Git add. 7. Git commit -m"Comment"Git push origin only needs to repeat steps 6, 7,8Copy the code