25
  • 我在 github 上创建了一个新的存储库。
  • 我选择了添加 README.md 的选项之一。
  • 然后我 cd 进入我硬盘上的项目。
  • 我运行了 git init: Initialized empty Git repository in /Users/myusername/github/myproject/.git/
  • 我跑了“git add”。然后是“git commit -m 'project files'”,它的回应是:

    [master (root-commit) ca52fe0] project files
     981 files changed, 257939 insertions(+), 0 deletions(-)
     create mode 100644 index.php
     create mode 100644 license.txt
     create mode 100644 readme.html
     create mode 100644 wp-activate.php
     ...
    
  • 然后我运行“git remote add origin https://github.com/myusername/myproject.git
  • 然后我跑了“git push origin master”
  • 然后我运行“git status”,它没有说什么要提交

但是我查看 repo 并且我的“我的项目文件”提交不存在。于是我跑了 git pull 得到了这个:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

然后 git push 并再次检查,我的提交仍然不在 github repo 上。我唯一能看到提交的是当我运行“git log”时:

MacBook-myproject myusername$ git log
commit ca52fe090e6dbf1b6aa6ee51c3283efbe7549904
Author: User <myemailaddress>
Date:   Sat Jun 23 19:22:05 2012 -0400
project files

我尽我所能遵循 github 的指示。我究竟做错了什么?

4

3 回答 3

24

在您的 Github 存储库创建之后(即您可以在 Github 上查看它),那么您应该已经拥有:

1. 本地仓库设置:

git init

2. 创建并添加到存储库的 README 文件:

touch README.md
git add README.md 
git commit -m 'first commit'

3.远程调用origin链接到您的存储库:

git remote add origin https://github.com/username/repo.git

4. 初始推送,将您的本地 README 复制到您的 Github 存储库:

git push -u origin master

如果您可以在 Github 上查看您的存储库,则说明它已成功创建。在这种情况下,您可能已经使用在线编辑工具在 Github 上编辑了您的 README 文件,这导致您的远程和本地分支出现分歧。

在您可以push对 Github 进行本地更改之前,您需要fetchpull您的远程更改,在本地合并更改(使用 自动合并pull),然后push到远程。

请参阅 Pro Git:从遥控器中获取和拉取

于 2012-06-24T01:23:24.020 回答
4

当您在 GitHub 上创建存储库时,您选择了包含 README.md 文件的远程初始化。下一步是git clone https://github.com/username/repo.git在您的终端中运行。此时,您在 GitHub 存储库上有一个本地副本,因此您将移入您的项目文件。git add *然后运行。git commit -m 'first commit' _ git push origin master您的更改现在应该在 GitHub 上可见。

于 2012-06-24T07:26:48.123 回答
2

在 git commit 之后,您需要推送更改

git push origin master to push在主分支

git push origin branch_name to push在二级分支

一个分支的完整工作流程:

git checkout -b aosp_in_docker //checkout a branch and switch into it
git branch // to check all branches current branch will have * sign
git status //to check status of file
git add .  //add untraced files
git commit -a -m 'added a docker container'
git push origin aosp_in_docker // push changes
git staus // check if success
于 2018-11-30T11:23:00.407 回答