Reputation: 21
I am a newbie in version control, and very new in using git. I know that version control systems such as svn store the changes made, while git keeps a record of "snapshots" (commits), so how is it possible to undo a change in git? What is git actually doing?
I also found this: "Are Git's pack files deltas rather than snapshots?" which seems to state git does store deltas.
Upvotes: 2
Views: 756
Reputation: 19025
It is indeed true that a git commit reflects a snapshot of your root directory. When you say "undo a change," I am assuming you mean through git revert [commit]
. When you run that, git looks at [commit]'s parent (which is also a snapshot) and compares it to [commit]'s snapshot, and creates a diff on the fly. It then takes that diff and applies its "opposite" to HEAD. Interestingly enough, if a commit has more than one parent (i.e. it is a merge commit), you need to specify which parent to use to create the diff with the -m option.
As far as pack files go, yes, they use deltas, but that is a storage implementation detail. It does not store commits as deltas, but individual files. In fact, often it stores the latest version of a file in its entirety, and stores previous versions of the file as diffs. But for all intents and purposes, a git commit is a snapshot.
Upvotes: 1
Reputation: 558
Git saves your work in 'snapshots' of the state of your working directory in a given point in time, these saves are called "commits"
To see all the commits you have made you can open git shell in a repository and write:
$ git log
what you'll see is the unique SHA-1 hash of every commit and some information regarding it (who made it, time, title, message, etc.). These commits are specific to the branch you currently have checked (unless you have merged this branch with another one).
To answer you question about how to 'undo a change' (or more correctly: how to return you workspace to the state of a previous commit) you first have to find out the SHA-1 hash of the commit you want to return to (it's not necessary to put the whole hash, with the first 6 characters you are ok).
So, let's say I want to return to a commit with hash: 49c005 . What I have to do is write in the git shell this command:
$ git reset --hard (hash code; in my case 49c005)
there are also other ways to use the "reset" command in git, but this is the one I have found easier.
If you need further reference you can always check out the git reset documentation
Upvotes: 0
Reputation: 4894
Distributed control systems such as git actually store your entire history on each local copy (every version of every file is in there), whereas centralized ones such as SVN require a connection to a central server. While this means your initial "clone" of a git repository may take longer than it would to say, check out an SVN repo, you have the advantage of being able to access the entire history locally (without connecting to some central server) with git.
Upvotes: 0
Reputation: 7633
Git keeps a directory called .git/objects with many files. Each file from your project is hashed, and then renamed to the value of the hash and put under this objects directory. This is how git detects identical files and then only saves them once. When you commit git hashes all the files and records their original name. This data is also stored in the reflog which you can read about here:
http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html
Upvotes: 0