以下是一个完整的示例,从初始化到推送代码:
cd /path/to/your/project
git init
git remote add origin https://github.com/yourusername/yourrepository.git
git add .
git config user.name "Your Name"
git config user.email "you@example.com"
git commit -m "Initial commit"
git push origin master
-
git init
初始化一个新的 Git 仓库。在当前目录中创建一个.git
文件夹,开启版本控制。 -
git remote add origin https://github.com/yourusername/yourrepository.git
将远程仓库添加为origin
,其中origin
是默认的远程仓库名称,这里设置的仓库地址是 GitHub 上的一个仓库。 -
git add .
将当前目录下的所有文件(包括子目录中的文件)添加到暂存区,为提交做好准备。 -
git config user.name "Your Name"
和git config user.email "you@example.com"
配置提交时的用户信息,user.name
是你的用户名,user.email
是你的邮箱。提交信息会记录这些内容,以识别提交者。 -
git commit -m "Initial commit"
创建一次提交,并附上提交信息"Initial commit"
。这次提交包含了暂存区中的所有文件和修改。 -
git push origin master
将本地的master
分支推送到远程仓库的master
分支上。执行该命令后,代码会被上传到 GitHub 上的远程仓库。
常见问题
#认证失败:确保你有正确的访问权限。如果使用SSH密钥认证,请确保你的SSH密钥已添加到你的Git服务(例如GitHub)的账户中。
#远程分支不存在:如果远程仓库中没有主分支,你可能需要创建一个新分支:
git push origin your-branch-name