Liao Xuefeng git beginner

1. Basic instructions
1.1: Create an empty file directory (all files in this directory can be managed by Git, and Git can track the modification and deletion of each file, so that history can be tracked at any time, or in the future It can be "restored" at a certain moment.)
1.2: Use git init to turn this directory into a warehouse that Git can manage
1.3: git add …Use the command git add to tell Git to add the file to the warehouse (work area -> temporary storage area) )
1.4: git commit tells Git to submit the file to the warehouse (temporary area -> master branch)
1.5 git status command allows us to keep track of the current status of the warehouse at all times
1.6 git diff: can see what has been modified
1.7 git log You can check our history, from the most recent to the farthest commit log
1.8 git reset will fall back to the previous version $ git reset --hard HEAD^: fall back to the previous version.
HEAD: Current version
HEAD^: Last version
HEAD^^: Last version
HEAD^^^: Last version
If there are many rollbacks, for example, the last 100 versions can be used: HEAD~100

1.9 git reflog: record every command
2.0 git checkout: command git checkout-readme.txt means that all modifications of the readme.txt file in the workspace are undone. There are two situations:
one is readme.txt after modification It has not been placed in the temporary storage area. Now, undoing the modification will return to the same state as the version library;
one is that the readme.txt has been added to the temporary storage area and then modified, and now, undoing the modification will return to adding The state after reaching the temporary storage area.
In short, it is to make this file back to the state of the last git commit or git add.

2.1 git reset HEAD readme.txt: undo the file submitted to the temporary storage area and return to the work area
2.2 git rm readme.txt: delete the readme.txt file, but there is still the file in the repository and need to be git commit -m again "Submit to delete the file in the repository
2.3 git checkout --readme.txt: restore the deleted file to the latest version (a file that has never been deleted before being added to the repository cannot be restored!)

2. Work area and temporary storage area
Insert picture description here

3. Remote warehouse
3.1 ssh-keygen -t rsa -C "[email protected]": In the user's home directory, see if there is a .ssh directory, if so, then see if there are id_rsa and id_rsa in this directory. If you already have these two files, you can skip to the next step. If not, open Shell (open Git Bash under Windows) and create SSH Key:

3.2 Set ssh-key: Open the "Account settings", "SSH Keys" page: Then, click "Add SSH Key", fill in any Title, paste the content of the id_rsa.pub file in the Key text box, so that your own The computer is linked to github

3.3 Create a warehouse on github

3.4 git remote add origin [email protected]:michaelliao/learngit.git: synchronize the local warehouse with the github warehouse

3.5 $ git push -u origin master: the first submission needs to add -u to submit the local data to the remote warehouse

3.6 $ git push origin master: After submitting locally in the future, directly use this command to upload to the remote warehouse

4. Branch operation
4.1 git checkout -b dev: Create a dev branch and switch to the dev branch, which is equivalent to git branch dev (create a dev branch) and git checkout dev (switch to dev branch) two instructions

4.2 git branch: View the current branch, the command will list all branches, and the current branch will be marked with an *.

4.3 git merge dev: merge the current branch with the dev branch

4.4 git branch -d dev: delete the dev branch

4.5: It is recommended to use switch to create, switch, and delete branches git switch -c dev (create a dev branch) git switch dev (switch to a dev branch)

4.6: git merge --no-ff -m "merged bug fix 101" issue-101: Use -no-ff to merge branches in normal mode. After merging, you can view the merge record through git log

5. Multi-person collaborative development
The working mode of multi-person collaboration is usually like this:

First, you can try to push your own changes with git push origin;

If the push fails, because the remote branch is newer than your local, you need to use git pull to try to merge;

If there is a conflict in the merge, resolve the conflict and submit it locally;

After there is no conflict or the conflict is resolved, use git push origin to push again to succeed!

If git pull prompts no tracking information, it means that the link relationship between the local branch and the remote branch has not been created. Use the command git branch --set-upstream-to origin/.

