Contents
The project involves synchronizing code across multiple devices. The same code exists on a laptop, desktop, and industrial PC, and it needs to be debugged on all of these platforms. Furthermore, when debugging code on experimental equipment, many uncontrollable factors may cause code loss. Therefore, a platform-based solution for synchronizing and preserving code is needed to prevent such loss.
I. Introduction
When making major changes to project code, we may worry that something could go wrong. We therefore need to create multiple versions of the code files and use Git for version control so that the code can be saved and restored.
- Git: A distributed version control system used to track file changes, record history, and manage branches.
- GitHub: An online Git-based code hosting platform that provides remote repositories, permission management, Issues, pull requests, and other collaboration features.
II. Approach
Individual developers may need to modify code frequently. However, committing every change to Git can cause many problems:
- Version management becomes very disorganized, making conflicts between multiple devices likely.
- It becomes difficult to find the appropriate version when restoring code.
- Frequent commits generate a large amount of version information, causing the repository size to expand rapidly.
Git can solve this problem through branches. For example, after completing each milestone version, create a branch as a backup. This prevents incorrect changes to the main branch from making earlier versions of the entire project’s code impossible to recover.
The specific approach is as follows:
- Create a remote repository on GitHub, such as
my-project. - Run
git cloneon each device, such as a laptop, desktop, or industrial PC, to clone the same repository. - Before finishing work each day, use
git pushto push changes to GitHub. - Create a separate branch as a backup for every major change, such as a new feature, refactoring, or bug fix.
- Before debugging on another device, enter the project directory and run
git pull, or rungit fetch && git checkoutfor a specific branch, to quickly obtain the latest code.
III. Common Commands
| Category | Scenario | Example command |
|---|---|---|
| Repository initialization | Create a local repository for a new project for the first time | git init |
| Clone a remote repository | Retrieve an existing project on a new device or in a new directory | git clone https://github.com/用户名/my-project.git |
| Configure identity | Required when using Git for the first time | bash<br>git config --global user.name "Your Name"<br>git config --global user.email "you@example.com"<br> |
| Check status | See which files have changed and which are in the staging area | git status |
| Add to the staging area | Add new or modified files to the next commit | git add filename or git add . |
| Commit changes | Save a snapshot of the current staging area | git commit -m "修复登录接口参数校验" |
| Create a branch | Create a local branch and switch to it | git checkout -b debug |
| View branches | List all local branches | git branch -v |
| Switch branches | Switch to an existing branch | git checkout release |
| Merge branches | Merge changes from a branch into the current branch | bash<br>git checkout main<br>git merge debug<br> |
| Resolve conflicts | Edit the conflicting file after a merge conflict and mark it as resolved | Edit the conflicting file → git add filename → git commit |
| Manage tags | Mark an important commit | git tag v1.0.0 |
| Delete tags | Delete a local or remote tag | bash<br>git tag -d v1.0.0 # 本地<br>git push origin :refs/tags/v1.0.0 # 远程<br> |
| Add a remote repository | Associate the local repository with GitHub | git remote add origin https://github.com/用户名/my-project.git |
| Push to a remote | Upload local branch commits to GitHub | git push origin main |
| Pull remote updates | Retrieve and merge the latest commits from the remote repository | git pull origin main |
| Restore a file | Discard uncommitted changes in the working tree | git restore filename |
| Roll back a version | Return to the previous version after committing | bash<br>git log --oneline # 找到提交哈希id<br>git reset --hard <commit-id> # 回退且丢弃后续改动<br> |
(1) Creating a Repository
| Scenario | Example command |
|---|---|
| Create a local repository | git init |
| Retrieve an existing project | git clone https://github.com/用户名/my-project.git |
| Configure Git when using it for the first time | git config --global user.name "Your Name"git config --global user.email "you@example.com" |
(2) File Operations
| Scenario | Example command |
|---|---|
| Check the project status | git status |
| Add changes to the next commit | git add filename or git add . |
| Commit changes to the staging area | git commit -m "修复登录接口参数校验" |
(3) Restoring Files
| Scenario | Example command |
|---|---|
| Discard uncommitted changes in the working tree | git restore filename |
| Return to the previous version after committing | git log --oneline (find the version number)git reset --hard 上一个版本号 (restore the previous version and delete the current version)git reverse 当前版本号 (restore the previous version and retain the current version) |
(4) Branch Operations
| Scenario | Example command |
|---|---|
| Create a local branch and switch to it | git checkout -b debug |
| List all local branches | git branch -v |
| Switch to an existing branch | git checkout release |
| Merge changes from a branch into the current branch | bash<br>git checkout main<br>git merge debug<br> |
| Edit the conflicting file after a merge conflict and mark it as resolved | Edit the conflicting file → git add filename → git commit |
(5) Tag Commands
| Scenario | Example command |
|---|---|
| Mark an important commit | git tag "标签名" |
| Delete a local or remote tag | git tag -d "标签名" # 本地git push origin :refs/tags/标签名 # 远程<br> |
(6) Adding a Remote Repository
| Scenario | Example command |
|---|---|
| Associate the local repository with GitHub | git remote add origin https://github.com/用户名/my-project.git |
| Upload local branch commits to GitHub | git push |
| Retrieve and merge the latest commits from the remote repository | git pull |
(git pull is equivalent to git fetch + git merge: it first downloads the latest commit information and branches, then attempts to merge them.)
When pushing over SSH, you need to generate a key and add it to the remote repository.
ssh-keygen -t rsa -C "your email@example.com"
IV. Multi-Device Backup and Synchronization Example
Suppose you have three devices—a laptop, a desktop, and an industrial PC—and want to share the same project1 project among them.
(1) Creating a Repository on GitHub
Click New repository on GitHub, name it project1, and note its HTTPS clone URL.
(2) Laptop: Initialization and First Push
# 进入项目目录
cd ~/projects/project1
# 初始化 Git 仓库
git init
# 关联远程
git remote add origin https://github.com/yourname/project1.git
# 添加所有文件并提交
git add .
git commit -m "Init project structure"
# 推送到 GitHub 主分支
git push -u origin main
(3) Desktop: Cloning and Developing a New Feature
The first time:
# 克隆仓库
git clone https://github.com/yourname/project1.git
cd project1
# 修改代码,完善功能
# ...
git add .
git commit -m "Add Functions"
# 推送分支到远程
git push
Subsequently:
# 拉取代码
git pull
# 修改代码
# ...
# 推送代码
git add .
git commit -m "Add Functions"
git push
(4) Industrial PC: Pulling the Latest Code
The first time:
# 克隆代码
git clone https://github.com/yourname/project1.git
cd project1
# 进行测试
Subsequent use:
# 拉取代码
git pull
# 修改代码
# ...
# 推送代码
git add .
git commit -m "Add Functions"
git push
(5) Branch Management
After the feature has been validated, run the following commands on any one of the computers:
git checkout v0.1.0
git push
This way, you can always use git pull or switch branches on any device to quickly synchronize and restore the project state. Combining branches and tags with a remote repository makes multi-device development both efficient and secure.
Appendix: GitHub Client / Git Tools Integrated into an IDE
Although Git commands can perform many operations, GitHub’s official client or the Git tools integrated into VS Code are better choices for users who are new to Git. They can perform all of the operations above while offering a more intuitive interface.

Comments