Summary of git basic usage

Summary of git basic usage

1. Create a repository

After selecting a target, use git init to turn this directory into a warehouse that git can manage.

2. Add the file to the repository

Use the command git add readme.txt to add to the temporary storage area

3. Add the file to the version library

First: Create a new notepad file-readme.txt in the current directory, with the content "111111".

Step 1: Use the command git add readme.txt to add it to the temporary storage area.

Step 2: Use the command git commit to tell Git to add the file to the temporary repository.

The third part: Use the command git status to check if there are any files that have not been submitted.

Step 4: Modify the readme.txt file, and check the specific modified content through git diff readme.txt.

Step 5: Commit the code through git commit -m "modified content".

4. Version rollback

git log

The git log command displays the display log from the nearest to the farthest.

git reset --hard HEAD^

Go back to the previous version and view the current command through cat readme.txt.

git reset --hard HEAD^^

Roll back the previous two versions.

git reset --hard HEAD~100

Roll back the first 100 versions.

git reflog

Get all the version numbers.

git reset -hard version number

Revert to the current version.

5. Commonly used simple commands
  • git checkout branch name (such as feature/mybranch) (switch to the specified development branch)
  • git pull (pull code)
  • git add (temporarily store files that need to be submitted)
  • git commit -m'remarks' (submit the files that have been staged)
  • git push (synchronized to server)
  • git rebase (merge multiple commits)
6.git stash

When we are developing a project, a change suddenly needs to be modified. In addition to submitting the current project (commit) and then switching (checkout) to other branches, we can also stash the current modification first, and then Then switch (checkout) to other branches without committing (commit), so you can reduce one commit (although you can use git commit --amend to modify the last commit).

There are two situations for temporary storage modification:

1. The file has been tracked by git, but the code has been modified (not the new condition file), we can use git stash or git stash save "comment" to temporarily save the modification.
2. If there are newly added files, then you need to add the -a parameter (for example, git stash -a or git stash save -a "comment"), or first git add. Then use git stash or git stash save "comment" "To temporarily save the changes.

When the changes are completed, you can use git stash apply stash@{id} to open the project when you open the project. The id in stash@{id} starts from 0 by default, and the most recent temporary store is 0. If you open the most recent temporary store, you can use git stash pop or git stash apply stash@{0} to open it. In terms of visual effects, the effects of git stash pop and git stash apply stash@{0} are the same. However, in some respects, the two commands are different. After git stash pop opens a certain modification temporary storage, The last modified temporary record will be deleted in the stash list, but git stash apply stash@{0} will not.

To see how many changes are temporarily stored, you can use git stash list to view the stash list.

We can also delete the modification temporary storage, delete a certain modification temporary storage, git stash drop stash@{id}, if you need to clear all the modification temporary storage, you can use the git stash clear command.

7. Summary

Some basic operations of git are necessary skills for teamwork. Many tutorials on the Internet are more cumbersome to explain, I simplified them based on them, so that novices can get started quickly.