git quick start and common usage

As a technician, you know what Git does. From the perspective of the server, it is a code warehouse, which can be used for multi-person collaboration, version control, and efficient processing of all the contents of large or small projects; from the client side, it can easily manage local branches and synchronize with the server code, from pulling, Merge, commit, etc. management branches rely on it!

Git is lightweight and easy to learn. If you don't need to build and maintain a code repository (operation and maintenance responsibilities), you can easily deal with it at work as long as you master a few common git commands.

The following briefly introduces a few concepts and lists common commands in work:

Main concept

Get started quickly and understand the following concepts:

  • Workspace (Working Directory): what you can see in the computer catalog, or clone (clone) down the directory;
  • Repository (Repository): inside the work area has a hidden directory .git, this is not the work area, but Git repository;
  • Temporary storage area (stage): There is a temporary storage area called stage in the version library, and git add can put the content to be submitted into the temporary storage area;
  • Master branch (master): The repository also has a master branch called master. Git commit submits all the contents of the temporary storage area to the current branch;

Main usage

At work, generally we only need four steps to submit code:

  • The first step is to pull the code with git pull, and ensure that it is consistent with the server-side warehouse before submitting the code to avoid conflicts;
  • In the second step, git add ./your_file.txt adds the file, which is actually submitted from the work area to the temporary storage area;
  • The third step is git commit -m'first commit' to submit the changes, and then submit all the contents of the temporary storage area to the current branch (default master);
  • The fourth step, git push [remoteName] pushes to the remote warehouse, that is, pushes to the server, so that others can pull your code;

Common usage

The above four steps are also used in normal work. Of course, there are exceptions to everything. Here are some exceptions:

1. Checkout to switch branches

git checkout <branch>: switch to the branch you need (dev, hotfix)

git checkout -b <branch>: If there is no branch, add the -b parameter to create and switch;

Reference link: https://git-scm.com/docs/git-checkout

Second, the issue of revocation after git submission

There are three situations for revocation score:

  • First, the file has been modified but the git add undo method has not been executed;
    I deliberately revoke the .gitignore file without git add, and directly cancel
    it through git checkout - <file>; but this command will not undo the newly created file, because The newly created file has not been added to the Git management system, so git is unknown, just delete it manually.
λ git status
On branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
D:\learning\git\work (master -> origin)
λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   .gitignoreno changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\work (master -> origin)
λ git checkout -- .gitignore
D:\learning\git\work (master -> origin)
λ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Extension: The
command git checkout - .gitignore means to undo all the modifications of the .gitignore file in the work area. There are two situations:
one is that .gitignore has not been placed in the temporary storage area since the modification, and the modification is now undone Just return to the same state as the version library;
one is that .gitignore has been added to the staging area and modified again, and now, if you undo the modification, it will return to the state after adding it to the staging area.
In short, it is to let this file return to the state of the last git commit or git add.
Reference link: https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536

  • Second, the method of revoking the file that has been modified and git add
    needs to execute git reset .gitignore to revoke to the state of not git add, and then perform the first step.
λ git add .gitignore
D:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes to be committed:  (use "git reset HEAD <file>..." to unstage)
        modified:   .gitignoreD:\learning\git\work (master -> origin)λ git reset .gitignoreUnstaged changes after reset:M       .gitignoreD:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   .gitignoreno changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\work (master -> origin)λ git checkout -- .gitignoreD:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
  • Third, how to revoke Git's commit:
    Go back to the unmodified state directly through git reset --hard commitid.
λ git add .gitignore
λ git commit -m "test"
#(省略无用部分)λ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree cleanD:\learning\git\work (master -> origin)λ git logcommit b7de9378f39834dbc8304d4a8d30f39a4003c673 (HEAD -> master)Author: test <[email protected]>
Date:   Mon Sep 14 02:59:02 2020 +0800
    testcommit b3ed1078e543cdb26b984dac584df9db7553d506 (origin/master, origin/HEAD)Author: test <[email protected]>
Date:   Mon Sep 14 02:39:54 2020 +0800
    09142020
D:\learning\git\work (master -> origin)λ git reset --hard b3ed1078e543cdb26b984dac584df9db7553d506HEAD is now at b3ed107 09142020
D:\learning\git\work (master -> origin)λ git logcommit b3ed1078e543cdb26b984dac584df9db7553d506 (HEAD -> master, origin/master, origin/HEAD)Author: test <[email protected]>
Date:   Mon Sep 14 02:39:54 2020 +0800
    09142020
    D:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Three, git stash save and restore the contents of the workspace

Git stash can temporarily save the code that you have modified but do not want to submit (git push) to the stack, that is, return to the state when you git pull. Then you can switch branches at will to fight the fire, switch back and then git push pop to restore the previous changes. Stash can not only restore to the branch originally developed, but also to any other designated branch (cross-branch).

  • git stash
    saves the current work progress and saves the changes in the temporary storage area and the workspace. After executing git status, you will find that it is currently a clean workspace without any changes.
    Use git stash save'message...' to add some comments
  • git stash list
    displays a list of save progress. This means that the git stash command can be executed multiple times.
  • git stash pop
    restores the latest progress to the workspace. By default, git will restore the changes in the workspace and temporary storage area to the workspace.
  • git stash pop stash@{stash_id}
    restore the specified progress to the work area. The stash_id obtained by the git stash list command will delete the current progress after the progress is restored by the git stash pop command.
  • git stash drop stash@{stash_id}
    can use the git stash drop command, followed by stash_id or use the git stash clear command to delete all cached stash
  • git stash show
    view the difference between the latest stash saved in the stack and the current directory

for example

  1. Modify the .gitignore file and create a new test.txt file. When executing stash, it is found that the new file will not be stored;
  2. After git add, stash found that the workspace is clean;
  3. Explain that files that are not in git version control (git add) cannot be saved by git stash;
  4. Finally, restore through git stash pop.
λ git status
On branch masterYour branch is up to date with 'origin/master'.
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:   .gitignoreUntracked files:  (use "git add <file>..." to include in what will be committed)
        test.txtno changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
Untracked files:  (use "git add <file>..." to include in what will be committed)
        test.txtnothing added to commit but untracked files present (use "git add" to track)
D:\learning\git\timed_tasks (master -> origin)
λ git add test.txt
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash liststash@{0}: WIP on master: 542a055 create .gitignore
stash@{1}: WIP on master: 542a055 create .gitignore
stash@{2}: WIP on (no branch): 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
nothing to commit, working tree clean
D:\learning\git\timed_tasks (master -> origin)
λ git stash show test.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
D:\learning\git\timed_tasks (master -> origin)
λ git stash popOn branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt
Dropped refs/stash@{0} (b69da2894d5e7f511be18277c5a0cd4582fbf453)
D:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

Tip: What if you don’t want all the files you modified? Can be cleared by git stash, okay?

λ git status
On branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash clearD:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
nothing to commit, working tree clean

git quick start and common usage

 

λ git status
On branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash clearD:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
nothing to commit, working tree clean

Guess you like

Origin blog.csdn.net/mrchaochao/article/details/108625832