What is the TortoiseGit equivalent of git checkout <commit> <path>? - tortoisegit

git checkout has a mode that is very different from its normal operation. When called as git checkout <commit> <path>, for example
git checkout 1234abcd .
it will not change HEAD at all but instead it will load the file set from <commit> and replace the working copy with it so that the files can be committed as new changes.
Is there a way I can trigger the same operation using the TortoiseGit dialogs?

If you exactly know the revision and need specific files, open the repository browser (it can also be opened from the log dialog), select the file(s) there and then either drag'n'drop it to where you need it or use the context menu to revert the files in place.
If I want to reset my working tree files to the state of a specific version, I usually use the Log Dialog and perform two resets: First a hard reset to the revision and then a mixed or soft reset back to the "old" revision I came from.

Related

Switch / Checkout from Git Synchronization dialog

I need to do a lot of switching between branches from a remote repository to inspect code, perform code reviews, contribute to other developers' work and such. I'm not a command line fan so I use TortoiseGit almost exclusively. I also prefer TG over Git clients in various IDEs because I find it really shines when you run into more complex operations like non-trivial three-way merges.
I noticed I find myself clicking A LOT, though. To switch to another branch consists of: Revert or stash any current local changes, fetch (to get the latest remote branches), switch/checkout. So I started looking into the "Git Synchronization" dialog, hoping that I could perform all those operations from within one dialog with a few less clicks, dialogs and OKs.
However, I simply cannot find the switch (or checkout) functionality in that dialog. Is that simply missing, or is there some other Git operation or combination of operations that accomplishes the same?
Quick solution
You can simply select a different branch from Local Branch dropdown in Git Sync dialog. When you subsequently invoke Pull, TortoiseGit will force the checkout automatically:
For other operations (Fetch, Push) switching to a branch is not necessary. However be cautious with the Commit and Show log buttons as they will always apply to the current branch (not the branch selected in Local Branch dropdown) and no checkout will be forced.
Solution using Switch/Checkout dialog
If you need to use full Switch/Checkout dialog TortoiseGit dialog, you can use Browse references dialog which you can invoke from Git Sync dialog by clicking on the ellipsis button next to the Local Branch dropdown.
In the Browse references dialog there is a list of available branches. You should right click the desired branch you want to checkout and select Switch/Checkout to this from context menu. Switch/Checkout dialog will pop up. After completing the checkout, return to the Browse references dialog where the desired branch is selected and click OK. Now the desired branch is checked out and also selected as a Local Branch in Git Sync dialog.

How can I cancel a Merge in TortoiseGit?

I must be missing something.
I am a git Newbie. I unintentionally created a Branch when I did a Switch/Checkout on a single file (reverting to a previous version).
I then tried to Merge that right branch. But files were in Conflict, so I cancelled.
Now, everytime I try Commit or Switch I get an error that "did not exit cleanly" but when I try to Commit I get the Conflicted Merge.
How can I cancel the request for the Merge?
You have two choices:
Go to the log dialog and perform a hard reset (on the latest revision of your branch)
Open the context menu in explorer and select "Abort merge" which basically does the same.
Both options will reset all files in the working tree and the index "back" to the latest version of your current branch.
Update: Starting with TortoiseGit 2.4.5 you can also select "git reset --merge" in the "Abort merge" dialog. This option resets the index and tries to restore the pre-merge state of the repository.

Can you do partial commit in TortoiseGit?

Native git allows partial commits: You can commit only some of the lines of your file and leave the rest for a later commit.
Is this possible in TortoiseGit?
Yes, there are official instructions in the docs.
In practice, I find this workflow useful:
Right click a file you want to partially commit
Click Restore after commit - This immediately creates an internal copy of the file.
Double click the file to edit in TortoiseGitMerge
Right click -> Mark this block for each change you want to commit now
Right click -> Leave only marked blocks to revert the other changes.
As an alternative to these two steps, you can edit the file the way you want.
Save and Close TortoiseGitMerge
Commit - this restores the internal copy of the file afterwards.
The changes you just reverted in TortoiseGitMerge are now restored in your working tree.
Update:
Starting with TortoiseGit 2.13 TortoiseGit comes with a partial staging feature in the commit dialog.
You need to enable the partial staging feature on the commit dialog, then the can open the patch view on the right side and select lines/hunks to stage/unstage.
The answer https://stackoverflow.com/a/32527098/3906760 is basically correct, however there are fewer steps required.
Choose Restore after commit in the context menu of the file you want to partially commit - This immediately creates an internal copy of the current state of the file.
Now adjust the file to only contain the changes you want to commit
This can be done using any editor you want to use, but also using TortoiseGitMerge:
Double click the file to edit in TortoiseGitMerge (or your configured diff tool)
Adjust the file to only contain the changes you want to commit.
Save and Close
Commit - this commits the selected files and restores the internal copy of the file - the original state of the file is now in your working tree again.
These steps are required as TortoiseGit does not yet support the full staging process and hunk/block selection, cf. https://tortoisegit.org/issue/2299.

