git - 同步到远程仓库时git push的问题

标签 git powershell cmd azure-devops

我遇到一个奇怪的问题,文件同步到我的远程仓库(TFS 2015 Git到VSTS GIT)。

我有报告1是包含“Hello”的“sample.txt”。
我在cmd中使用以下命令:

git clone 
git remote add vsts PAT 
git checkout master 
git push vsts master

当我将文件从“Hello”修改为“Hello 1”->执行相同的CMD时,它没有问题,并且可以在远程存储库中看到所有更改。

现在,我在Powershell中使用了相同的CMD
if ( $(git remote) -contains 'vsts' )
{
  git remote rm vsts 2>&1|Write-Host
  echo 'VSTS Account removed'
}
git remote add vsts https://Personal%20Access%20Token:TOKEN@my.visualstudio.com/teamproject/_git/repo 2>&1|Write-Host
git checkout ${env:BUILD_SOURCEBRANCHNAME} 2>&1|Write-Host
git push  vsts ${env:BUILD_SOURCEBRANCHNAME} -f 2>&1|Write-Host

构建成功,但不会同步我的更改。

我经常看到错误:
Previous HEAD position was eb5c087... Updated test.ps1
Your branch is behind 'origin/master' by 9 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
Switched to branch 'master'

有时,相同的CMD来自cmd,而不适用于Powershell。

请问有什么帮助吗?

最佳答案

此问题是由于导致的,TFS2015在获取源代码步骤期间未使用master自动更新origin/master分支。因此,您需要通过添加命令master来使用origin/master分支更新本地git reset --hard origin/master分支。

更新的powershell脚本如下所示:

if ( $(git remote) -contains 'vsts' )
{
    git remote rm vsts 2>&1|Write-Host
    echo 'VSTS Account removed'
}

git remote add vsts https://Personal%20Access%20Token:TOKEN@my.visualstudio.com/teamproject/_git/repo 2>&1|Write-Host

git checkout ${env:BUILD_SOURCEBRANCHNAME} 2>&1|Write-Host
git reset --hard origin/master 2>&1|Write-Host
echo 'update local branch with remote successfully'
git push  vsts ${env:BUILD_SOURCEBRANCHNAME} -f 2>&1|Write-Host

关于git - 同步到远程仓库时git push的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073587/

相关文章:

ruby-on-rails - 带有 cmd 固定编码的 Rspec

git - 修复不完整的.git目录

git - 禁用 git EOL 转换

git - 终端 Git 跟踪与 VS Code git 跟踪不同步

windows - 查找值不属于另一个数组 PowerShell 的数组元素

powershell - 将以空格分隔的 PowerShell 输出解析为表格

git - 在 git remotes 中找不到远程产品

powershell - 将UTF-8转换为ANSI

java - gradle windows java.io.IOException : CreateProcess error=206, 文件名太长

windows - "Access is denied"带 NUL > index.html 命令