Code version control is a basic and necessary skill for our embedded software development post, which needs to be mastered. The common version control systems in practice are Git (distributed version control system) and SVN (centralized version control system).
This article shares the basic use method of Git in practical work.
Git download, installation and configuration
Git and Github
-
Git: distributed version control system.
-
GitHub: Hosting platform for open source and proprietary software projects. Similar to GitHub is Gitee, a Git-based code hosting service launched by Open-source China. The following example uses Gitee to demonstrate this.
-
Git and GitHub only support Git as the only version library format for hosting, hence the name GitHub.
2. Git download and installation
(1) Download:
Method 1 (download from official website) :
Address: git-scm.com/
The official website download speed is slightly slower.
Method 2 (download from mirror website) :
Address: npm.taobao.org/mirrors/git…
The mirror website downloads quickly.
(2) Installation:
Click the default options to install after downloading. Right click in any folder to see:
Git GUI is a Git graphical client software and Git Bash is a command line terminal. You can use either of them, just use one of them if you like. Git Bash is recommended, however, if you are familiar with Git Bash, you can use some graphical clients (not just Git GUI). Having said that, once you’ve mastered Git Bash, there seems to be no need to use a graphical client.
3. Configure git locally
Right-click Git Bash in any folder and enter the following command for local configuration:
$ git config --global user.name "user"
$ git config --global user.email "email"
Copy the code
Where user is your username (optional) and email is your email address. The reason for setting these two information is that the remote library requires a key to be associated. Configure these two information to ensure that you are unique. Using the –global parameter indicates that all warehouses on your machine are configured the same. For example my configuration:
4. Associate the remote warehouse
As mentioned earlier, common remote repositories are GitHub and Gitee. The methods for associating remote repositories are the same, and here we use Gitee as an example.
(1) Register with Gitee/GitHub
(2) Create an SSH key
Enter the command:
ssh-keygen -t rsa -C "email"
Copy the code
Example Create an SSH key. In this case, two files id_rsa and id_rsa.pub are generated in the. SSH/directory under the root directory of the user. The contents in the id_rsa.pub file are copied to the Gitee account for setting.
Run cat ~/. SSH /id_rsa.pub to view the contents of id_rsa.pub. Such as:
Then make a copy of the output for later use.
(3) Set the SSH key in the remote repository
Find SSH Public Key in Gitee Settings:
Paste the SSH key we just copied into the public key input box:
Among them, the title is arbitrary. This is how Gitee sets SSH keys, and Github sets SSH keys similarly.
2. Establish association between local and remote warehouses
For example, we have a local gIT_test project:
Let’s demonstrate uploading the project to a remote repository and establishing associations. To demonstrate this, we need to look at a diagram:
This diagram is the core of using Git for version control.
The upside:
First, use the add command to add files from the working directory to the staging area. Then, commit to the local repository using the commit command. Finally, push to the remote repository using the push command.
The downside:
For the first time, you need to run the clone command to clone a copy from the remote repository to the local computer. If the remote repository has updated content, you can use pull to pull the updated content to the local.
(1) Local code management
Even if we don’t associate remote repositories, we can manage our code locally. Git_test: git init git git init
Git init generates a.git/ hidden directory in the current project directory with some related files:
Because of the.git/ directory, we can do version control. .git/ the contents are no longer explained, interested friends can do their own research.
Add the git_test project files to the staging area:
Among them:
Git status Command: check the git status. Git add <file> : Add files to the staging area. Git add. : Commits all changes in your workspace to staging.Copy the code
To submit the staging area file to the local repository:
(2) Associated remote warehouse
First, create a repository named git_test on Gitee:
After creating the warehouse, the warehouse page will have some simple instructions and commands that you can focus on. Since we have already created the local repository in the previous section, we can just follow the existing repository:
You need to enter the gitee account password for the first upload. Upload can be completed after entering the account password:
Among them, the first command is to associate the local library with the remote library, where the name of the remote warehouse origin can be changed; The second command is to push the contents of the local repository to the remote repository.
Our local repository can be synchronized to both Gitee and GitHub. The method of synchronizing to GitHub is the same as that of synchronizing to Gitee. It should be noted that the name of the remote repository cannot be Origin, because the name of our GitHub remote repository has been named Origin.
At this point, refresh the remote repository to see the file we uploaded:
At this point, we have established a connection between our local and remote repositories, and we are ready for good version management. Here are some examples of how to use Git.
Git common operations
On your first day in the company, the boss will assign you a Git account and a remote warehouse address. Suppose the address of the remote repository is:
Gitee.com/zhengnianli…
The first thing you need to do is configure Git, and then you can create a folder for the code projects you want to manage. If this folder is git_project, let’s clone the project of the remote warehouse into git_project:
This is also the project we uploaded above. We add a readme. md file and upload it to the remote repository:
You leave work early because it’s your first day. Your colleague changed the code and uploaded it like:
The next day you go to work, first pull the latest code to local, execute the command:
git pull origin master
Copy the code
Then we can have fun writing our code. When performing a pull operation, you may encounter a situation like this: We have locally modified the module1.c file, and if we change it to:
Module1.c on the remote repository has also been updated, if updated to:
Will the local module1.c file be overwritten if a pull operation is performed? Will your code be overwritten? No, because the pull fails if there is a conflict, and the conflict needs to be resolved before the pull can be restarted.
For example, if we perform a pull operation at this time, there will be a conflict:
We can use the git stash command to temporarily save our local changes, then do the pull operation, and finally restore our stash with the git stash pop command:
Again, will the temporary save overwrite the code you just pulled? It won’t. It is up to us to keep or discard, for example:
We can keep both changes, use the current change (the code just pulled from the remote repository), use the incoming change (temporarily save the restored code), and compare the changes:
Here we choose to keep both sides modified and the code becomes:
This is the most basic git operation, namely how to manage code, how to pull code, how to upload code. In addition, there are some common commands and operations that you can look up when you encounter how to use, such as how to create branches, merge branches, version rollback, etc. Because the space is too long, I will not expand the demonstration.
Four, the main points for attention
1. If you are not familiar with Git at the beginning, you can do a backup before pushing or pulling code to prevent irreversible code loss.
2, before submitting the code, make sure that the project is compiled and not modified others’ code. Use a comparison app (such as Beyond Compare) to see what changes you made, if they were necessary, and what you threw out.
5. Some Git graphical clients and tools
Use the command line or use the graphical client to see personal habits, here also share some tools (recommended by Gitee from the code cloud) :
-
Sourcetree Client (Windows, Mac)
-
Giggle Client (Linux)
-
Git Client (Windows)
-
TortoiseGit (Windows)
-
Git Extensions client (Windows, Mac, Linux)
-
SmartGit Client (Windows, Mac, Linux)
-
GitEye Client (Windows, Mac, Linux)
-
Gitg client (Windows, Linux)
-
Ungit Client (Windows, Mac, Linux)
-
Git-cola Client (Windows, Mac, Linux)
-
Tower Client (Mac)
-
Gitbox Client (Mac)
-
GitUp Client (Mac)
6. Git learning materials
1. Liao Xuefeng Git tutorial
www.liaoxuefeng.com/wiki/896043…
It’s very detailed, with short video demonstrations.
2, Learn Git shoot
Git Git Git Git Git Git Git Git
Seven, some common commands
The following commands are from:
www.ruanyifeng.com/blog/2015/1…
1. Create a new code base
Create a new Git repository in the current directory
$ git init
Create a new directory and initialize it as a Git code base
$ git init [project-name]
Download a project and its entire code history
$ git clone [url]
Copy the code
2, configuration,
Git’s Settings file is.gitconfig, which can be in the user home directory (global configuration) or in the project directory (project configuration).
Display current Git configuration
$ git config --list
Edit Git configuration files
$ git config -e [--global]
Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
Copy the code
3. Add/delete files
Add the specified file to the staging area
$ git add [file1] [file2] ...
Add the specified directory to the staging area, including subdirectories
$ git add [dir]
Add all files from the current directory to the staging area
$ git add .
Before adding each change, ask for confirmation
For multiple changes to the same file, we can implement multiple commits
$ git add -p
Delete the workspace file and place the deletion in the staging area
$ git rm [file1] [file2] ...
# stop tracking the specified file, but it will remain in the workspace
$ git rm --cached [file]
Rename the file and place the rename in the staging area
$ git mv [file-original] [file-renamed]
Copy the code
4. Code submission
Submit the staging area to the warehouse area
$ git commit -m [message]
Submit the file designated for the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message]
Commit workspace changes since the last commit, directly to the warehouse
$ git commit -a
Display all diff information when submitting
$ git commit -v
Use a new COMMIT instead of the previous commit
If there are no new changes to the code, it is used to overwrite the commit information from the last commit
$ git commit --amend -m [message]
Rework the last commit and include new changes to the specified file
$ git commit --amend [file1] [file2] ...
Copy the code
5, branch
# list all local branches
$ git branch
List all remote branches
$ git branch -r
List all local and remote branches
$ git branch -a
Create a new branch, but remain in the current branch
$ git branch [branch-name]
Create a new branch and switch to that branch
$ git checkout -b [branch]
Create a new branch that points to commit
$ git branch [branch] [commit]
Create a new branch and set up a trace relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]
Switch to the specified branch and update the workspace
$ git checkout [branch-name]
# Switch to the previous branch
$ git checkout -
Establish a trace between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]
Merge the specified branch into the current branch
$ git merge [branch]
Select a COMMIT and merge it into the current branch
$ git cherry-pick [commit]
# delete branch
$ git branch -d [branch-name]
# delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
Copy the code
6, labels,
# list all tags
$ git tag
Create a new tag for the current commit
$ git tag [tag]
# Create a new tag before specifying commit
$ git tag [tag] [commit]
# delete the local tag
$ git tag -d [tag]
# delete remote tag
$ git push origin :refs/tags/[tagName]
# check the tag information
$ git show [tag]
# submit the specified tag
$ git push [remote] [tag]
# submit all tags
$ git push [remote] --tags
Create a new branch that points to a tag
$ git checkout -b [branch] [tag]
Copy the code
7. Check information
# show the changed file
$ git status
Display the version history of the current branch
$ git log
Display the commit history and the files that changed each commit
$ git log --stat
# Search submission history by keyword
$ git log -S [keyword]
Display all changes after a commit, with each commit occupying one row
$ git log [tag] HEAD --pretty=format:%s
All changes made after a commit must match the search criteria
$ git log [tag] HEAD --grep feature
Display the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]
# display every diff associated with the specified file
$ git log -p [file]
# show the last 5 commits
$ git log -5 --pretty --oneline
# display all submitted users, sorted by number of submitted users
$ git shortlog -sn
# show when and by whom the specified file was modified
$ git blame [file]
Display the difference between the staging area and the workspace
$ git diff
Show the difference between the staging area and the last commit
$ git diff --cached [file]
Show the difference between the workspace and the latest COMMIT of the current branch
$ git diff HEAD
Display the difference between two commits$ git diff [first-branch]... [second-branch]# show how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}"
Display metadata and content changes for a commit
$ git show [commit]
Display the files that have changed during a commit
$ git show --name-only [commit]
Display the contents of a file at the time of a commit
$ git show [commit]:[filename]
Displays the most recent commits for the current branch
$ git reflog
Copy the code
8. Remote synchronization
Download all changes to the remote repository
$ git fetch [remote]
Display all remote repositories
$ git remote -v
Display information about a remote repository
$ git remote show [remote]
Add a new remote repository and name it
$ git remote add [shortname] [url]
Fetch the changes from the remote repository and merge them with the local branch
$ git pull [remote] [branch]
Upload the local branch to the remote repository
$ git push [remote] [branch]
Push current branch to remote repository forcibly, even if there is conflict
$ git push [remote] --force
Push all branches to the remote repository
$ git push [remote] --all
Copy the code
9, cancellation
Restore the specified file from the staging area to the workspace
$ git checkout [file]
Restore the specified files of a COMMIT to the staging area and workspace
$ git checkout [commit] [file]
Restore all files from the staging area to the workspace
$ git checkout .
Reset the specified file in the staging area, same as the last commit, but with the same workspace
$ git reset [file]
Reset the staging area and workspace as the last COMMIT
$ git reset --hard
Reset the pointer to the current branch to commit, and reset the staging area, but the workspace remains the same
$ git reset [commit]
Reset the HEAD of the current branch to the specified COMMIT, and reset the staging area and workspace to the same as the specified COMMIT
$ git reset --hard [commit]
Reset the current HEAD to specify commit, but leave the staging area and workspace unchanged
$ git reset --keep [commit]
Create a commit to revoke the specified commit
All changes of the latter are cancelled out by the former and applied to the current branch
$ git revert [commit]
Uncommitted changes are temporarily removed and moved in later
$ git stash
$ git stash pop
Copy the code
10 and other
Generate a zip package ready for distribution
$ git archive
Copy the code