HR: Can you answer these frequently asked git questions?

Summary

Git is a powerful distributed version control system that provides many useful features to help developers manage code. In this article, we summarize some of the frequently asked questions about Git interviews, including branch management, code conflict resolution, and code rollback. With these tools and commands, developers can more easily manage version history and branches of code, resolve code conflicts, roll back code, and more.

What is the Git workflow like?

Git's workflow includes: workspace, staging area, and repository. When you modify the file, first save the modified file in the work area, then add the modified file to the temporary storage area through the git add command, and finally submit the file in the temporary storage area to the repository through the git commit command.

  • Create a branch: Use the git branch command to create a new branch. For example, to create a new branch called "dev", you can run the command git branch dev.

  • Switch branch: Use the git checkout command to switch to an existing branch. For example, to switch to the branch named "dev", you can run the command git checkout dev.

  • View branches: Use the git branch command to view all available branches and the current branch. For example, to view all branches, you can run the command git branch -a.

  • Merging branches: Use the git merge command to merge a branch into the current branch. For example, to merge the branch named "dev" into the current branch, run the command git merge dev.

  • Delete branch: Use the git branch -d command to delete an existing branch. For example, to delete the branch named "dev", you can run the command git branch -d dev.

  • Push branch: Use the git push command to push the local branch to the remote warehouse. For example, to push a branch named "dev" to a remote repository, run the command git push origin dev.

  • Pull branch: Use the git pull command to pull a branch from the remote warehouse to the local. For example, to pull the branch named "dev" from the remote warehouse to the local, you can run the command git pull origin dev.

  • Tag version: Use the git tag command to tag a version in the Git repository. For example, to create a tag called "v1.0" on the latest commit of the current branch, you can run the command git tag v1.0.

What are the common commands of Git?

Common commands of Git include: git init, git add, git commit, git status, git log, git branch, git checkout, git merge, git pull, git push, etc.

How to do Git branch management?

Git's branch management includes: creating branches, switching branches, merging branches, etc. You can use the git branch command to create branches, use the git checkout command to switch branches, and use the git merge command to merge branches.

  • Create a branch: Use the git branch command to create a new branch. For example, to create a new branch called "dev", you can run the command git branch dev.

  • Switch branch: Use the git checkout command to switch to an existing branch. For example, to switch to the branch named "dev", you can run the command git checkout dev.

  • View branches: Use the git branch command to view all available branches and the current branch. For example, to view all branches, you can run the command git branch -a.

  • Merging branches: Use the git merge command to merge a branch into the current branch. For example, to merge the branch named "dev" into the current branch, run the command git merge dev.

  • Delete branch: Use the git branch -d command to delete an existing branch. For example, to delete the branch named "dev", you can run the command git branch -d dev.

  • Push branch: Use the git push command to push the local branch to the remote warehouse. For example, to push a branch named "dev" to a remote repository, run the command git push origin dev.

  • Pull branch: Use the git pull command to pull a branch from the remote warehouse to the local. For example, to pull the branch named "dev" from the remote warehouse to the local, you can run the command git pull origin dev.

  • Tag version: Use the git tag command to tag a version in the Git repository. For example, to create a tag called "v1.0" on the latest commit of the current branch, you can run the command git tag v1.0.

How does Git resolve conflicts?

Conflicts can arise when multiple people are making changes to the same file. You can use the git diff command to view the conflicting parts, then manually resolve the conflict, and finally use the git add and git commit commands to submit the changes.

  • Pull the latest code: Before modifying your own code, you should first use the git pull command to pull the latest code from the remote warehouse. This can avoid some unnecessary conflicts.

  • View conflicting files: When Git finds conflicts, it will prompt you which files have conflicts. You can use the git status command to view the file status, or you can use the git diff command to view the file differences.

  • Manual conflict resolution: When Git finds a conflict, it will mark the conflicting part in the conflict file. The file can be manually edited to merge changes from the two versions into one. After the modification, you need to use the git add command to add the modified file to the temporary storage area.

  • Use merge tools: Sometimes it can be difficult to manually resolve conflicts. In such cases, you can use the merge tools provided by Git to resolve conflicts. You can use the git mergetool command to start a merge tool, such as vimdiff or meld, and follow the prompts to resolve conflicts.

  • Submit the merge result: After resolving the conflict, you need to use the git commit command to submit the merge result. When committing, provide a clear commit message describing the changes made.

How to resolve git conflicts in idea

Open the conflicting file in IDEA: When Git finds a conflict, it will mark the conflicting file in IDEA. You can find this file in the Git tool window and double-click to open it.

  • View conflicts: In the opened file, the conflicting parts will be displayed with special marks. Usually, the three symbols "<<<<<<<", "=======" and ">>>>>>>" surround the conflicting part.

  • Resolving conflicts: You can manually edit the conflicting parts, or use the merge tool provided by IDEA to resolve conflicts. To use the merge tool provided by IDEA, you can select "VCS" > "Git" > "Resolve Conflicts" in the menu bar. In the pop-up dialog box, select the "Launch External Merge Tool" option, and follow the prompts to resolve the conflict.

  • Mark as resolved: After resolving conflicts, the file needs to be marked as resolved. You can choose "VCS" > "Git" > "Resolve Conflicts" in the menu bar, and then select the "Mark as Resolved" option in the pop-up dialog box.

  • Submit the merged result: Finally, use IDEA's commit tool to submit the merged result. You can choose "VCS" > "Git" > "Commit File" in the menu bar, and then provide a clear commit message in the pop-up dialog box, describing the changes made.

How to resolve git conflicts in vscode

  • Open conflicting files: When Git finds a conflict, it flags the conflicting file in Visual Studio Code. You can find this file in the Git panel and double-click to open it.

  • View conflicts: In the opened file, the conflicting parts will be displayed with special marks. Usually, the three symbols "<<<<<<<", "=======" and ">>>>>>>" surround the conflicting part.

  • Resolving conflicts: You can manually edit conflicting parts, or use the merge tool provided by Visual Studio Code to resolve conflicts. To use the merge tool provided by Visual Studio Code, you can choose "Git" > "Open Changes" in the menu bar. In the Git panel that opens, select the conflicting file and click the "Accept Changes" button. In the dialog box that pops up, select the changes you want to keep, and click the "Accept Selected Changes" button.

  • Mark as resolved: After resolving conflicts, the file needs to be marked as resolved. Select the conflicting file in the Git panel and click the "Mark as Resolved" button.

  • Commit the merged results: Finally, use Visual Studio Code's commit tool to commit the merged results. You can choose "Git" > "Commit" in the menu bar, and then provide a clear commit message in the pop-up dialog box, describing the changes made.

How does Git perform code rollback?

Git can implement code rollback through version control. You can use the git log command to view the commit history, and then use the git reset or git revert command to roll back the code to the specified version.

git revertWhen rolling back the code, *** will create a new submission record to undo the previous submission, so the previous submission history will not be modified, and the previous submission record will not be deleted, so that the integrity of the submission history can be maintained. ***If you need to restore again after the rollback, you can use git revertthe command to undo the previous undo operation again.

git resetWhen rolling back the code, *** will point the current branch pointer to a submission before the rollback, so the previous submission history will be modified, and the subsequent submission records will also be deleted. ***This rollback method is irreversible, because the commit record after the rollback has been deleted. If you need to restore it again, you can only use git reflogthe command view the historical operation records, and then use git resetthe command to restore to the state before the rollback.

Guess you like

Origin blog.csdn.net/qq_27575627/article/details/130179990