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

Ben*_*off 5 git

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

在某些时候,我想重新启动本地仓库,将它连接到远程,并最终将我的本地工作目录完全按原样推送到远程(也就是说,我希望拥有所有添加/删除的文件在遥控器上是一样的)

我怎么做到这一点?

这是我目前的解决方案,我不喜欢(并且可能不适用于所有情况).

git init

git remote add origin [some_url]

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

git commit -m"添加文件"

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

  • 做一个分支,

  • 将遥控器取入其中,

  • 'git diff master branch> my_patch'

  • 将该补丁应用于分支,

  • 从分支推送到远程,

  • 拉进主人,

  • 并杀死分支.)

很明显,我的想法非常复杂和丑陋.有任何想法吗?

CB *_*ley 9

这样的事情(假设你是从失落的.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"
Run Code Online (Sandbox Code Playgroud)