Git command tutorial (knowledge organized by myself)

(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.

insert image description here

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)

insert image description here

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)

insert image description here

5. The git commit command submits the file to the warehouse (the input after -m is the description of this submission)

insert image description here

(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)

insert image description here

3. git diff command to view (what was modified before)

insert image description here

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)

insert image description here

(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)
insert image description here

2. Use the git log command to view how many versions of the readme.txt file have been submitted to the warehouse

insert image description here

3. Command git log --pretty=oneline to view the simplest version information (the number in front is the version number)

insert image description here

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)

insert image description here

5. If you roll back the previous version, you can use the command to add the version number

insert image description here

6. The command git reflog is used to record each command (you can also use this command to view the version number)

insert image description here

(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.

insert image description here

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,

insert image description here

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.insert image description here
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.insert image description here
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.

insert image description here

5. After merging the branches, you can delete the dev branch, which is to delete the dev pointer.insert image description here
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.
16. Command: git stash apply stash@{0} to restore the specified stash.
17. Command: git pull grabs the latest commit from origin/dev.
18. Command: git push origin branch-name Push the branch from the local, if the push fails, first use git pull to grab the remote new submission.
19. Command: git remote -v to view remote library information.
20. Command: use git branch --set-upstream branch-name origin/branch-name to establish the association between local branch and remote branch.

(10) label

1. (First, switch to the branch that needs to be tagged) Command: git tag v1.0 to make a new tag.
2. Command git tag to view all tags.
3. If you forget to tag, you can use the command git log --pretty=oneline --abbrev-commit to find the commit id of the historical submission, and then use the command git tag v0.9 6224937.
4. Use git show tagname to view tag information.
5. git tag -a v0.1 -m "version 0.1 released" 3628164 Create a tag with description, use -a to specify the tag name, and -m to specify the description text.
6.git tag -s v0.2 -m "signed version 0.2 released" fec145a signs a tag with a private key through -s.
7. Command: git push origin tagname can push a local tag
8. Command: git push origin --tags can push all local tags that have not been pushed
9. Command: git tag -d tagname can delete a local tag
10. Command: git push origin :refs/tags/tagname can delete a remote tag.

Replenish

1. Command: git pull --rebase origin master Merge the current content submitted to the local warehouse with the remote warehouse

This blog post is only used to record the blogger's learning process (comment if you have any questions)

Supongo que te gusta

Origin blog.csdn.net/qq_46043634/article/details/104217031
Recomendado
Clasificación