Git Usage Details
Scenario 1: how to create a local branch for multi-person collaboration?
If you have cloned others' repositories and need to modify them, the best way is to create your own branches and merge them. The specific steps are as follows:
1. Create a branch
Git branch mybranch
In this case, you can use git branch to view the current branch status. In the case of no exceptions, a master mybranchi branch is displayed, and you are currently on the master branch.
2. Switch to the new branch
Git checkout mybranch
In this case, we can modify it at will in myabranch, which does not affect the master of the master branch. Note: All of the above operations do not affect the remote warehouse and are local backups.
3. If we have made many changes on mybranch. Then we can perform commit under mybranch.
Git commit-m "I had do some thing in mybranch"
4. Now we return to the original master Branch
Git checkout master
5. Merge the previous modifications on branch to the master
Git merge mybranch
6. Now the mybranch branch is useless for us. Delete it.
Git branch-d mybranch
7. Now we can push the modification of the local master (after merge) to the remote
Git push
Note: If you want to create and switch branch once, you can
Git checkout-B branchname
Scenario 2: How to establish remote branches for multi-person collaboration?
1. if I operate a rep (https://github.com/bkjia/trygit.git) with another colleague, scenario 1 is safe for me locally, because the branch I created locally has been modified to the local master of merge and then pushed to the remote, but the problem is that if there are still many problems with the remote push, you have to modify it on the remote master. In fact, you can also create a branch remotely, push all to this new branch.
Git checkout-B shopping_cart
In this case, create a local host and switch to the shopping_cart branch.
2. link the local branch to the remote Branch
Git push origin: shopping_cart
Before performing this operation, the shopping_cart branch is not available remotely.
3. perform operations on the current branch and then push
Git add 2.html
Git commit-m "add 2.html in shopping_cart branch"
Git push
Hosts file.
4. I think my code should be fine, so I need to merge it into the main branch.
Git checkout master
Git merge shopping_cart
Git push
5. Now it's my turn to operate another one at the same time. He first clones the rep and then checks the branch
Git clone https://github.com/bkjia/trygit.git
Git branch
At this time, only * master is displayed. Why? For this colleague, his local master branch is indeed the only one.
6. view remote branches
Git branch-r
Display:
Origin/HEAD-> origin/master
Origin/master
Origin/shopping_cart
This time he saw the shopping_cart branch I created remotely.
7. He wants to operate the shopping_cart Branch
Git checkout shopping_cart
In this case, checkout places the remote shopping_cart locally and switches to this branch.
Check the local branch status to find that the local branch does exist:
Master
* Shopping_cart
In this case, if git remote show origin is synchronized remotely and locally (both have two branches ).
8. At this time, the colleague checked the code pushed on the master and thought it was okay, so the remote temporary branch shopping_cart would be useless.
Git push origin: shopping_cart
Display:
To https://github.com/bkjia/trygit.git
-[Deleted] shopping_cart
You must be familiar with this command. Execute the command and delete it if it is not created sometimes.
9. The remote Branch has been deleted. It is useless to delete the local shopping_cart.
Git branch-d shopping_cart
If an error is reported, try git branch-D shopping_cart.
Of course, if he thinks there is a problem with my code, he can modify his shopping_cart at 8. Then he can push it to the remote shopping_cart.
Scenario 3: undo the file added by git?
1. Create a new rep to test this example and clone it to the local device.
Git clone https://github.com/bkjia/trygit.git
2. Go to the trygit folder. There is only one README. md file in the folder. manually modify the file content and view diff.
Git diff
As I modified the file, git displays the following differences:
Diff -- git a/README. md B/README. md
Index 8686c6e .. 62a73fe 100644
--- A/README. md
++ B/README. md
@-1, 2 + 1, 2 @@
-Trygit
+ Trygit by zxf
======
Apparently, I changed trygit to trygit by zxf.
3. The status of the README. md file must be unstaged. You can view
Git status
4. Now let's stage the file.
Git add README. md
5. view the differences
Git diff
I found no content. Why? Because git diff is used to detect the differences between unstaged files.
6. Check the difference of staged.
Git diff -- staged
The result is the same as 2. The file status is changes to be committed.
7. Oh, bad. I think the file added by git is not README. md. Is it swollen?
Git reset head readme. md
The following prompt is displayed:
Unstaged changes after reset:
M readme. md
After the HEAD pointer is reset, README. md is changed to unstaged. Of course, you can use git diff again.
8. Since I have added the wrong file to git, should I undo the previous modification?
Git checkout -- READM. md
This occurs:
On branch master
Your branch is up-to-date with 'origin/Master '.
Nothing to commit, working directory clean
Obviously, the file is back to the original state (the content is trygit), and a file that has been tracked can be changed to untrack by using checkout.
Ps: What is track? As long as the file is untrack in the current git working directory, you can use git add to change the file to staged. After the final modification of the file is pushed, it will return to the original state. This Original State does not mean that it was untrack at that time when it was just created, but now it is changed to the track state after being pushed. For example, no matter how you modify the file you created, you cannot see git diff. But after you add git, git can track the file.
Scenario 4: undo a commit file?
1.modify 1.html and submit the file (note that the file is already in track state by default)
Git commit-a-m "add 1.html file"
-A can capture the changes of all tack files, saving the hassle of git add xxx1 xxx2 xxx3.
2. Oh, bad. I didn't write comments on the content I modified in the submission. I want to add it. Is it swollen?
Git reset -- soft HEAD ^
This command changes the commit status to the staging status. git status:
Changes to be committed:
(Use "git reset HEAD <file>..." to unstage)
Modified: 1.html
The prompt is that you have modified it. You can commit it. That's too obvious. This is the staging status.
3. Modify commit again
Git commit -- amend-m "add 1.html file and modify its content"
At this time, git log shows that only the following commit record is missing.
Note: In this scenario, if the comments of commit are written incorrectly, use 3 directly. if you want to add other files in git as the commit object instead of modifying the comments, you need 2.
Other related commands:
Undo the previous commit and all changes
Git reset -- hard HEAD ^
Undo the previous secondary commit and all changes
Git reset -- hard HEAD ^
Scenario 4: rebase
Git checkout mybranch
Git rebase master
Git checkout master
Git merge mybranch
Git fetch
GitHub Tutorials:
GitHub tutorials
Git tag management details
Git branch management
Git remote repository details
Git local Repository (Repository) Details
Git server setup and Client installation
Git Overview
Share practical GitHub tutorials
Git details: click here
Git: click here
This article permanently updates the link address: