如何丢弃本地更改并从GitHub存储库中提取最新信息

时间:2016-08-04 20:05:34

标签: git github

我的机器上有一个目录,我存储了GitHub中的所有项目。我打开了其中一个并在我的机器上进行了本地更改。项目搞砸了,现在我想丢弃我所做的所有更改,并从存储库中提取最新版本。我是GitHub的新手,使用Git Shell。

[TestClass()] public class JobTests { private Service service; private LaborTime laborTime; private LaborRates laborRates; [TestInitialize] public void init() { service = new EmergencyService(); } [TestMethod()] // add one hour of service at $75/50 rate public void Job_OnFullCost_Is75() { // Arrange laborTime = new LaborTime( checkIn: new DateTime(year: 2016, month: 7, day: 20, hour: 10, minute: 0, second: 0), checkOut: new DateTime(year: 2016, month: 7, day: 20, hour: 11, minute: 0, second: 0) ); laborRates = new LaborRates(75, 50); service = new Labor(service, laborTime, laborRates); // Act var expected = 75.0M; var actual = service.JobCost; // Assert Assert.AreEqual(expected, actual); } [TestMethod()] // add another hour to the service, at same rate of $175/60 public void Job_OnFullCost_Is125() { // Arrange laborTime = new LaborTime( checkIn: new DateTime(year: 2016, month: 7, day: 20, hour: 12, minute: 0, second: 0), checkOut: new DateTime(year: 2016, month: 7, day: 20, hour: 13, minute: 0, second: 0) ); LaborRates laborRates = new LaborRates(75, 50); service = new Labor(service, laborTime, laborRates); //service.IsContinuation = true; // Act var expected = 125.0M; var actual = service.JobCost; // Assert Assert.AreEqual(expected, actual); } } 命令是否足够? 我是否需要做额外的事情以丢弃在本地进行的更改? 有人可以帮忙吗?

6 个答案:

答案 0 :(得分:63)

git reset是你想要的,但是我要添加一些你可能觉得有用的额外的东西,其他的答案没有提到。

git reset --hard HEAD会将您的更改重置为您本地仓库跟踪的上一次提交。如果您提交了一个提交,但没有将其推送到GitHub,并且想要将其丢弃,请参阅@absiddiqueLive的答案。

git clean -df将丢弃您可能添加的任何新文件或目录,以防您想要丢弃它们。如果您还没有添加任何内容,则无需执行此操作。

git pull(或者如果你在GitHub客户端使用git shell)git sync将从GitHub获得新的更改。

从未来开始编辑: 我在另一周更新了我的git shell,并注意到默认情况下不再定义git sync命令。对于记录,在bash中键入git sync相当于git pull && git push。我发现它仍然有用,所以它在我的bashrc中。

答案 1 :(得分:16)

运行以下命令

git log

从这里你将得到你的最后一次推送提交哈希键

git reset --hard <your commit hash key>

答案 2 :(得分:10)

如果您已经提交了比revert changes更改的内容。

如果您尚未提交,只需进行干净的结帐git checkout .

答案 3 :(得分:3)

除了上述答案外,还有焦土法。

rm -R <folder>
Windows shell中的

命令是:

rd /s <folder>

然后你可以再次签出项目:

git clone -v <repository URL> 

这肯定会删除所有本地更改并从远程存储库中提取最新信息。小心使用rm -R,因为如果你输错了路径,它会删除你的好数据。例如,肯定不会执行:

rm -R /

编辑:修复拼写并增加重点。

答案 4 :(得分:0)

只是补充其他人所说的,我会做以下事情。 如果您已经进行了更改,这意味着您已经在某个时候执行了“git add”, 我会这样做:

git reset HEAD <file_name>

如果您没有声明要更改的文件,就像之前有人提到的那样:

git checkout . 

答案 5 :(得分:-6)

推翻旧回购。 import java.util.*; import java.lang.*; import java.io.*; public class Driver{ public static void main(String[] args) { //initialize variables String filepath; BufferedWriter bw = null; String toRead = ""; CustomList[] arrayForList; CustomList listToBuild; try { System.out.println("To find the determinant of a Matrix, please enter the file below!"); System.out.println("Please enter the file path of the txt file:\n"); //read user input Scanner user_input = new Scanner(System.in); filepath = user_input.next(); //print out the file path for user to confirm the //correct file path was entered System.out.println("Filepath read: " + filepath); System.out.println(""); //finds the spot of the "." in .txt int extCounter = filepath.indexOf('.'); String Output_Path = filepath.substring(0, extCounter); //close the scanner user_input.close(); //Specify the file name and path here //the below code allows the user to enter one path //and get the output file at the same path //without having to enter it twice String OutFile; OutFile = Output_Path.concat("_Output5_File.txt"); File file = new File(OutFile); // This logic will make sure that the file // gets created if it is not present at the // specified location if (!file.exists()) { file.createNewFile(); } //initialize array to hold strings String [] arrayToHoldInts = new String [100]; //sets up filewriter to write output FileWriter fw = new FileWriter(file); bw = new BufferedWriter(fw); // open input stream test.txt for reading purpose. BufferedReader br = new BufferedReader(new FileReader(filepath)); String input = ""; input = br.readLine(); int sizeOfArrayToStore = 0; while (input != null) { //below 2 lines get the size of the matrix sizeOfArrayToStore = Integer.parseInt(input); System.out.println("Size:" + sizeOfArrayToStore); //reads the next line after getting the size input = br.readLine(); //checks for blanks and continues on error if (input.length() == 0){ continue; } String [] stringSplitterForBR = null; arrayForList = new CustomList [sizeOfArrayToStore * sizeOfArrayToStore]; //for loop to add ints parse the string that the //bufferred reader reads in. there is another nested //for loop to add each int that is parsed into a new //node for to build the list for (int i = 0; i < sizeOfArrayToStore; i++){ listToBuild = new CustomList(); stringSplitterForBR = input.split(" "); int tracker = 0; int valueToInsert = 0; //for loop parses the ints and adds them into nodes //from the CustomList class for(int j = 0; j < sizeOfArrayToStore; j++) { valueToInsert = Integer.parseInt(stringSplitterForBR[tracker]); System.out.println("insert " + valueToInsert); listToBuild.addToList(valueToInsert); tracker++; } arrayForList[i] = listToBuild; input = br.readLine(); } //Compute the deterimant using the same formula from //Lab2 int length = arrayForList.length; System.out.println("len: " + length); //print out the results to a .txt file bw.write("Matrix read: "); bw.newLine(); bw.write("------------------" + "---------------------"); bw.newLine(); bw.flush(); int size2 = 0; int valueToPrint; for (int x = 0; x < length; x++){ listToBuild = arrayForList[x]; size2 = listToBuild.sizeOfList(); System.out.println("size2 " + size2); for (int y = 0; y < size2; y++) { valueToPrint = listToBuild.ValueOfNode(y); bw.write(valueToPrint); System.out.println("val" + valueToPrint); bw.flush(); } bw.newLine(); } } bw.close(); } catch (Exception e) { e.printStackTrace(); } } }

我认为git push -u origin master --force也适用于拉动。