将提交的 git 文件移动到另一个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10883931/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:00:56  来源:igfitidea点击:

Moving committed git files to another directory

gitcommitreset

提问by hybrid9

I've been doing development work in a 'dev-test' directory and committed files along the way. I now need to commit those same files to a differently named directory and remove the committed files in the 'dev-test' directory. I'm not sure how to retain my working files.

我一直在“dev-test”目录中进行开发工作,并在此过程中提交了文件。我现在需要将这些相同的文件提交到不同命名的目录,并删除“dev-test”目录中提交的文件。我不确定如何保留我的工作文件。

My 'dev-test' repo is local, but I have been doing pulls that are tracking as 'master', but I have not merged my code.

我的“开发测试”存储库是本地的,但我一直在做以“主”身份跟踪的拉取,但我还没有合并我的代码。

Can I use git reset before my first commit of these files as other developers have been merging into 'master':

我可以在我第一次提交这些文件之前使用 git reset 吗,因为其他开发人员已经合并到“master”中了:

git reset --soft #commitID

回答by Shahbaz

Your question is still a bit unclear, but you probably want one of these:

您的问题仍然有点不清楚,但您可能想要以下之一:

  1. Just move the files if they aren't modifications to some other files

    $ git mv dev-test/files other-dir
    $ git commit -a
    
  2. If your test files are modifications to other files, you would want to get a diffof your work from when you started it until now and apply the patch to the other directory.

  1. 如果文件不是对其他文件的修改,只需移动文件

    $ git mv dev-test/files other-dir
    $ git commit -a
    
  2. 如果您的测试文件是对其他文件的修改,您可能希望获得diff从启动它到现在的工作并将补丁应用到其他目录。

In case what you wanted was the second, you should have in fact done a different thing from the start. If you want to test something and then later apply it to master, you should create a branch, work on it and if you are satisfied, merge master into your branch. If everything was fine, then you can merge everything back to master and have everyone know about it.

如果您想要的是第二个,您实际上应该从一开始就做不同的事情。如果你想测试一些东西,然后将它应用到 master,你应该创建一个分支,处理它,如果你满意,将 master 合并到你的分支中。如果一切正常,那么您可以将所有内容合并回 master 并让每个人都知道它。

回答by Zuha Karim

To move a file from one directory to another you can use git mvcommand. For example you have 2 directories a.draft, b.pageson your git repository (gitrepo is the name in my case) . You want to move a file abc.md from draft to pages make sure you're in yor gitrepo use cdto go to your gitrepo ( you can check status by $git statusas well) and write :

要将文件从一个目录移动到另一个目录,您可以使用git mv命令。例如,您的 git 存储库中有 2 个目录 a.draftb.pages(在我的情况下 gitrepo 是名称)。您想将文件 abc.md 从草稿移动到页面,请确保您在 gitrepo 中,使用cd转到您的 gitrepo(您也可以通过$git status检查状态)并写入:

[zuha@localhost gitrepo] $ git mv draft/abc.md pages

It will show something like

它会显示类似

[zuha@localhost gitrepo] $ renamed: draft/abc.md -> pages/single-batch-ex.md~

Now commit your file

现在提交你的文件

 [zuha@localhost gitrepo] $ git commit -m "first file"

Enter your username and password, and push it .

输入您的用户名和密码,然后推送。

 [zuha@localhost gitrepo]  $ git push origin (your-branch)