Six: Problems encountered
1.: Failed to push some refs to'[email protected]:qweqqr/kings.git'
Reason: My problem is that the readme.md file is missing, just add one again, local Create a readme file in the warehouse, just use git add readme and git commit -m "submit the readme file"

2.fatal: remote origin already exists.
This is because the local warehouse is already associated with a remote warehouse. You can delete this association through git remote rm origin, and then git remote add origin [email protected]:FBing/java-code- generator re-associates with remote warehouse

7. Summary points:
4.1 git add and git commit -m ""
Use the command git add. Note that you can use it repeatedly to add multiple files;
use the command git commit -m to complete.

4.2 git status and git diff
To keep track of the status of the workspace at any time, use the git status command.
If git status tells you that some files have been modified, use git diff to view the modified content.

2.3 git reset rollback version

After rolling back the latest version, you can no longer see it! It's like you took the time shuttle from the 21st century to the 19th century. If you want to go back, you can't go back. What should you do?
In fact, there are ways. As long as the above command line window has not been closed, you can look up and search, and find that the commit id of that submission is 1094adb..., so you can specify back to the future A certain version: But this method can only be used when the commit id is known, and if you don’t know it, use git reflog to view each command

The version pointed to by HEAD is the current version, so Git allows us to shuttle between the version history, using the command git reset --hard commit_id.
Before shuttle, use git log to view the commit history to determine which version to roll back to.
To return to the future, use git reflog to view the command history to determine which version to return to in the future

2.4 Why is Git better designed than other version control systems?
Because Git tracks and manages changes, not files. Every time you modify, if you don't need git add to the temporary storage area, it will not be added to the commit.

2.5 git checkout-in the file command-is very important. Without -, it becomes a "switch to another branch" command. We will encounter the git checkout command again in the branch management later.

2.6 git checkout --readme.txt and git reset HEAD readme.txt
Scenario 1: When you change the content of a file in the workspace and want to discard the modification of the workspace directly, use the command git checkout – file.

Scenario 2: When you not only change the content of a file in the workspace, but also add it to the temporary storage area, you want to discard the modification. There are two steps. The first step is to use the command git reset HEAD to return to scenario 1. The second Step by step to operate according to scene 1.

Scenario 3: When an inappropriate modification has been submitted to the repository, and if you want to cancel this submission, refer to the version rollback section, but the premise is that it has not been pushed to the remote repository.

2.7 git rm readme.txt
command git rm is used to delete a file. If a file has been submitted to the repository, then you never have to worry about deleting it by mistake, but be careful, you can only restore the file to the latest version, and you will lose the content you modified after the last submission.

2.8 Remote warehouse operations
To associate a remote repository, use the command git remote add origin git@server-name:path/repo-name.git;
after the association, use the command git push -u origin master to push all the contents of the master branch for the first time ;
After that, after each local submission, as long as necessary, you can use the command git push origin master to push the latest changes;

2.9 Git encourages a large number of branches: because the branch is just to create a new pointer and modify the pointer to point to, it is fast

View branch: git branch

Create a branch: git branch

Switch branches: git checkout or git switch

Create + switch branches: git checkout -b or git switch -c

Merge a branch to the current branch: git merge

Delete branch: git branch -d

3.0 Merge conflict resolution method: When Git cannot automatically merge branches, the conflict must be resolved first. After resolving the conflict, submit it again and the merge is complete. Resolving the conflict is to manually edit the file that failed to merge in Git to the content we want, and then submit it. Use the git log --graph command to see the branch merge graph.

3.1 Branches for bugs: When fixing bugs, we will fix them by creating a new bug branch, then merge, and finally delete;

When the work at hand is not completed, first git stash the work site, then fix the bug, after the repair, then git stash pop and return to the work site;

If you want to merge the bugs fixed on the master branch to the current dev branch, you can use the git cherry-pick command to "copy" the changes submitted by the bug to the current branch to avoid duplication of work

3.2: To 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 delete it forcibly through git branch -D.

Guess you like

Origin blog.csdn.net/weixin_44273311/article/details/107210138