preface

Recently, my supervisor asked me to do a group share on how I use Git in my daily work. Now I sort into an article, share, hope to help you.

Basic concept

Git has three partitions:

  • Working Directory: a place directly edited by the developer, where changes to files are displayed, including tracked and untracked files. throughgit addAdds the workspace file to the staging area.
  • Staging area (Stage | Index) : the data of the temporary storage area, throughgit commitAdd the staging file to the local version library.
  • Local Commit History: stores all committed datagit pushPush to remote repository.

Basic commands

git status

This command is used to check the status of the workspace. If the traced files have been modified, you can use this command to find out. Git status shows that the readme. md file has changed on the Develop branch.

jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code

git diff

Git diff

: git diff

jere@JereMBP GitTest (develop) $ git diff README.md diff --git a/README.md b/README.md index 7eb4917.. 3d6d2a4 100644 -- a/ readme.md +++ b/ readme.md @@ -1,2 +1,5 @@ # GitTest
 For git command practice
+
+do something on develop branch
+
jere@JereMBP GitTest (develop) $ 
Copy the code

Alternatively, you can use Git diff directly, which shows all changes to all files.

git checkout

Switch branches, for example if I switch from feature-1 to Develop:

jere@JereMBP GitTest (feature-1) $ git checkout develop 
Switched to branch 'develop'
jere@JereMBP GitTest (develop) $ 
Copy the code

Create a new branch on the current branch node and switch over, for example if I create a new develop branch on the main branch and switch over:

jere@JereMBP GitTest (main) $ git checkout -b develop
Switched to a new branch 'develop'
jere@JereMBP GitTest (develop) $ git branch
* develop
  main
Copy the code

Git restore

… For example, I now want to discard the changes in readme.md

jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md
	modified:   dev-file.txt

no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ git checkout README.md
Updated 1 path from the index
jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   dev-file.txt

no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ 
Copy the code

Alternatively, if you want to discard all changes in your workspace, you can use Git Checkout. To discard all modifications.

git branch

View local branches:

jere@JereMBP GitTest (main) $ git branch
* main
Copy the code

View remote warehouse branches:

jere@JereMBP GitTest (develop) $ git branch --remote
  origin/HEAD -> origin/main
  origin/main
Copy the code

Create a new branch on the current node, for example if I create a new branch on Develop with feature-1:

jere@JereMBP GitTest (develop) $ git branch feature-1
jere@JereMBP GitTest (develop) $ git branch
* develop
  feature-1
  main
Copy the code

In addition, there are some common operations to create branches, such as:

#Make a new branch based on a branch
$ git branch <new-branch> <base-branch>

#Open a new branch based on a commit
$ git branch <new-branch> commit_hash

#A new branch is opened based on a tag
$Git branch < new - branch > v1.1
Copy the code

Delete the branch

  • Delete a local branch:git branch -d <branch-name>.
  • Delete remote branches:git push -d origin <branch-name>.

git add

Add workspace changes to the staging area, such as the readme.md file to the staging area:

jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ git add README.md
jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md

jere@JereMBP GitTest (develop) $ 
Copy the code

Alternatively, you can use the git add. command, which adds all your workspace changes to the staging area.

git commit

Save the contents of the staging area to the local workspace. We added the readme.md file to the staging area in the previous step, so save it to the local workspace:

jere@JereMBP GitTest (develop) $git commit -m "README file changed" [develop b18e4f1] 3 insertions(+) jere@JereMBP GitTest (develop) $ git status On branch develop nothing to commit, working tree clean jere@JereMBP GitTest (develop) $Copy the code

You can also use git commit -am, which is a combination of git add. & Git commit -m.

git push

To push the local file to the remote repository, for example, push the readme. md file saved locally in the previous step to the remote repository:

jere@JereMBP GitTest (develop) $ git push origin develop Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (3/3), done. Writing objects: 371.00 100% (3/3), 371 bytes | KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/JereChen11/GitTest.git 1f7fb80.. b18e4f1 develop -> develop jere@JereMBP GitTest (develop) $Copy the code

git fetch

Git fetch: the main branch of the remote repository has a new commit, so it is downloaded.

jere@JereMBP GitTest (develop) $ git fetch remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/JereChen11/GitTest 30f049e.. d6ff31d main -> origin/main jere@JereMBP GitTest (develop) $ git fetch jere@JereMBP GitTest (develop) $Copy the code

