初始化 Git 仓库并推送到远程
echo "# majiang-html" >> README.md
创建名为 README.md 的文件,并写入初始内容 # majiang-html。
git init
在当前目录初始化一个新的 Git 仓库。
git add README.md
将 README.md 文件添加到暂存区。
git commit -m "first commit"
提交更改到本地仓库,附带提交信息 first commit。
git branch -M main
将默认分支名称从 master 更改为 main(若已存在 master 分支)。
git remote add origin https://github.com/x/majiang-html.git
添加远程仓库地址,命名为 origin。
git push -u origin main
将本地 main 分支推送到远程仓库,并设置上游跟踪关系。
注意事项
确保已安装 Git 并配置用户名和邮箱(通过 git config)。
远程仓库 https://github.com/x/majiang-html.git 需提前在 GitHub 创建。
首次推送可能需要 GitHub 身份验证(如 Personal Access Token)。
后续操作建议
修改文件后重复 git add、git commit 和 git push 流程更新远程仓库。
使用 git status 检查文件状态,git log 查看提交历史。
echo "# majiang-html" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/x/majiang-html.git
git push -u origin main
添加远程仓库并推送代码
git remote add origin https://github.com/x/majiang-html.git
该命令将远程仓库地址添加为本地仓库的远程源,命名为origin。
git branch -M main
将当前分支重命名为main,确保与GitHub默认分支名称一致。
git push -u origin main
将本地main分支推送到远程仓库,并设置上游追踪关系。后续推送可直接使用git push。
常见问题排查
若推送失败,检查远程仓库是否存在且权限正确。
确保本地已存在提交内容(git commit),空仓库无法推送。
网络问题可能导致推送失败,验证网络连接或HTTPS权限。