git:如何获取最新版本的代码?

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

git: How do I get the latest version of my code?

git

提问by Dave

I'm using Git 1.7.4.1. I want to get the latest version of my code from the repository, but I'm getting errors ...

我正在使用 Git 1.7.4.1。我想从存储库中获取最新版本的代码,但出现错误...

$ git pull
….
M   selenium/ant/build.properties
….
M   selenium/scripts/linux/get_latest_updates.sh
M   selenium/scripts/windows/start-selenium.bat
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>' as appropriate to mark resolution, or use 'git commit -a'.

I've deleted the local copies of the files the tool is complaining about, but I still get the errors. How do I check out the latest version from the remote repo? - Dave

我已经删除了该工具抱怨的文件的本地副本,但我仍然收到错误消息。如何从远程仓库查看最新版本?- 戴夫

回答by Alex Curtis

If you don't care about any local changes (including untracked or generated files or subrepositories which just happen to be here) and just want a copy from the repo:

如果您不关心任何本地更改(包括恰好在此处的未跟踪或生成的文件或子存储库),而只想从 repo 中复制:

git reset --hard HEAD
git clean -xffd
git pull

Again, this will nuke any changes you've made locally so use carefully. Think about rm -Rfwhen doing this.

同样,这会破坏您在本地所做的任何更改,因此请谨慎使用。rm -Rf做这个的时候想想。

回答by tvl

Case 1: Don't care about local changes

案例1:不关心本地变化

  • Solution 1: Get the latest code and reset the code

    git fetch origin
    git reset --hard origin/[tag/branch/commit-id usually: master]
    
  • Solution 2: Delete the folder and clone again :D

    rm -rf [project_folder]
    git clone [remote_repo]
    
  • 解决方案1:获取最新的代码并重置代码

    git fetch origin
    git reset --hard origin/[tag/branch/commit-id usually: master]
    
  • 解决方案 2:删除文件夹并再次克隆 :D

    rm -rf [project_folder]
    git clone [remote_repo]
    

Case 2: Care about local changes

案例 2:关心本地变化

  • Solution 1: no conflicts with new-online version

    git fetch origin
    git status
    

    will report something like:

    Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
    

    Then get the latest version

    git pull
    
  • Solution 2: conflicts with new-online version

    git fetch origin
    git status
    

    will report something like:

    error: Your local changes to the following files would be overwritten by merge:
        file_name
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    Commit your local changes

    git add .
    git commit -m ‘Commit msg'
    

    Try to get the changes (will fail)

    git pull
    

    will report something like:

    Pull is not possible because you have unmerged files.
    Please, fix them up in the work tree, and then use 'git add/rm <file>'
    as appropriate to mark resolution, or use 'git commit -a'.
    

    Open the conflict file and fix the conflict. Then:

    git add .
    git commit -m ‘Fix conflicts'
    git pull
    

    will report something like:

    Already up-to-date.
    
  • 解决方案一:与新上线版本无冲突

    git fetch origin
    git status
    

    将报告如下内容:

    Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
    

    然后获取最新版本

    git pull
    
  • 解决方案二:与新上线版本冲突

    git fetch origin
    git status
    

    将报告如下内容:

    error: Your local changes to the following files would be overwritten by merge:
        file_name
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    提交您的本地更改

    git add .
    git commit -m ‘Commit msg'
    

    尝试获取更改(会失败)

    git pull
    

    将报告如下内容:

    Pull is not possible because you have unmerged files.
    Please, fix them up in the work tree, and then use 'git add/rm <file>'
    as appropriate to mark resolution, or use 'git commit -a'.
    

    打开冲突文件并修复冲突。然后:

    git add .
    git commit -m ‘Fix conflicts'
    git pull
    

    将报告如下内容:

    Already up-to-date.
    

More info: How do I use 'git reset --hard HEAD' to revert to a previous commit?

更多信息: 如何使用 'git reset --hard HEAD' 恢复到以前的提交?

回答by Kzqai

I suspect that what's happened may be that you've deleted the files that you modified (because you didn't care about those changes) and now git is taking the deletion to be a change.

我怀疑发生的事情可能是您删除了您修改的文件(因为您不关心这些更改),现在 git 将删除视为更改。

Here is an approach that moves your changes out of your working copy and into the "stash" (retrievable should it actually turn out that you ever need them again), so you can then pull the latest changes down from the upstream.

这里有一种方法可以将您的更改从您的工作副本移到“藏匿处”(如果事实证明您再次需要它们,则可以检索),这样您就可以从上游拉取最新的更改。

git stash  
git pull

If you ever want to retrieve your files (potential conflicts with upstream changes and all), run a git stash applyto stick those changes on top of your code. That way, you have an "undo" approach.

如果您想检索您的文件(与上游更改和所有更改的潜在冲突),请运行 agit stash apply将这些更改粘贴到您的代码之上。这样,您就有了“撤消”方法。

回答by Amokrane Chentir

You have to merge your files first. Do a git statusto see what are the files that need to be merged (means you need to resolve the conflicts first). Once this is done, do git add file_mergedand do your pullagain.

您必须先合并文件。做一个git status看看需要合并的文件是什么(意味着你需要先解决冲突)。一旦这样做了,做git add file_merged和你做pull一次。

回答by Amirouche Zeggagh

try this code

试试这个代码

cd /go/to/path
git pull origin master

回答by damian

If you just want to throw away everything in your working folder (eg the results of a failed or aborted merge) and revert to a clean previous commit, do a git reset --hard.

如果您只想丢弃工作文件夹中的所有内容(例如失败或中止合并的结果)并恢复到干净的先前提交,请执行git reset --hard.

回答by Dan Ray

I understand you want to trash your local changes and pull down what's on your remote?

我知道您想删除本地更改并拉下遥控器上的内容?

If all else fails, and if you're (quite understandably) scared of "reset", the simplest thing is just to clone origin into a new directory and trash your old one.

如果所有其他方法都失败了,并且如果您(完全可以理解)害怕“重置”,那么最简单的方法就是将 origin 克隆到一个新目录中并丢弃旧目录。

回答by ReCatCoder

By Running this command you'll get the most recent tag that usually is the version of your project:

通过运行此命令,您将获得通常是项目版本的最新标签:

git describe --abbrev=0 --tags

回答by kusma

It sounds to me like you're having core.autocrlf-problems. core.autocrlf=true can give problems like the ones you describe on Windows if CRLF newlines were checked into the repository. Try disabling core.autocrlf for the repository, and perform a hard-reset.

在我看来,您遇到了 core.autocrlf 问题。如果将 CRLF 换行符签入存储库,core.autocrlf=true 可能会出现您在 Windows 上描述的问题。尝试禁用存储库的 core.autocrlf,并执行硬重置。

回答by zar

If you are using Git GUI, first fetch then merge.

如果您使用 Git GUI,请先获取然后合并。

Fetch via Remote menu >> Fetch >> Origin. Merge via Merge menu >> Merge Local.

通过远程菜单获取>>获取>>原点。通过合并菜单>>合并本地合并。

The following dialog appears.

出现以下对话框。

enter image description here

在此处输入图片说明

Select the tracking branch radio button (also by default selected), leave the yellow box empty and press merge and this should update the files.

选择跟踪分支单选按钮(默认情况下也是选中的),将黄色框留空并按合并,这将更新文件。

I had already reverted some local changes before doing these steps since I wanted to discard those anyways so I don't have to eliminate via merge later.

在执行这些步骤之前,我已经恢复了一些本地更改,因为无论如何我都想丢弃这些更改,因此我以后不必通过合并来消除。