git merge

Merge branches. For example, in the previous step, we found that the remote mian branch had a new commit through git fetch, so the local main branch is currently behind, so we should merge the remote mian branch into our local main branch to achieve synchronization.

jere@JereMBP GitTest (develop) $ git checkout main Switched to branch 'main' Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) jere@JereMBP GitTest (main) $ git merge origin/main Updating 30f049e.. d6ff31d Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+) jere@JereMBP GitTest (main) $Copy the code

git pull

If the remote repository has an update, the update will be downloaded and merged into the current branch. This is equivalent to a combination of Git fetch and Git merge. Again, this time we use Git pull to pull new commits on the remote mian branch.

jere@JereMBP GitTest (main) $ git pull remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/JereChen11/GitTest 55e808c.. 4a1a531 main -> origin/main Updating 55e808c.. 4a1a531 Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+)Copy the code

The business scenario

In addition to the above basic commands, NOW I will introduce some more commands according to our usual business scenarios and cases.

Resolve the conflict

In terms of conflicts, conflicts occur when branch merges are involved and both branches make changes to the same place.

Such as: In the previous step, we pulled the remote mian branch down and found that it had made changes to the readme. md file in the same location as the develop file that we had originally made. If we tried to merge the Develop branch into the main branch, There will be conflict.

jere@JereMBP GitTest (main) $ git merge develop Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result. jere@JereMBP GitTest (main) $ git status On branch main Your branch is up to date with 'origin/main'. You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort  the merge) Changes to be committed: new file: dev-file.txt Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md jere@JereMBP GitTest (main) $Copy the code

It is the readme.md file that is in conflict, and we need to open it to see what the conflict is.

# GitTestFor Git command practice <<<<<<< HEAD chagne README file on the main branch ======= do something on develop branch 2 / / content
>>>>>>> develop
Copy the code

The content of the conflict will be < < < < < < < and > > > > > > > surrounded by using = = = = = = = separated, also as my comments 1 and 2 two pieces of content into content.

  • The content of 1: is the content of the current branch node, i.emian.
  • Content of the 2: The content of the branch to merge, i.edevelop.

Select the content you want, delete the <<<<<<<, >>>>>>>, and ======= isolators and save them. Then run git add and git commit to merge.

jere@JereMBP GitTest (main) $ vim README.md jere@JereMBP GitTest (main) $ git status On branch main Your branch is up to  date with 'origin/main'. You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Changes to be committed: new file: dev-file.txt Unmerged paths: (use "git add <file>..." to mark resolution) both modified: Readme. md jere@JereMBP GitTest (main) $git commit -am "Resolve README conflict" [main 8e08c23] jere@JereMBP GitTest (main) $Copy the code

Synchronizing development progress between team members

In real projects, we work together as a team, splitting up requirements, and then merging them into the Develop branch. What if you want to synchronize a team member’s code that has already been developed and merged into the Develop branch, but you haven’t?

Git rebase can be used to do this.

Git rebase moves the root node of your branch.

Here’s an example:

  1. feature-1feature-2Are based ondevelopA branch from the same node.
  2. As the development progressed,feature-1Develop first, and merge intodevelopBranch.
  3. At this timefeature-2usinggit rebase origin developTo synchronizefeature-1The code.
jere@JereMBP GitTest (feature-2) $ git rebase origin/develop First, rewinding head to replay your work on top of it... TXT jere@JereMBP GitTest (feature-2) $Copy the code

The node branches of these three states are shown in the figure below:

The initial state Feature-1 is merged into Develop Feature-2 synchronizes the Develop code

Collation merge commit

