Git Study Notes#
Tips for Using#
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 little hesitant because I was afraid that clearing it would cause some errors with the settings I had 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 situation
# Determine the tasks to be completed for this code development, create a remote branch x, and clearly indicate the relevant information (branch name, description, associated work item)
git pull # Make sure 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 (please make sure the functionality is implemented and there are no issues with local debugging)
# Next, merge the branches
# Create a merge request in Huawei Cloud (you 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 (you can also keep it 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 any 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 deleted remote branch still appears when running
git branch -a
:git remote prune origin
Logs#
git log
view branch commit historygit reflog
also view logs, but it will displayreset --hard
Code Reversion#
git reset --hard {commit_id}
git reset --hard HEAD^
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 my 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, I ran
git pull origin master
and got this error. - Solution:
git pull origin master --rebase
- Reference: Git Common 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 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 use Git well.
- Scenario: In the above scenario, I ran
-
Error:
- Scenario: Error occurred when pushing, indicating a network problem.
- Solution:
git config --global http.proxy 'http://127.0.0.1:7890' git config --global https.proxy 'http://127.0.0.1:7890'