PyCharm configuration and use of Git tutorial

We are usually used to developing under Windows, but we need to clone the code of the remote warehouse locally in real time, and we also need to push the code we modified to the remote server. The following are the steps to configure Git for Pycharm under Windows:

1. Install PyCharm

Skip it here.

2. Install Git

Reference blog: Installation, configuration and use of Git and TortoiseGit under Windows

3. Configure Git plug-in in PyCharm

Select File -> Settings -> Version Control -> Git
to configure the installation path of Git, and click the Test button to test whether the configuration is successful.
Insert image description here
The Git version number pops up, indicating that the configuration is successful.

Insert image description here

4. Connect to the remote warehouse

Select VCS --> Get from Version Control

Insert image description here

Note: The local project path here requires an empty directory!

5. Clone project code

After configuring the Git remote warehouse address and local project path, click the Clone button in the lower right corner of the page to pull the project code.
Insert image description here

6. Submit local files to remote repository

6.1 git add

Right-click the file you want to submit and click Git->Add. If you have used the Git command, you should know that this is equivalent to the git add file name.

Or enter from the top taskbar: VCS -> Git -> Add.
Insert image description here

6.2 git commit

Right-click the file name -> Git -> Commit File. It is the same as the git commit -m <description> command.

Insert image description here

6.3 git push

Right-click the file name -> Git -> Repository -> push
You can also use the shortcut key: Ctrl + Shift + K

Insert image description here

6.4 git pull

Right click on the file name -> Git -> Repository -> pull

7. Code rollback

In order to demonstrate how to use PyCharm to roll back the code on Github, we make another submission, this time changing the Version to 3.0, and follow the steps for updating the warehouse just now. You can view the records of three submissions in the Version Control in the lower left corner of PyCharm, as shown below:
Insert image description here

You can also click on the modified file to view it through Git -> History:
Insert image description here

Select the version file that needs to be rolled back, right-click and select revert to roll back to the corresponding version.

Looking at the files in the project again, they turned blue. We need to commit and push again to submit the rolled-back version of the code to the remote warehouse.
Insert image description here

8. Branch operations

After using PyCharm to open the cloned local project, the branch information of the project can be found in Git:master at the bottom right of the IDE, as shown below:

Insert image description here

8.1 Create a new branch

Select "+ New Branch" to create a new branch. Let's create a new branch named 1.0.0:
Insert image description here

After creation, you will find that "Git:master" in the lower right corner has been displayed as "Git:1.0.0", indicating that the local branch of the project has been switched to the 1.0.0 branch.
The code modification operations we make in the local 1.0.0 branch can be submitted to the 1.0.0 branch of the remote warehouse through Git commit and push operations.
Insert image description here

8.2 Switch branches

Branch switching in PyCharm is very simple. We click "Git: 1.0.0" under the current local branch in the lower right corner again. Select the branch that needs to be switched, such as master, and then click checkout to complete the branch switching.

Insert image description here

8.3 Merge branches

If we also need to merge the 1.0.0 branch into the master branch. First switch the project to the master branch, right-click --> Git --> Repository --> Merge Changes.

In the pop-up box, select the branch that needs to be merged as the local 1.0.0 branch, and write the Commit Message, as shown below:
Insert image description here
After clicking the "Merge" button, the project has been successfully merged.

If we still want to submit the merged master branch to Github, we need to pay attention at this time. After right-clicking and selecting Git, first select "Add" and then push to Github. Note that we select "Add" instead of "Commmit Directory" ". At this time, we check the contents of the master branch on Github, as follows:
Insert image description here
You can see that the remote branch has been successfully merged, and we have successfully merged the modifications in the 1.0.0 branch into the master branch.

8.4 Delete branches

The operation of branch deletion is also very simple. It is very similar to the operation of branch switching. We click "Git master" in the lower right corner, click the test local branch that needs to be deleted, and then select "Delete".

9. Resolve local and remote conflicts

Note: Before submitting local code, be sure to pull the remote warehouse first and then push!!!

9.1 Merge operation

In actual use, we usually collaborate with multiple people. The remote warehouse may have been updated before submission. In order to avoid unnecessary trouble, we must pull and synchronize first and then push.

On the git page, edit the test.py file online to simulate other people's submissions.
Insert image description here
Modify test.py locally:
Insert image description here
and create a new commit to the remote 1.0.0 branch. The system rejected this push because the files in the remote warehouse have changed. Here, we usually choose the Merge operation to resolve conflicts.
Insert image description here

Here, because we modified the test.py file locally and committed it to the local branch, and the test.py
file in the remote warehouse was also modified, there is a conflict at this time. After selecting Merge, there are three options. Accept left is to save local modifications, accept
right is to save remote modifications, and the middle one is the result of your manual merge.

9.2 Stash operation

Reference blog: Solve the problem that git pull cannot pull remote code due to conflicts between local code and remote code

You can stash the local content into the warehouse first. After performing the stash operation, the local code will return to the content before modification. At this time, the remote code can be downloaded to the local computer normally. Then the contents in the warehouse are merged locally through stash operation, and if there are conflicts, they can be resolved.

10. Cancel commit

10.1 Cancel a commit without push

Right-click the commit of the corresponding branch under local and click undo commit. If you want to cancel multiple commits, cancel them from top to bottom, not from the middle.

10.2 Cancel an already pushed commit

Right-click the commit of the corresponding branch under remote, click revert commit, and then commit. The commit information of revert... will be automatically added to you. Then push, and the remote warehouse will be updated to the previous version.
The principle is to reverse commit, submit the previous version of the file and restore it to the previous version.

References

Guess you like

Origin blog.csdn.net/u012856866/article/details/132688006