How do I revert back to earlier commit using source tree

I have a project stored on my Mac and created a repository for from that location to my project. I am able to commit using source tree but now I would like to revert back to an earlier commit so all my files in folder update? I'm not sure how i do this?
Temporary Revert Back
If you want to temporarily go back to this commit, then come back to where you are.
Right click on the commit you want to revert to and click on "checkout".
Permanent Revert Back
This will add commits to revert all your work since this commit
Right click on the commit you want to revert to and click on "Reset <> to this commit".
I think in above discussion #dominones and #Giraldi maybe misunderstand each other,
#Giraldi maybe want to delete those unwanted commits permanently in the GIT, and make the git go back to a certain history commit, from there he could start all over again, like you want to be fresh from the beginning of somewhere.
while #dmonones is showing just how to revert back to a status of your code in a history, discarding some code changes you did, which will mostly end up with a new commit aiming to remove a unwanted/mistaken change,
without changing the git history.
To discard an already committed commit in git history, you need to use hard reset and push with force.

Git (TortoiseGit) - How to revert a single file to a previous revision and then undo the revert?

When using Git with TortoiseGit: Does somebody know how to revert a single file(or a complete repository) to a previous revision?
For example I have a repository containing multiple files. One file exists in three revisions (1 ; 2 ; 3). Now I want to change from revision 3 back to 2.
TortoiseGit offers a "Revert" function in the "Show log" dialog which allows to jump back to a specific revision, but this will revert your whole repository instead of a single file.
Also once I have reverted something, I don't have a clue how to undo the revert and jump back to the newest revision.
From the command line: git checkout is probably what you want.
The documentation shows an example of:
$ git checkout master~2 Makefile
to revert Makefile to two revisions back in the master branch
From within TortoiseGit (via Windows Explorer) it looks like you can do this with the following steps:
Navigate in Explorer to the folder where the file is.
Right-click on the file you want to revert, choose Show log from the TortoiseGit context menu
In the top section ("graph") select the revision that has the version of the file you want to revert to
In the third section (file list) right-click the file and choose Revert to this revision
You should get a message like 1 files revert to e19a77
It is also possible to get a specific file:
Right-click on the file
Choose the menu item: Git Show Log
TortoiseGit show a dialog with a list of all revisions
Click on the prior revision (2)
In a list in the bottom of the dialog it shows all the files associated with the commit
Find the file you are looking to view
Right-click on that file and Save Revision To
This will save that previous revision of the file with the commit number concatenated on the file
For example:
- original file is file.txt
- revision 2 will save as file-67b51a8.txt
I'm using TortoiseGit v2.2.0.0
In TortoiseGit the answer is to right-click the project folder and click Show Log, then right-click the change you want to roll back to and click Switch/Checkout to this... . It will let you then proceed from that weird place in the commit stack, or branch in case you plan to commit and want things to stay sane.
Updated my answer, based on these comments:
by Alexander.
by mlibby
by Chris Moschini
Suppose the working tree is clean and you want:
Checkout some file(s) of its previous revision
Testing
Revert to current revision
Checkout some file(s) of its previous revision
(a) Right click the file you want to revert and Show Log for that file
(b) Right click the file in file list and perform Revert to parent revision
(c) repeat (a), (b) until you get all files you want.
Testing
Revert to current revision
(a) perform Revert... in context menu of explorer
This way, you can choose the file(s) you want to revert.
(b) or this quick way: perform Reset Hard in Log dialog
This way, all changed files revert. (=> Lost all working dir changes)
(Tested on TortoiseGit 1.8.16.1, GfW 2.6.4.windows.1, Win 10 64bit)
For Tortoise Version 2.3.0:
You can do it in several ways, but fastest i think is that:
Right click on the file
Click the menu option "Diff with previous version"
Right click on the left (previous) version of file
Click the menu option "Use this whole file"
Save the changes (ctrl+s)

Resources