GIT:如何从master中删除文件并放入其他分支?

时间:2015-08-26 05:13:41

标签: git

我意外地在master而不是mybranch分支中添加并编辑了文件。现在,即使我切换到mybranch我也无法通过git status看到它。如何在mybranch中添加/提交和重新添加该文件?

3 个答案:

答案 0 :(得分:1)

您可以在mybranch

中重新定位此提交

首先,修复master分支

git checkout master
git branch tmp   // This creates a branch in the "wrong" commit
git reset --hard HEAD~1

您将master分支移回一个提交。你仍然有一个指向你的提交的时间分支。

现在,将提交移至mybranch分支

git checkout tmp
git rebase --onto mybranch master tmp
git checkout mybranch
git merge tmp
git branch -d tmp

rebase命令:

  • 出发点:--onto mybranch
  • 来自什么提交:master
  • 到什么提交:tmp

我建议你打开一些前端,看看你在做什么。我使用gitk

gitk --all &

答案 1 :(得分:0)

在主分支中使用git rm dir1/dir2/file。在正确的分支中使用git show master:dir1/dir2/file >dir1/dir2/filegit add dir1/dir2/file

如果您在错误提交后更新了主分支,当然您无法在master命令中使用git show来恢复文件。您可以使用git reflog查找合适的参考。如果它恰好是HEAD@{2},则命令将为git show HEAD@{2}:dir1/dir2/file >dir1/dir2/file

(git会自动识别它是相同的文件内容,因此您不必担心双磁盘空间消耗。)

答案 2 :(得分:0)

详细答案为here。您可以使用var app = angular.module('TestApp',[]); app.controller('TestController', function($scope) { $scope.nameSearch = {}; $scope.nameSearch.position = 'All'; $scope.positions = ['All','QB','RB','WR','TE','DEF','K']; $scope.players = $scope.allPlayers; $scope.setPosition = function() { $scope.players = []; if ($scope.nameSearch.position != 'All') { for (var i in $scope.allPlayers) if ($scope.allPlayers[i].Position == $scope.nameSearch.position) $scope.players.push($scope.allPlayers[i]); return; } $scope.players = $scope.allPlayers; }; $scope.allPlayers = [ { "Rank":1, "First":"Le'Veon", "Last":"Bell", "Position":"RB", "Team":"PIT", "Bye Week":11 }, { "Rank":2, "First":"Jamaal", "Last":"Charles", "Position":"RB", "Team":"KC", "Bye Week":9 }, { "Rank":3, "First":"Adrian", "Last":"Peterson", "Position":"RB", "Team":"MIN", "Bye Week":5 }... ... .. ...]; $scope.setPosition(); }); rebase -onto。至于我 - 我更喜欢cherry-pick