Git advanced usage-advanced usage and conflict avoidance

1. Commonly used commands

1. Code regular preservation

  1. The programmer’s development habit is that after typing a piece of code that he thinks, he likes to save it with Ctrl+S. I
    Put the code in
    personally suggest that 代码之前做好代码差异比对,检查好提交文件的状
    if the code is modified and saved, you can withdraw it if you don’t want to keep git restore'file name'.
    View the file statusgit status

2. git stash temporary save

Note: Execute before git add
and encounter other requirements during the code development process. If the code developed at this stage is not rolled back, you can save the code temporarily

  1. git stash save'temporary comment information' —> will show your temporary location stash@{0} 0 is the start of the index
  2. git stash list ------>View the list of temporary areas
  3. git stash pop stash@{num} ------>Restore work progress to the workspace {restore the most recent time by default}
  4. git stash apply stash@{num}------>Restore the work progress to the work area and the work progress can be restored repeatedly
  5. git stash drop stash@{num}------>Delete the saved work progress {restore the most recent one by default}
  6. git stash clear -------->Delete all saved work progress.

3. git add

  1. git add. -----> will add all local untrack files to the temporary storage area, and filter according to .gitignore
  2. git add * -----> will add all local untrack files to the temporary storage area, and will not filter based on .gitignore
  3. git add -A -----> commit all changes
  4. git add -u ----->Submit modified and deleted files, excluding new files (new)
    If you don’t want to keep git restore --staged'file name after the code is placed in the staging area 'To withdraw

4. git commit

  1. git commit -m "Remarks" ------>Submit the staging area to the local warehouse
  2. git commit -a "Remarks" ------>Submit the staging area to the local warehouse, no need to execute the git add command
    修改注释
  3. git commit --amend ----->modify the last comment --> i edit --> Esc to exit --> :wq save and exit
    rebase -i HEAD~2---->You can modify which comment you want The pick in front of the comment is replaced with edit. The method is the editing method mentioned above: i—Edit, replace pick with edit—Esc—:wq.—>git commit --amend—>git rebase --continue
    If the code is placed in the warehouse, you don't want to keep
    $ git restore- s HEAD~1 xxx // This naming means to roll back the version to the previous version of the current snapshot
    $ git restore -s 91410eb9 xxx // Change the command to specify a clear commit id and roll back to the specified snapshot
    $ git reset - soft HEAD^ // This command means to revoke commit to the last commit version

5. git push

  1. git push <remote host name> <local branch name>:<remote branch name>
  2. The local and remote branch names are the same git push <remote host name> <local branch name>
  3. git push origin --delete xxx delete remote branch name
  4. git push --set-upstream origin xxx-----create the corresponding remote warehouse

6. git pull

  1. git pull origin master:brantest----> pull the remote master branch to merge the local bratest branch.
  2. If the remote branch is merged with the current branch—>git pull origin master

Two, branch management

1. Create a new branch

  1. git branch xxx New branch
  2. git checkout -b xxx Create and jump

2. Switch branches

  1. git checkout xxx switch a branch

3. View branches

  1. git branch lists all local branches
  2. git branch -a lists all remote branches

4. Merging branches

  1. git merge branch name

3. Resolve conflicts

  • Avoid soft conflict
    1. Update local code git pull before daily development
    2. Before submitting the code add -->commit-->git pull origin develop:develop-->git push
  • Resolve hard conflicts
    Insert picture description here
  1. If there is a conflict, analyze whether the conflict area is your own or another developer's code, and if it is another person's code, you need to communicate and resolve it.
  2. Own local code conflict, confirm that the reserved part of the code accepts the current change (Accept Current Change) accepts the incoming change (Accept Incoming Change)
  3. Then add the code to git add. —> git commit -m "Remarks" —> Submit git push
  • When the frequency of development is high, try to keep your local code consistent with the version code, reduce differences, and avoid conflicts

Guess you like

Origin blog.csdn.net/weixin_45176415/article/details/115366854