Sometimes we want to merge our commit. (Want a background? Git notes: Merge two commits into one

Git rebase -i HEAD~x git rebase -i HEAD~x git rebase -i HEAD~x git rebase -i HEAD~x git rebase -i HEAD~x

For example, we made two commits to feature-3. TXT on the feature-3 branch, respectively 598CC68 modified feature-3. TXT and 8561ef3 modified feature-3. TXT again, now we need to merge the two commits.

  1. performgit rebase -i HEAD~2.
  2. This is automatically enteredvimThe following information is displayed:
Pick 598CC68 Modify feature-3.TXT pick 8561ef33.txt ... Omit...Copy the code

Here we want to merge the two commits, so change the second pick to s as follows:

Pick 598CC68 Modify feature-3.TXT s 8561ef33.txt ... Omit...Copy the code

Save and exit, another vim file will be automatically opened to modify the submission text information, as follows:

# This is a combination of 2 commits.
# This is the 1st commit message:Modify the feature - 3. TXT
# This is the commit message # 2:Modify feature-3.txt again
# Please enter the commit message for your changes. Lines starting
# with The '#' will be ignored, and an empty message aborts the commit.
#

Copy the code

Here we change it to the final version, modify feature-3. TXT, save and exit.

jere@JereMBP GitTest (feature-3) $git rebase -I HEAD~2 [detached HEAD 18296c5] Final version, modify feature-3.txt Date: Wed Dec 1 10:21:05 2021 +0800 1 file changed, 2 insertions(+) Successfully rebased and updated refs/heads/feature-3. jere@JereMBP GitTest (feature-3) $Copy the code
  1. At this point, your local branch has finished merging, and when you try to push it to the repository, it will be rejected. A. refused B. refused C. refused D. refusedYour current branch is behind the remote branchAt this point, we need togit push origin feature-3 -f.Forced to pushTo complete the merge.
jere@JereMBP GitTest (feature-3) $ git push origin feature-3 To https://github.com/JereChen11/GitTest.git ! [rejected] feature-3 -> feature-3 (non-fast-forward) error: failed to push some refs to 'https://github.com/JereChen11/GitTest.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ... ') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. jere@JereMBP GitTest (feature-3) $ git push origin feature-3 -f Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 356.00 100% (3/3), 356 bytes | KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/JereChen11/GitTest.git + 8561ef3... 18296c5 feature-3 -> feature-3 (forced update)Copy the code

The node branches of these three states are shown in the figure below:

performgit rebase -i HEAD~2 usingsquashMerge submissions and modify the text information Force push to warehouse

Remember when we changed pick to s? The S here refers to a squash, which means that the commit is squashed and merged into the previous commit. He has a few other options to check out:

P, pick <commit> = commit with r, reword <commit> = commit with r, edit <commit> = commit with e, edit <commit> = Commit with You can modify it again by git commit --amend; Git rebase --continue s, squash <commit> = Commit f, fixup <commit> = Similar to "squash" The step of modifying the submitted text information is ignored.Copy the code

Commit 1: Finish login feature, followed by commit 2: Code review for login feature. In fact, such operation is quite normal and reasonable. You did a code review after completing the function, but in fact, we can merge these two submissions into one submission, so that we and colleagues can check your code easily.

But note: you need to operate in your own branch.

Cancel the submission

The undo commit can be divided into local revoke commit and remote revoke commit.

Revoke local submission

When you commit code locally, but at this point, you want to withdraw the commit and edit it again. You can do this with git reset.

This undo is divided into whether to keep the modification.

  • Reserved modification:git reset --softThis is also known as the safest undo.
  • Discard modification:git reset --hardAny changes you made before will disappear, so use them with care.

For example: you are currently on the feature-4 branch, and you have made a local commit for a second change, and you want to undo it and go back.

jere@JereMBP GitTest (feature-4) $ git reset --soft HEAD~
Copy the code

If, I simply do not want this second change local commit, then:

jere@JereMBP GitTest (feature-4) $git reset --hard HEAD~ HEAD is now at 16960c7Copy the code

The node branches of these three states are shown in the figure below:

The initial state git revert --soft git revert --hard

Undo remote Commit (rollback operation)

When you push your local commit to the remote repository, you realize that you did it all wrong and you want to roll back.

This is where you need to use the Git Revert operation.

For example: I have lifted two commits on the feature-4 branch and pushed them to the repository, the first and the second modification respectively. Now I want to roll back and undo the second modification of the commit.

jere@JereMBP GitTest (feature-4) $git revert HEAD [feature-4 C73e361] delete "delete"Copy the code

When you run this command, a vim file automatically opens that allows you to modify the submitted text. By default, add the revert comment at the beginning, save and exit, and you’re done.

The node branches of the two states before and after revocation are shown in the figure below:

Cancel the former After the cancellation

You can use git rebase -I HEAD~x to reorganize and merge the commit if you don’t want it to be known that you rolled back.

Alternatively, you can use git REVERT

to rollback a given commit, resolve any conflicts, and continue with git REVERT –continue.

Note: Rollback needs to be context-sensitive, and if someone pulls your code, it’s not good to roll back.

There is a Bug on the line, urgent fix

Tell me a story.

After some time of development, your product was finally released as V1.0, and you immediately worked on requirements for V1.1. One day, the operation and product come to you at the same time and say that they have just found a serious bug on the line and need to fix it urgently. You immediately put into the repair work, after intensive screening and testing, finally you fix the problem, ready to release. At this point, you notice that you are working on the Develop branch and that the develope branch already contains some of the features of V1.1. How do you commit this emergency fix to V1.0?

You can do this with git cherry-pick to select a commit.

For example, we are now going to merge the d818F10 emergency fix line bug commit on the Develop branch into our main V1.0. Switch to the main branch first, and then select the d818F10 commit to merge.

jere@JereMBP GitTest (develop) $ git co main Switched to branch 'main' Your branch is up to date with 'origin/main'. jere@JereMBP GitTest (main) $git cherry-pick d818f10 [main 55e808c] Wed Dec 1 15:49:48 2021 +0800 1 file changed, 2 insertions(+)Copy the code

The node branches of these three states are shown in the figure below:

The initial state indevelopThe repair Select this fix commit tomain

Save your work for the time being

One day when you are seriously developing requirements, the test comes to you and asks you to help look at an online problem. At this point, you’ve just written half of your code and don’t want to submit it. What do you do?

At this point, we can temporarily store our code through git Stash, and then switch to the online environment branch to troubleshoot the problem. After solving the problem, we can switch back to the previous branch and execute git Stash pop to continue development.

For example, I am currently developing new requirements on feature-5 branch. At this point, I need to temporarily save all changes and switch to main branch to troubleshoot problems. After the troubleshooting, return to feature-5, temporarily access and continue development.

jere@JereMBP GitTest (feature-5) $ git status On branch feature-5 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: feature-5.txt no changes added to commit (use "git add" and/or "git commit -a") jere@JereMBP GitTest (feature-5) $ git stash Saved working directory and index state WIP on feature-5: jere@JereMBP GitTest (feature-5) $git status On branch feature-5 nothing to commit, working tree clean jere@JereMBP GitTest (feature-5) $ git stash pop On branch feature-5 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: feature-5.txt no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (1ec48d00e8d1bd5c0042d88d1209dbb9051815d4) jere@JereMBP GitTest (feature-5) $Copy the code

In addition, there are some common operations with Git Stash.

#Save to the Stash stack and add custom message decorations
$ git stash save "message"

#List all the elements in the Stash stack
$ git stash list

#Stash applies the x element of the stack, pop applies simultaneously and is removed from the stack, and apply applies only
$ git stash apply stash@{x}

#Delete the x-th element in the Stash stack
$ git stash drop stash@{x}
Copy the code

The line sends version

When our product development is completed and we find it online, we need to label it so that we can find it better next time.

Git tag: git tag: git tag: git tag: git tag: git tag: git tag: git tag: git tag

For example: our version 1.0 is coming online, so call v1.0.

jere@JereMBP GitTest (feature-5) $git tag v1.0 jere@JereMBP GitTest (feature-5) $git tag v1.0 v1.0.1Copy the code

In addition, some other operations on tag are:

#View all labels
$ git tag

#Delete the specified tag.
$Git tag -d Specifies the tag name
Copy the code

Git config some configuration

Git config –list to check your Git configuration information.

Configure an alias

For each entry, we need to complete checkout and commit, which is cumbersome. We can set the alias to do this.

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
Copy the code

Git config –list git config –list

alias.co=checkout
alias.br=branch
alias.ci=commit
alias.st=status
Copy the code

Git co

git st

Configure the agent

Due to some special network reasons, github is very unstable at times, sometimes we push some code and 403 fails. This can be resolved by setting up a proxy.

For example, we set up a local proxy.

Git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0.0.1:1080 git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global http.proxy 'socks5://127.0.0.1:1080'Copy the code

Git config –list git config –list

http.proxy=http://127.0.0.1:1080
https.proxy=https://127.0.0.1:1080
Copy the code

The end:

OK, that’s the end of the article.

It is recommended to try, do not panic when encountering problems, pay attention to the hints it gives, I believe that you will soon be able to master.

In fact, the biggest purpose of sharing articles is to wait for someone to point out my mistakes. If you find any mistakes, please point them out without reservation and consult with an open mind. In addition, if you think this article is good and helpful, please give me a like as encouragement, thank you ~ Peace~!