Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### What is Git? Describe the method Git uses to store files in a repository.
- Git is a fully distributed Version Control System (VCS) that is efficient, robust, and simple to use.
- If you are unfamiliar with Git or a VCS, think of the problems that can arise from a rudimentary system of copying a project directory and saving it under a new timestamp multiple times a day. Imagine a team working on this same directory and attempting to manually merge everyone's contributions.
- Comparing changes between an old file and a new one is unreliable and attributing a change to a particular user is difficult. The solution is a version control system and there are a few types in use today.
- Git mirrors a central repository, or the storage location of a project's history. If a server fails, projects are protected because there could be several clones on other local machines. This repository serves as the single point-of-entry for all changes. Multiple collaborators can copy the entire project, work independently, then integrate their changes back to the main repository with ease.
- #### The Git Method
- How does Git store files in a repository? It takes a snapshot of a project with the `git commit` command and stores it in a tree structure. Each commit is referenced by a SHA1 hash.
- 18af1bb0ae95301e244f0c4d09c2f1d935d2ad2f
- This snapshot is a comprehensive, unique record of what every file in the directory looked like at a moment in time and which user made the commit. By running `git add` and `git commit`, Git stores the commit, tree, and blob objects as separate files in the .git/objects directory.
- To demonstrate how Git stores files, create a simple repository.
- $ mkdir my_repo
- $ cd my_repo
- $ git init
- Initialized empty Git repository in /Users/azure/my_repo/.git/
- Next, stage the files and commit them.
- $ echo "Hello World" > example.txt
- $ git add .
- $ git commit -m "Initial commit"
- [master (root-commit) 72757b0] Initial commit
- 1 file changed, 1 insertion(+)
- create mode 100644 example.txt
- Now, view your project's files with the command `find .git/objects -type f`. The output will show three objects each named with a unique SHA1 checksum. By running the `git cat-file -t` command, you can inspect each object and determine whether it is a commit, tree, or blob. With the flag `-p`, you get a more detailed view.
- $ find .git/objects -type f
- .git/objects/72/757b072e58bd2a48bcb19850ef755262e586ff
- .git/objects/91/dd72904e26baa26c934c5222714e460a43dc1b
- .git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
- **Commit Object**
- The commit object stores information about who saved the snapshot, when it was saved, what changes were made, and why they were made.
- $ git cat-file -p 72757b072e58bd2a48bcb19850ef755262e586ff
- tree 91dd72904e26baa26c934c5222714e460a43dc1b
- author Azure <email@gmail.com> 1554763287 -0700
- committer Azure <email@gmail.com> 1554763287 -0700
- Initial commit
- **Tree Object**
- Tree objects have their own SHA and can point to numerous blobs and trees that also have their own SHA numbers. The tree object in the example contains the contents of a directory. If a new directory is created, that will be listed as a subtree object beneath example.txt.
- $ git cat-file -p 91dd72904e26baa26c934c5222714e460a43dc1b
- 100644 blob 557db03de997c86a4a028e1ebd3a1ceb225be238 example.txt
- **Blob Object**
- Blob stands for binary large object and it contains the content of a file.
- $ git cat-file -p 557db03de997c86a4a028e1ebd3a1ceb225be238
- Hello World
- Git facilitates the collaboration between multiple users while preserving the history and keeping files up-to-date. Regardless of the project type, if version control is important to the work flow, Git will serve as a fantastic tool.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement