Based on our progress, here is your consolidated, complete guide to using Git with SiteGround. This covers everything from the initial handshake to daily deployment.
Part 1: Initial Setup (One-Time)
Before you can send code, you must establish a secure connection between your computer and SiteGround using SSH Keys.
1. Generate your Key Pair (on your computer)
Open your terminal (Git Bash or PowerShell) and run:
Bash
ssh-keygen -t rsa -b 4096
- Action: Press Enter for the file path and Enter twice for no passphrase.
- Verification: Run
ls ~/.sshto ensure you seeid_rsaandid_rsa.pub.
2. Add the Key to SiteGround
- In your terminal, copy your public key:
cat ~/.ssh/id_rsa.pub - Log in to SiteGround Site Tools > Dev > SSH Keys Manager.
- Click the Import tab, name it “MyPC”, and paste the code.
- Important: If you have multiple sites, ensure the key is added to the specific site environment you are working on.
Part 2: Creating and Cloning the Repo
1. Enable Git on SiteGround
- Go to Site Tools > Dev > Git.
- Select your website from the dropdown and click Create.
- Once created, click the three dots (kebab menu) next to the repo and select Git Info. Copy the
git cloneURL.
2. Clone to your Local Machine
In your terminal, navigate to where you want the project and run:
Bash
git clone [Paste Your Git Info URL]
- If prompted: Type
yesto confirm the host’s authenticity.
Part 3: Daily Workflow (Syncing & Pushing)
This is the “Loop” you will use whenever you make changes.
1. The “Sync” (Pull First)
Because SiteGround may have automatic updates or file changes, always pull before you push to avoid the “Rejected” error we saw earlier.
Bash
git pull origin master
Tip: If you see the “Merge” screen (Vim editor), press
Esc, type:wq, and hitEnter.
2. The “Commit” (Save Locally)
After you have edited your code:
Bash
git add .
git commit -m "Describe your changes here"
3. The “Deploy” (Push to Live)
先要添加origin master和远端连上线, 在已经git init的文件夹里
git remote add origin ssh://u4666-jkzmava372tg@gsgpm1041.siteground.biz:18765/home/customer/www/aprompt.com.cn/public_html/
然后再输入git remote -v 查看是否成功
This command sends the code to SiteGround and updates the live site immediately.
Bash
git push origin master
Part 4: Troubleshooting Common Errors
| Error Message | Meaning | Fix |
Permission denied (publickey) | SiteGround doesn’t recognize your key. | Ensure id_rsa.pub is imported in Site Tools. |
fatal: 'origin' does not appear... | Your local Git folder lost its link. | Run: git remote add origin [Your Git URL] |
[rejected] master -> master | Server has changes you don’t have. | Run git pull origin master first. |
Best Practices
- Database: Git only tracks files. If you change a WordPress setting or add a post, you must still do that through the WP dashboard or a database migration tool.
- Static Files: Don’t use Git to upload massive images or videos; it will make the repository slow. Use a CDN or FTP for large media.
Would you like me to help you create a .gitignore file to make sure you aren’t accidentally pushing junk files to your SiteGround server?
SiteGround Git Tutorial: How to Use the Git Tool
This video provides a visual walkthrough of the SiteGround Site Tools interface, helping you locate the Git and SSH settings mentioned in this guide.
总结流程:
git init 初始化本地仓库
打开git bash, ssh-keygen -t rsa -b 4096 创建ssh证书,连续双击创建默认证书,
ls ~/.ssh 查看证书,
cat ~/.ssh/id_rsa.pub 展示证书,然后把展示的证书复制一下,粘贴到siteground site-tools的dev面板里的ssh key manager里面去,点击下import然后保存
回到cmd面板
如果有仓库,拉仓库到本地
git pull ssh://u4666-jkzmava372tg@gsgpm1041.siteground.biz:18765/home/customer/www/aprompt.com.cn/public_html/
查看branch名称: git branch
查看git状态 git status
添加远端仓库进行握手: git remote add origin ssh://u4666-jkzmava372tg@gsgpm1041.siteground.biz:18765/home/customer/www/aprompt.com.cn/public_html/
查看握手状态: git remote -v
先新增、修改或者删除文件或者修改任意文件代码之后,
添加git add . 添加所有文件,或者只添加你修改过的文件,比方说git add index.php
然后提交你的修改记录: git commit -m “changed the index.php to fix the bug” 这步必须要做
然后发送文件到云端仓库 git push -u origin master ; -u是upstream意思,就是以后可以直接输入git push就可以传送文件了。 origin 是你和云端仓库握手时候给自己设置的仓库的slug, 就是在这个命令中设置的:git remote add origin https://github.com/YOUR_USERNAME/git-practice.git
查看修改记录 git log –oneline
回滚记录
2. “等等,先别暂存”(取消暂存)
如果你运行了git add,但意识到你还没有准备好提交这些更改,你可以将它们从“暂存区”(暂存区)中取出,放回你的工作目录。
git reset HEAD <文件名>
3. “我需要回到过去”(撤销提交)
如果您已经提交了代码,但发现它有问题,您可以创建一个新的提交,执行与错误完全相反的操作。这样既能保持历史记录完整,又能修复问题。
git revert <提交 ID>
知识点:git clone是复制仓库到本地,如果需要修改需要和云端仓库一样的commit记录,所以需要进行pull, 而不是clone
WP开发教程 WordPress导师在线教程