Git usage and common commands

Time:2021-6-24

Git application process in team project

1. First, from a git remote warehouse clone project to the local

Git clone warehouse address

2. Create development branch

Generally, we don’t write code on the master branch, but create a new branch


git checkout -b test

3. Modify the code on the test branch, such as completing the development of a function

4. Submit the code to the test branch after modification

git add .
git commit -m "your comment"

  5. Review code (not required)

After developing a function on the test branch, I suggest you review the modified code and compare it with the master branch

6. Switch to the master branch


git checkout master

7. Update the master branch

Git pull (keep the local master consistent with the latest server code)

8. Switch to the test branch again


git checkout test

9. Update the master code to the test branch

Git merge master (merge the master branch into the current branch)

10. After handling the conflict, submit the test branch to the server

Git push origin branch name

11. Submit merge request

After the merge request is submitted, the code audit team will audit the submitted code. If there is no problem, they will agree to the merge request and merge the code of the test branch into the master branch

Tips for using Git

Use of stash

Sometimes there is a scenario: when you develop a function on a local branch, there is an urgent bug to deal with. However, before the current function is developed, you don’t want to submit. If you don’t submit, you can’t switch branches. At this time, you can use the stash command to temporarily store the current code.


git add .
git stash

At this time, your modified code will be temporarily stored in a stack like container, and the code will be restored to the last submitted location.
You can execute git stash list to see whether to hold the list of stacks.

After storing the code, you can switch the branch. At this time, you can switch to another branch to fix the bug. After that, switch to the branch and execute the following command to recover


git stash pop

The use of black

Sometimes when we modify someone else’s code, we want to see who wrote a specific line in a file. We can use “black”

Git blade file

After executing the above command, each line will show who submitted it, and you can use the page turning key to turn the page.

Git command alias settings

Do you think it’s a bit troublesome to type git? Sure, I think so, too.
We can set the alias in the ~ /. Gitconfig file, that is, add the following lines


[alias]
 st = status
 ci = commit
 br = branch
 co = checkout
 df = diff

Aliases can be set according to your own habits.

Common git commands

Git branch - R / - a view remote branches / local and remote branches
Git checkout branch name switch branch
Git checkout - b create a new branch name and switch to the new branch
Git branch - D branch name delete local branch
Git pull updates the local branch to the latest
Git merge branch name merges a branch to the current branch
Git diff view the content modification without add at present
Git diff -- cached view has been added, no commit changes
Git diff head is a combination of the above two commands
Git log -- oneline -- number displays log, one line for each log and number bar for each log
Git add. Adds the modified code to the cache
Git push origin branch name pushes the specified branch to the server
Git status to view which changes have been added to the staging area for submission and which have not been added to the staging area
Git reset head file name cancels the temporary storage of a file