Contents
  1. I. Introduction
  2. II. Approach
  3. III. Common Commands
  4. IV. Multi-Device Backup and Synchronization Example
  5. Appendix: GitHub Client / Git Tools Integrated into an IDE

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:

  1. Create a remote repository on GitHub, such as my-project.
  2. Run git clone on each device, such as a laptop, desktop, or industrial PC, to clone the same repository.
  3. Before finishing work each day, use git push to push changes to GitHub.
  4. Create a separate branch as a backup for every major change, such as a new feature, refactoring, or bug fix.
  5. Before debugging on another device, enter the project directory and run git pull, or run git fetch && git checkout for a specific branch, to quickly obtain the latest code.

III. Common Commands

CategoryScenarioExample command
Repository initializationCreate a local repository for a new project for the first timegit init
Clone a remote repositoryRetrieve an existing project on a new device or in a new directorygit clone https://github.com/用户名/my-project.git
Configure identityRequired when using Git for the first timebash<br>git config --global user.name "Your Name"<br>git config --global user.email "you@example.com"<br>
Check statusSee which files have changed and which are in the staging areagit status
Add to the staging areaAdd new or modified files to the next commitgit add filename or git add .
Commit changesSave a snapshot of the current staging areagit commit -m "修复登录接口参数校验"
Create a branchCreate a local branch and switch to itgit checkout -b debug
View branchesList all local branchesgit branch -v
Switch branchesSwitch to an existing branchgit checkout release
Merge branchesMerge changes from a branch into the current branchbash<br>git checkout main<br>git merge debug<br>
Resolve conflictsEdit the conflicting file after a merge conflict and mark it as resolvedEdit the conflicting file → git add filenamegit commit
Manage tagsMark an important commitgit tag v1.0.0
Delete tagsDelete a local or remote tagbash<br>git tag -d v1.0.0 # 本地<br>git push origin :refs/tags/v1.0.0 # 远程<br>
Add a remote repositoryAssociate the local repository with GitHubgit remote add origin https://github.com/用户名/my-project.git
Push to a remoteUpload local branch commits to GitHubgit push origin main
Pull remote updatesRetrieve and merge the latest commits from the remote repositorygit pull origin main
Restore a fileDiscard uncommitted changes in the working treegit restore filename
Roll back a versionReturn to the previous version after committingbash<br>git log --oneline # 找到提交哈希id<br>git reset --hard <commit-id> # 回退且丢弃后续改动<br>

(1) Creating a Repository

ScenarioExample command
Create a local repositorygit init
Retrieve an existing projectgit clone https://github.com/用户名/my-project.git
Configure Git when using it for the first timegit config --global user.name "Your Name"
git config --global user.email "you@example.com"

(2) File Operations

ScenarioExample command
Check the project statusgit status
Add changes to the next commitgit add filename or git add .
Commit changes to the staging areagit commit -m "修复登录接口参数校验"

(3) Restoring Files

ScenarioExample command
Discard uncommitted changes in the working treegit restore filename
Return to the previous version after committinggit 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

ScenarioExample command
Create a local branch and switch to itgit checkout -b debug
List all local branchesgit branch -v
Switch to an existing branchgit checkout release
Merge changes from a branch into the current branchbash<br>git checkout main<br>git merge debug<br>
Edit the conflicting file after a merge conflict and mark it as resolvedEdit the conflicting file → git add filenamegit commit

(5) Tag Commands

ScenarioExample command
Mark an important commitgit tag "标签名"
Delete a local or remote taggit tag -d "标签名" # 本地
git push origin :refs/tags/标签名 # 远程<br>

(6) Adding a Remote Repository

ScenarioExample command
Associate the local repository with GitHubgit remote add origin https://github.com/用户名/my-project.git
Upload local branch commits to GitHubgit push
Retrieve and merge the latest commits from the remote repositorygit 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.