如何撤消Git中的最后一次本地提交并返回远程状态?

时间:2017-05-05 09:12:29

标签: git merge git-remote revert

我将一个远程分支合并到我的本地仓库中,它创建了26个本地提交。

现在,我想恢复这个合并,但是它认为将提交1还原为1或者寻找远程版本的最后一次提交以恢复它是非常无聊和错误敏感的。

我浏览了帖子How to undo last commit(s) in Git?,这个帖子的答案非常有趣,但我没有找到任何简单的方法来进行本地恢复。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

使用Git Log& Git重置

    override func viewDidLoad()
{
    super.viewDidLoad()

    nameArray.removeAll(keepingCapacity: false)
    addressArray.removeAll(keepingCapacity: false)


    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Reloading")
    refresher.addTarget(self, action: #selector(downloadJsonWithURL), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresher)
    self.downloadJsonWithURL()

}


func downloadJsonWithURL()
{
    // Emtpy arrays to avoid index out of range crash
    nameArray.removeAll(keepingCapacity: false)
    addressArray.removeAll(keepingCapacity: false)

    let url = NSURL(string: urlString)

    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in


        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {

            if let projectArray = jsonObj!.value(forKey: "project") as? NSArray {
                for project in projectArray{
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "societe") {
                            self.nameArray.append(name as! String)
                        }
                    }
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "address") {
                            self.addressArray.append(name as! String)
                        }
                    }
                }
            }

            OperationQueue.main.addOperation(
                {
                    self.tableView.reloadData()
                    self.refresher.endRefreshing()
            })
        }
    }).resume()

}

// Count length of array and create number of cells accordingly
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return nameArray.count
}

// Create cell data from arrays
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.projectDesc.text = nameArray[indexPath.row].uppercased()
    cell.projectAddress.text = addressArray[indexPath.row]
    return cell
}

这将为您提供带有消息的提交ID,通过该消息可以识别您的提交ID。

使用提交ID然后重置。

git log

如果你永远想要回到那个提交。你可以使用以下内容。

git reset <commit-id>

答案 1 :(得分:0)

我尝试了一两件事,这件事有点脏,但它很简单,似乎有效:

  • 结帐到另一个分支(如果没有,则创建一个):git checkout -b tempo
  • 删除错误的本地分支:git branch -D dirty_one
  • 重新创建与远程分支同步的本地分支:git checkout -b dirty_one origin/dirty_one

那就是全部。

我认为肮脏的事情是本地提交仍然存在,但分支机构并不知道它们,所以它们可能仍然隐藏起来。

如果我重做我的合并(事实上我已经完成),合并效果很好,但我不确切知道(脏的部分......)