Git Study Notes#
Tips for Usage#
Searching within a Github Repository#
On the repository page, press T and then directly enter the file name.
Records#
Configuration of Gitee#
Because the SRTP project is hosted on Gitee, I tried to configure Gitee today. It took some time, and the record is as follows:
- The main reference was Configuring Gitee and GitHub Simultaneously, but the first step on it is to clear the global settings of Git. I was a bit hesitant because I was afraid that clearing it would cause some errors in the settings I made before.
- I looked at several other tutorials and found that Configuring GitHub and Gitee to Coexist did not mention clearing the global settings. So I followed the steps in that tutorial and set it up smoothly.
- One thing I learned is that both
config
andid_rsa
can be opened and edited with Notepad.
Workflow for Collaborative Development#
This time, the group project used Huawei Cloud, so the workflow is based on Huawei Cloud.
# Note: Replace the Chinese text in the code with the actual content
# Determine the tasks to be completed in this code development, create a remote branch x, and provide relevant information (branch name, description, associated work items)
git pull # Ensure that the code and branch are up to date
git checkout -b local_branch_name origin/remote_branch_name # Check out the remote branch to the local repository
# Code development
git add .
git commit -m "Appropriate annotation"
git push # Push the code to the remote branch. This operation can be done frequently during development. The benefits are code backup and version management.
# After completing the tasks for this code development (make sure the functionality is implemented and there are no issues with local debugging)
# Proceed to merge the branches
# Create a merge request in Huawei Cloud (can set reviewers and approvers to ask others for help)
# After the merge is completed (by default, the source branch is deleted after the merge)
git remote prune origin # When the remote branch is shown as deleted on Huawei Cloud, but still appears when executing git branch -r, run this command
git checkout master # Switch back to the master branch (after completing a task, it is recommended to switch back to the master branch to avoid losing the code you wrote when pulling)
git branch -d local_branch_name # Delete the local branch used for this task (can also be kept as a backup)
Understanding Remote Branches#
There are potentially three versions of every remote branch:
- The actual branch on the remote repository
- The snapshot of that branch locally
- A local branch that might be tracking the remote branch
Common Commands#
add#
git add .
filters based on .gitignoregit add *
ignores .gitignore and adds all files
Pushing#
Three steps for pushing files:
git add
git commit -m "Enter your message"
git push
Checking Status#
git status
Branches#
-
Switch to another branch:
git checkout {branch_name}
-
View local branches:
git branch
-
View remote branches:
git branch -r
-
View local and remote branches:
git branch -a
-
Delete local branch:
git branch -d {local_branch_name}
-
Force delete local branch:
git branch -D {local_branch_name}
-
Delete remote branch:
git push origin --delete {remote_branch_name}
-
If a remote branch has been deleted but still appears when running
git branch -a
:git remote prune origin
Logs#
git log
to view branch commit historygit reflog
also shows logs, but mainly displaysreset --hard
Code Reversion#
git reset --hard {commit_id}
git reset --hard HEAD^
to revert to the previous version
Errors and Solutions#
-
Error:
Updates were rejected because the remote contains work that you do not have locally.
- Scenario: When trying Gitee, I first created a remote repository. Then I created a folder with the same name locally, and then ran the following commands in the folder:
git init git remote add origin https://gitee.com/spike23187/hello-gitee.git
When I tried to push, I got this error.
- Solution: According to the prompt below, I didn't pull first, so the local files were not up to date.
- Scenario: When trying Gitee, I first created a remote repository. Then I created a folder with the same name locally, and then ran the following commands in the folder:
-
Error:
Updates were rejected because the tip of your current branch is behind its remote counterpart
- Scenario: In the above scenario, after running
git pull origin master
, I got this error. - Solution:
git pull origin master --rebase
- Reference: Common Git Error: Updates were rejected because the tip of your current branch is behind
- Lesson Learned: Although I have been using GitHub to host code for some time, I have always used plugins to simplify the operations. This is the first time I used Git Bash and encountered two errors. I feel that there is still a long way to go to master Git.
- Scenario: In the above scenario, after running
-
Error:
- Scenario: Error occurred when pushing, indicating a network issue.
- Solution:
git config --global http.proxy 'http://127.0.0.1:7890' git config --global https.proxy 'http://127.0.0.1:7890'