git-checkout older revision of a file under a new name

ID : 10190

viewed : 16

Tags : gitgit

Top 5 Answer for git-checkout older revision of a file under a new name

vote vote

90

You can use git show for that:

git show HEAD^:main.cpp > old_main.cpp 

(Note that there is colon [:] character between HEAD^ and main.cpp.) The <revision>:<path> syntax is described in git rev-parse manpage, next to last point in the "Specifying revisions" section:

<rev>:<path>, e.g. HEAD:README, :README, master:./README

A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon. :path (with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path. A path starting with ./ or ../ is relative to the current working directory. The given path will be converted to be relative to the working tree’s root directory. This is most useful to address a blob or tree from a commit or tree that has the same tree structure as the working tree.

Note that <path> here is FULL path relative to the top directory of your project, i.e. the directory with .git/ directory. (Or, to be more exact, to "<revision>", which in general can be any <tree-ish>, i.e. something that represents tree.)

If you want to use path relative to the current directory, you need to use ./<path> syntax (or ../path to go up from current directory).

Edit 2015-01-15: added information about relative path syntax


You can get in most cases the same output using low-level (plumbing) git cat-file command:

git cat-file blob HEAD^:main.cpp > old_main.cpp 
vote vote

88

Just to add to Jakub's answer: you don't even have to redirect the output to a file with >, if you are only interested in skimming the file contents in the terminal. You can just run $ git show 58a3db6:path/to/your/file.txt.

vote vote

73

Singular use case

In order to get consistent checkout behavior including autocrlf etc., use a secondary folder (TEMP as example) and restore the file state from an older / different like this:

git --work-tree TEMP/ restore -s <commit> main.cpp mv TEMP/main.cpp old_main.cpp 

Note:

git show <commit>:main.cpp > old_main.cpp 

.. will just produce a raw read from the repository.

Use a 2nd work tree - anonymous or linked

For more comfort working with alternative file states (without the need for renaming) use a secondary "anonymous" directory as a (long term) parallel work tree like this:

# one time setup mkdir WD2 cd WD2 echo gitdir: WD1 > .git  # operate freely here with an alternative file set while the same branch is checked out in git git restore -s <commit> main.cpp 

(WD2 : path to secondary dir. WD1 : path to primary dir - Absolute or relative)

Note: Any kind of git project work (restore / add / commit / switch ..., build tasks) can be done with that alternative file set - as if suddenly the work tree files in WD1 would have been swapped. Hard links could be used for efficiently mirroring unchanged files in huge repos.

Similarly a "linked working tree" can be used via git-worktree (new since git v2.6.7). This causes somewhat more git overhead, but allows to checkout and work on a different branch (or detached head) simultaneously without using an extra repo.

vote vote

60

vote vote

54

I had same exception in a custom binding scenario. Anybody using this approach, can check this too.

I was actually adding the service reference from a local WSDL file. It got added successfully and required custom binding was added to config file. However, the actual service was https; not http. So I changed the httpTransport elemet as httpsTransport. This fixed the problem

<system.serviceModel> <bindings>    <customBinding>     <binding name="MyBindingConfig">        <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"         messageVersion="Soap11" writeEncoding="utf-8">         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"           maxBytesPerRead="4096" maxNameTableCharCount="16384" />       </textMessageEncoding>        <!--Manually changed httpTransport to httpsTransport-->       <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"         maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"         bypassProxyOnLocal="false"          decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"         keepAliveEnabled="true" maxBufferSize="65536"          proxyAuthenticationScheme="Anonymous"         realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"         useDefaultWebProxy="true" />     </binding>   </customBinding>  </bindings>  <client>   <endpoint address="https://mainservices-certint.mycompany.com/Services/HRTest"     binding="customBinding" bindingConfiguration="MyBindingConfig"     contract="HRTest.TestWebserviceManagerImpl" name="TestWebserviceManagerImpl" /> </client>   </system.serviceModel> 

References

  1. WCF with custombinding on both http and https

Top 3 video Explaining git-checkout older revision of a file under a new name

Related QUESTION?