(1) Create a repository
1. The pwd command is used to display the current directory. On my computer. This repository is located at /c/Users/25165/Desktop/learngit.
2. Use the git init command to turn this directory into a warehouse that Git can manage. (Git will build the warehouse in an instant, and tell you that it is an empty warehouse (empty Git repository). Now you can find that there is one more in the current directory. git directory)
Add files to repository
3. Now we write a readme.txt file with the following content (take this as an example):
Git is a version control system.
Git is free software.
(!!! Note: Be sure to put it in the learngit directory (subdirectories are also fine))
4. The git add command adds the file to the warehouse (no display, indicating that the addition is successful)
5. The git commit command submits the file to the warehouse (the input after -m is the description of this submission)
(2) Master the status of the workspace
1. Try to modify the readme.txt file for example:
Git is a distributed version control system.
Git is free software
2. Use the git status command to see the results (the git status command grasps the current status of the warehouse, the above command tells us that readme.txt has been modified, and there is no modification to be submitted yet)
3. git diff command to view (what was modified before)
4. Submit the modification (same as submitting a new file)
(After git add readme.txt, there is also no output. Before executing the git commit command, you can run the git status command to see the status of the current warehouse. After executing git status, it prompts us to submit changes including readme.txt, commit submission After that, the working directory is clean (working directory clean)
(3) Version rollback
1. Try to modify the readme.txt file and submit it, for example:
Git is a distributed version control system.
Git is free software distributed under the GPL.
(The intermediate status step can be omitted)
2. Use the git log command to view how many versions of the readme.txt file have been submitted to the warehouse
3. Command git log --pretty=oneline to view the simplest version information (the number in front is the version number)
4. Use the git reset command to roll back the version, which is the add distributed version (in the command below, HEAD^ is the previous version, and the previous version is HEAD^^) (you can use the cat readme.txt command as shown in the figure below to see You can also use the git log command to check the version number to see if the previous version is currently rolled back)
5. If you roll back the previous version, you can use the command to add the version number
6. The command git reflog is used to record each command (you can also use this command to view the version number)
(4) Work area and temporary storage area
Workspace: The directory that can be seen on the computer, such as the learngit folder is a workspace.
Repository: There is a hidden directory ".git" in the workspace. This is not a workspace, but a Git repository.
Temporary storage area: stage (or index) in the repository.
Branch master: The first branch master created automatically, a pointer to the master is called HEAD.
1. At the beginning, the file is in the work area. After the command git add < file> is executed, the file arrives in the temporary storage area.
2. After the command git commit -m "description" is executed, the file is transferred to the branch
(5) Undo the modification
1. When you execute the add command without committing and want to undo the modification, you can use the git status command to check it and it will tell you that the command git reset HEAD < file> can roll back the modified undo file in the temporary storage area to the work area.
2. Continue to use the git status command to check to know that the git checkout – < file> command can discard the modification of the workspace.
(6) Delete files
1. First use the command rm < file> to delete the file, the workspace and the version library are not consistent, use the git status command to know immediately which files have been deleted.
2. If these files are what you want to delete, use the command git rm <file> and the commit command to submit the deletion.
3. If you feel that you deleted it by mistake because the version library still exists, you can use the command git checkout – <file> to restore it with one click.
(7) Remote warehouse (using github)
1. Register a github account.
2. Check if there is a .ssh directory in the computer user directory, and if so, check if there are two files id_rsa and id_rsa.pub, if there is, go to the third step; if not, open Git Bash and create an SSH Key:
Create command: ssh-keygen -t rsa -C "[email protected]"
After that, keep pressing Enter. If it succeeds, you can find the .ssh directory in the main directory of the computer. There are two files id_rsa and id_rsa.pub in it. These two are the secret key pair of SSH Key. id_rsa is the private key and cannot be leaked out. id_rsa.pub is the public key, which can be told to others.
3. Log in to github, open the "Account settings", "SSH Keys" page, click "Add SSH Key", fill in any Title, paste the content of the id_rsa.pub file in the Key text box, and click "Add Key", you can See the Key that has been added.
(8) Add remote library
1. There is already a git warehouse locally, and you want to create a git warehouse on github, and remotely synchronize the two warehouses, first log in to github to create a new warehouse.
2. Command: git remote add origin [email protected]:xiaoyingzi/learngit.git associate local library
Among them, origin is the name of the remote library, which can be chosen arbitrarily, and then replace xiaoyingzi with your own github account name
3. The command git push -u origin master pushes all the contents of the local library to the remote library, which actually pushes the current branch master to the remote.
4. Command git push origin master to push the latest modification.
(9) Clone from remote repository
1. Create a new warehouse, check Initialize this repository with a README, and there will be a README.md file.
2. Command git clone [email protected]:xiaoyingzi/gitskills.git to clone the new warehouse to the local library, where [email protected]:xiaoyingzi/gitskills.git is replaced with your own warehouse link.
(9) Branch management
1. When there is only one branch master,
The master branch is a line. Git uses master to point to the latest submission, and then uses HEAD to point to the master to determine the current branch and the submission point of the current branch. Every time you commit, the master branch will move forward one step, so that as you continue to commit, the line of the master branch will become longer and longer.
2. When creating a new branch, such as dev, Git creates a new pointer called dev, which points to the same submission as the master, and then points HEAD to dev, which means that the current branch is on dev.
3. From now on, the modification and submission of the workspace is aimed at the dev branch. For example, after a new submission, the dev pointer moves forward one step, while the master pointer remains unchanged.
4. If our work on dev is completed, we can merge dev into master, that is, directly point master to the current commit of dev, and the merge is completed.
5. After merging the branches, you can delete the dev branch, which is to delete the dev pointer.
6. Command: git checkout -b dev to create a dev branch, and then switch to the dev branch. (commands equivalent to $ git branch dev and $ git checkout dev)
7. Command: git branch to view the current branch (the git branch command will list all branches, and the current branch will be marked with a *)
8. Command: git checkout master switch master branch.
9. Command: git merge dev merge branch. (The git merge command is used to merge the specified branch into the current branch)
10. After the merge is completed, you can safely delete the branch. Command: git branch -d dev to delete the dev branch.
11. The git log --graph command can see the branch merge graph. (git log --graph --pretty=oneline --abbrev-commit)
12. Command: git merge --no-ff -m "merge with no-ff" dev means that the dev branch is ready to be merged, pay attention to the --no-ff parameter, which means that "Fast forward" is disabled.
13. Command: git stash can "store" the current work site, and continue working after restoring the site later.
14. Command: git stash list to view the work site.
15. Command: want to resume work after using (command 13):
- Use git stash apply to restore, but after restoration, the stash content is not deleted, you need to use git stash drop to delete.
- Use git stash pop to delete the stash content while restoring.