Git 基本工作流程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2670680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git basic workflow
提问by bsr
Possible Duplicate:
git push error '[remote rejected] master -> master (branch is currently checked out)'
I am new to Git and trying to use it for a local grails project.
The steps I followed:
我是 Git 的新手,并试图将它用于本地 grails 项目。
我遵循的步骤:
- create the grails project
- go to the project directory and
git init
- Add all the files in the project in staging area and commit.
The git status at the repo gives the below message
BXX@BXX-PC /c/Work/Grails/projects/yyy/tables (master) $ git status # On branch master nothing to commit (working directory clean)
Trying to keep it as the master branch, make the changes by cloning the repo, and later push the changes back. For that
- In my IDE, checkout the project (IntelliJ).This actually clone the project to another dir.
- Make the changes and commit the project
Push the local changes to master.
15:41:56.249: git push -v origin master Pushing to c:/Work/Grails/projects/xxx/tables remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD.
- 创建 grails 项目
- 转到项目目录和
git init
- 将项目中的所有文件添加到暂存区并提交。
仓库中的 git status 给出了以下消息
BXX@BXX-PC /c/Work/Grails/projects/yyy/tables (master) $ git status # On branch master nothing to commit (working directory clean)
尝试将其保留为主分支,通过克隆 repo 进行更改,然后将更改推回。为了那个原因
- 在我的 IDE 中,检查项目 (IntelliJ)。这实际上是将项目克隆到另一个目录。
- 进行更改并提交项目
将本地更改推送到 master。
15:41:56.249: git push -v origin master Pushing to c:/Work/Grails/projects/xxx/tables remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD.
The cloned repo status is
克隆的回购状态是
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
Please help me with understanding this. Is there a better workflow to follow. I may be able to initialize the repo through Intellij, and try to work on the main branch. Still not sure what is wrong above.
请帮助我理解这一点。是否有更好的工作流程可以遵循。我也许可以通过 Intellij 初始化 repo,并尝试在主分支上工作。仍然不确定上面有什么问题。
thank you.
谢谢你。
回答by mipadi
The problem is that you're trying to push to a non-bare repo. A non-bare repo is one which has an associated working tree (that is, the files are actually checked out to disk). By default, Git won't let you push to a non-bare repo; pushing to a non-bare repo only updates Git's internal data structures, and does notalter the working tree (the files on disk), which means that if you then go back to the repo you pushed to and start working on the files, you'll be working on older copies of the files. Naturally this will cause problems when you try to commit your changes.
问题是您正在尝试推送到非裸存储库。非裸存储库是具有关联工作树的存储库(即,文件实际上已签出到磁盘)。默认情况下,Git 不会让你推送到非裸仓库;推到非裸露的回购仅更新Git的内部数据结构,并不会不改变工作树(磁盘上的文件),这意味着,如果你再回去给你推到了回购,并开始对文件的工作,你将处理文件的旧副本。当您尝试提交更改时,这自然会导致问题。
The best way to do this is to push to a barerepository, which is one created by passing the --bare
flag to Git when creating the repo:
最好的方法是推送到一个裸仓库,它是通过在创建仓库--bare
时将标志传递给 Git 来创建的:
$ mkdir new_repo
$ cd new_repo
$ git --bare init
Of course, the bare repo won't have any files checked out, so you can't actually work it in (you'll have to clone it first).
当然,裸仓库不会检出任何文件,因此您实际上无法使用它(您必须先克隆它)。
If you're just using a Git repo for local development (and not sharing or serving the Git repo), you don't haveto have a remote repo to push to; you can work on a single copy of a local, non-bare repo.
如果你仅仅使用一个Git回购当地发展(而不是共享或服务Git的回购),你不具备有远程回购推到; 您可以处理本地非裸存储库的单个副本。
回答by Sergey Kuznetsov
First of all, you do not need to clone your local repository. You can use branches to divide development in the same repository.
首先,您不需要克隆本地存储库。您可以使用分支在同一存储库中划分开发。
Git is a Distributed VCS, and if you have experience with Subversion or CVS before it, you need to change your mind.
Git 是一个分布式 VCS,如果您之前有使用 Subversion 或 CVS 的经验,您需要改变主意。
Git is a very agile and you can use different workflows with it. Cloning repositories is more needed for a team work, not for local development (IMHO).
Git 是一个非常灵活的工具,您可以使用它来使用不同的工作流程。团队工作更需要克隆存储库,而不是本地开发(恕我直言)。
Branches is a good alternative for you.
Branches 是一个不错的选择。
See. Let's set masterbranch of your repository for a production-readycode. Let's create another branch for development:
看。让我们为生产就绪代码设置存储库的主分支。让我们为开发创建另一个分支:
$ git checkout -b development master
Now you are working on the developmentbranch.
现在您正在开发分支上工作。
You could use different branches for each feature you want to develop. It's very easy and helpful.
您可以为要开发的每个功能使用不同的分支。这非常简单和有帮助。
Let's imagine you want to implement some new feature, you need to create a new branch:
假设您要实现一些新功能,需要创建一个新分支:
$ git checkout -b newfeature development
Now you can work with your code, add files, commit and so on.
现在您可以使用您的代码、添加文件、提交等等。
Next you need to mergeyour new developed feature to the developmentbranch:
接下来,您需要将新开发的功能合并到开发分支:
$ git add .
$ git commit -m "My last changes for the new feature"
$ git checkout development
$ git merge newfeature
Now your new code from the newfeaturebranch merged into the developmentbranch.
现在你来自newfeature分支的新代码合并到了development分支。
For sometime in the future when you decide that your code in the developmentbranch get some milestone, you could merge all the changes from developmentto masterbranch.
将来某个时候,当您决定开发分支中的代码获得某个里程碑时,您可以将所有更改从开发分支合并到主分支。
It's a very basic workflow, it can be helpful for the many branches.
这是一个非常基本的工作流程,它对许多分支都有帮助。
My advise for you now: read more about git, branching, stashing (very-very-very helpful for quick fixes). And after some time you will get a great effort from using git.
我现在给你的建议是:阅读更多关于 git、分支、存储的信息(对快速修复非常有帮助)。一段时间后,您将通过使用 git 获得巨大的努力。
Good luck.
祝你好运。
回答by Jason Watts
This is the clearest, most comprehensive description of a successful git workflow. It covers basically what Sergey suggests, with the addition of some very hepful graphics.
这是对成功的 git 工作流程最清晰、最全面的描述。它基本上涵盖了 Sergey 建议的内容,并添加了一些非常生动的图形。
A successful Git branching model
The author also suggests when you merge to include the --no-ff
tag to keep record of the fact that you had a feature branch in history.
作者还建议您在合并时包含--no-ff
标签,以记录您在历史上有一个功能分支的事实。
回答by John Munsch
I ended up here when trying to fix the same kind of problem, fortunately there is a much better answer out there:
当我试图解决同样的问题时,我最终来到这里,幸运的是那里有一个更好的答案:
Git push error '[remote rejected] master -> master (branch is currently checked out)'
Git 推送错误“[远程拒绝] master -> master(分支当前已检出)”
You should check that one out. Especially as it is pretty easy to end up in this circumstance. For me all I did was create a directory and git init it to make my new "shared repository". It was on a USB key because our network is completely locked down and we can't share a directory or access GitHub yet.
你应该检查一下。尤其是在这种情况下很容易结束。对我来说,我所做的只是创建一个目录并 git init 来创建我的新“共享存储库”。它位于 USB 密钥上,因为我们的网络已完全锁定,我们还无法共享目录或访问 GitHub。
Then I copied all of our source into that directory, added it, committed it, and then cloned the resulting repository to my local drive so that one was now my origin. I figured I could later make GitHub remote and do away with the shared USB repository. However, my first change on the local drive and attempt to push to the remote (the key repository) gave me the message because the key repository wasn't "bare". All of the original files were out there still.
然后我将所有源代码复制到该目录中,添加它,提交它,然后将生成的存储库克隆到我的本地驱动器,这样一个现在就是我的源。我想我以后可以使 GitHub 远程化并取消共享的 USB 存储库。但是,我在本地驱动器上的第一次更改并尝试推送到远程(密钥存储库)给了我消息,因为密钥存储库不是“裸机”。所有原始文件仍然存在。
Consult the most highly rated answer on the linked to question to see what to do. The gist is that you set a flag in the shared repository so it thinks it is bare and then delete everything out of it except the .git subdirectory and your push will then work.
参考链接到问题的评分最高的答案,看看该怎么做。要点是您在共享存储库中设置了一个标志,因此它认为它是空的,然后删除其中除 .git 子目录之外的所有内容,然后您的推送将起作用。