Common Git commands and git commands
Since work, the source code management tool has used SVN, TFS, Git, and finally exclusive Git
This blog summarizes the Git commands used in my work. The main directories are as follows:
1. obtain remote code
Generally, every company that uses Git as the source code management tool will have its own Git server. When we obtain the project permission, the first step will definitely download the project code to the local machine,
The command is as follows:
git clone https://github.com/zwwhnly/WeiXinMPSDK.git
When the following window appears, the Code has been downloaded
At this time, you will find that a WeiXinMPSDK folder is added locally, and continue to execute the following command to switch to the Code directory
cd WeiXinMPSDK/
The following interface appears, indicating that the master branch of the Code is switched:
If the server code is updated, You need to obtain the latest code and execute the following command:
git pull
The following interface is displayed. Already up-to-date indicates that the Code is in the latest version.
2. View/modify configuration information
To view all configuration information, run the following command:
git config --list
To view the configuration information, run the following command:
git config user.name
The command to modify the configuration information is as follows:
git config --global user.name “xxx”git config --global user.email xxx@foxmail.com
3. View/submit the modified Code
Run the following command to check the local code status:
git status
If no code has been modified, the following situation will occur:
If there is any modification, it will appear as shown in
In this case, run the git add command to trace/save the file
git add .
Modify the file again. For example, if you add a comment to the method and run the git status command again, the file appears twice,
In fact, git only saves the version used to run the git add command. If the code is submitted now, the version before the annotation is submitted.
Therefore, after running the git add command, you need to re-run the git add command to save the latest version of code.
At this time, the modified Code has not yet been submitted. You must execute the git commit command before submitting the code. The content in the double quotation marks can be customized according to the code modified each time.
git commit -m "git commit test"
To make it faster, git allows the add command and the commit command to run the Code together, as shown below:
git commit -a -m "git commit test"
The code after executing the commit command cannot be obtained on the remote server. To submit the code to the remote server, you must execute the git push command.
git push origin master
Note: In some cases, force push is required to submit code to the remote server.
git push --force origin master
4. View Details of each submission
After the code is submitted, if you want to know the details of your modifications, you can run the gitk command to view the submission history and see which part of the code you have modified.
gitk
When you view the modified Code, Chinese garbled characters are found as follows:
Run the following git command to solve Chinese garbled characters:
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding gbk
export LESSCHARSET=utf-8
In this case, you can run the gitk command to check whether Chinese characters can be properly displayed.
5. Version rollback
Sometimes, after we submit the code, we will find that the submission is incorrect and the version must be rolled back.
First, run the git log command to view the submission history. Each commit has a corresponding commitid.
git log
To display a single line, run the following command:
git log --pretty=oneline
Either way, the yellow font represents the commitid each time. Check the version you want to roll back to and copy the commitid. Run the following rollback command:
git reset --hard 4751a9ae0b81e98e262064b308cd9372bc58b04f
6. Branch Management
View local branches:
git branch
View All branches:
git branch -a
Create a branch. For example, create a dev branch.
git branch dev
Switch the branch. For example, switch to the created dev branch.
git checkout dev
Delete A branch. For example, delete the created dev branch.
git branch -d dev
Note: You cannot delete the current branch. For example, you cannot delete the dev branch when you are in the dev branch.
Create a branch and switch the branch. For example, create a dev branch and switch to the dev branch.
git checkout -b dev
Merge the code of a branch to the current branch, such as the dev branch. Merge the code of the master branch to the dev Branch:
git merge master