git - 使用 git 时,如何将确切的工作目录推送到远程?

标签 git

我有一个远程 git 仓库和一个本地克隆。假设我丢失了本地 .git 目录,随后在本地工作目录中添加和删除了一些文件。

在某些时候,我想重新初始化本地存储库,将它连接到远程,并最终将我的本地工作目录完全原封不动地推送到远程(也就是说,我想要添加的所有内容/删除的文件在远程是一样的)

我将如何完成这个?

这是我目前的解决方案,我不喜欢它(并且可能并非在所有情况下都有效)。

初始化

git remote add origin [some_url]

git 添加。 # 添加工作目录中的所有文件

git commit -m "添加文件"

(此时,我目前的想法是:

  • 创建一个分支,

  • 将 Remote 放入其中,

  • 'git diff master branch > my_patch'

  • 将该补丁应用到分支,

  • 从分支推送到远程,

  • pull 进高手,

  • 并杀死分支。)

显然,我的想法非常复杂和丑陋。有什么想法吗?

最佳答案

像这样的事情(假设你是从丢失的 .git 时刻开始的)?

# Make the current directory a git repository.
# This puts you on a master branch with no commits.
git init

# Add a reference to the remote repository.
git remote add origin urlurlurl

# Fetch the remote refs.
git fetch

# Without touching the working tree, move master branch
# from nowhere onto the remote master.
git reset origin/master

# Stage all changes between the new index and the current tree.
git add -A

# Make a commit of these changes.
git commit -m "New working tree as a patch on remote master"

关于git - 使用 git 时,如何将确切的工作目录推送到远程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/471896/

相关文章:

python - 这是什么补丁格式以及如何应用它

git - 无法理解为什么 git merge -s ours 会忽略以前的提交

git - 在 Git 提交期间禁用重命名检测以保留历史记录

git revert merge 当 merge 回删除文件时

每次提交后暂停的 Git rebase

git show --tags > file.txt 缺少标签

git - 清除 git 本地缓存

git - Cygwin 混帐 : The merge tool kdiff3 is not available

Git 认为提交消息是未找到的命令

git - 将功能分支 merge 到主要错误 : You are attempting to modify a pull request based on out-of-date information