This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

introduce

Git is a version control tool, which is very common in development. There are a lot of commands, but the most common ones are those.

Common commands

The local operating

  • View code changes

Git status command

Ex. :

>git status
On branch dev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   src/main/java/com/wqy/TaskOperation.java
Copy the code

View the status, the current branch and modification content are printed in the result

  • Add changes to the staging area

Git add command.

“.” To place all contents of the current directory in the staging area

>git add .

Copy the code

Branch operation

  • View the current branch

Git branch -v command

Ex. :

>git branch -v
* dev                8b5310f Merge branch 'feature/name' into dev
  feature/wwa b7bee2 feat: <E6><8E><A8><E8><8D><90>-<E5><8F><91><E7><8E><B0><E9><A1><B5> - <E6><A0>
  feature/name 1b9c1a4 feat: <E6><8E><A8><E8><8D><90><E5><AE><A1><E6><A0><B8>
  master             162d523 1.<E8><AE><B2><E8><82><A1><E5><A0><82><E6><95><B0><E6><8D><AE><E5><90>
  pdt                a133379 feat: <E5><A2><9E><E5><8A><A0>pdt<E5><88><86><E6><94><AF>
Copy the code

Viewing branch results prints all branches and records of the last operation

  • Switch branch

Git checkout

Ex. :

>git checkout feature/name
Switched to branch 'feature/name'
D       src/main/java/com/wqy/service/package-info.java
A       src/test/java/com/wqy/mapper/NameMapperTest.java
A       src/test/java/com/wqy/util/RedisListTest.java
Your branch is up to date with 'origin/feature/name'.
Copy the code

Switch the branch, the result will print out the branch changes

  • The new branch

Git branch

Ex. :

>git branch aa

Copy the code

Create a branch. By default, create a branch based on the current branch

Stash operation

  • Save the current code

Command: git stash

Ex. :

>git stash
Saved working directory and index state WIP on dev: 8b533kk Merge branch 'feature/name' into dev
Copy the code
  • Look at the Stash collection

Git stash list

Ex. :

>git stash list
stash@{0}: WIP on dev: 8b3330f Merge branch 'feature/name' into dev
stash@{1}: WIP on name: d161368 feat: <E5><8F><91><E7><8E><B0><E9><A1><B5>-<E6>B4><E6><96><B0><E9><95><BF><E7><9F><A2><E5><BC><95>
stash@{2}: On dev: Uncommitted changes before Update at 2021/11/25 10:19
Copy the code

The list prints out the Stash contents of all branches

  • Restore stash contents

Git stash pop stash@{no.}

Restore the Stash content and delete the stash

Ex. :

>git stash pop stash@{2}
Auto-merging src/main/resources/generator.properties
Removing src/main/java/com/wqy/service/package-info.java
On branch dev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/test/java/com/wqy/mapper/QQMapperTest.java
        new file:   src/test/java/com/wqy/mapper/AMapperTest.java
        new file:   src/test/java/com/wqy/util/RedisListTest.java

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    src/main/java/com/wqy/service/package-info.java

Dropped stash@{2} (30feafb08ef0efaa58995311417ecfee86de05)
Copy the code

The restore operation prints the change in the result

Common combination operations for daily development

Business code is often written on different branches. Before switching branches, the code is not complete and does not want to submit, so the code must be saved in stash. When switching back to the branch again, the code will be restored; otherwise, the code will be brought to the branch after switching.

View branches before switching
>git status
On branch dev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   src/main/java/com/wqy/TaskOperation.java
Put the content into the local staging area
>git add .       
# add to stash
>git stash
Saved working directory and index state WIP on dev: 8b533kk Merge branch 'feature/name' into dev
# Switch branches
>git checkout feature/name
Switched to branch 'feature/name'
D       src/main/java/com/wqy/service/package-info.java
A       src/test/java/com/wqy/mapper/NameMapperTest.java
A       src/test/java/com/wqy/util/RedisListTest.java
Your branch is up to date with 'origin/feature/name'.
Switch back to the previous branch
>git checkout dev
Switched to branch 'dev'
D       src/main/java/com/wqy/service/package-info.java
A       src/test/java/com/wqy/mapper/QQMapperTest.java
A       src/test/java/com/wqy/mapper/AMapperTest.java
A       src/test/java/com/wqy/util/RedisListTest.java
Your branch is up to date with 'origin/dev'.
# Look at the Stash collection
>git stash list
stash@{0}: WIP on dev: 8b3330f Merge branch 'feature/name' into dev
stash@{1}: WIP on name: d161368 feat: <E5><8F><91><E7><8E><B0><E9><A1><B5>-<E6>B4><E6><96><B0><E9><95><BF><E7><9F><A2><E5><BC><95>
stash@{2}: On dev: Uncommitted changes before Update at 2021/11/25 10:19
# Restore stash content
>git stash pop stash@{2}
Copy the code

reference

  • monkeyCan understandGITAn introduction to
  • Git tutorial by Liao Xuefeng