GIT

The very first step of every project is to create a repository. Normally a project only have one repo, unless you have some forks for the project.

In essence, a repository is just a directory that contains the project files and a special hidden folder, .git where all of the internal tracking and versioning info takes place.

git init
git init

It is a command used to set different configurations either on a global or local environment.
The configuration file is .gitconfig

git config levels

By default, git will write to the local if no configuration option is passed.

git config user.name "user"
# fatal: not in a git directory

This error will be prompted if you are not within a directory revised by git.

  • --local Local levels are applied to the context a git repository .git/config
  • --global It is user specific it is stored in ~/.gitconfig
  • --system It is applied across an entire machine (all users), /etc/gitconfig

Writing values

If you don't pass it a value such as an username or an email it will just print the configuration value, to set it you have to add it.

git config --global user.name # list name
git config --global user.email  # list email

git config --global user.name "user" # set user
git config --global user.email "email@email" # set email

Change the editor

Many Git commands will launch a text editor to prompt for further input.
Git will look up at core.editor

git config --global core.editor "vim"

Change the default branch

You can override the default branch name e.g. when initializing a new repository.

git config --global init.defaultBranch nameBranch

See also Branching

Git config

Git it is a version control system, (VCS). It is a system that records changes to a file or a set of files over time so you can recall specific versions later. You can revert a file or the entire project to a previous state, compare changes of previous versions or even see who last modified something.

Git thinks of its data more like a series of snapshots of a miniature file system. With Git, every time you git commit, or save the state of your project, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn’t store the file again, just a link to the previous identical file it has already stored. Git thinks about its data more like a stream of snapshots.

Most operations in Git need only local files and resources to operate. Most of the time no information is needed from another computer on your network.

You have the the entire history of the project stored in your local disk.

For example, to browse the history of the project, Git doesn’t need to go out to the server to get the history and display it for you it simply reads it directly from your local database.

Everything in Git is checksummed before it is stored and is then referred to by that checksum. This means it’s impossible to change the contents of any file or directory without Git knowing about it.

What is git
How to set a name or email globally
Writing values

Writing values

If you don't pass it a value such as an username or an email it will just print the configuration value, to set it you have to add it.

git config --global user.name # list name
git config --global user.email  # list email

git config --global user.name "user" # set user
git config --global user.email "email@email" # set email

BASICS

GIT allows you to track code changes over time by author with a set of commands to search, manipulate and revert history.

A commit it is a snapshot of the repository at a given point. It is a way to save the state of the repository. It comes with the author, time of day, a message about the changes you were done, etc.

git commit -m "A message about the changes."
git commit

A file within a git repository can be in one of the 4 states.

  • Untracked Git does not know anything about that file. It has never been added to the index. The index is just the changes you have staged onto your system.
  • Modified The file is changed, but not commited.
  • Staged It marks a modified file in its current version to go into your next commit snapshot. git add file in order to add to the stage a file or files.
  • Commited The data is safely stored in your local database

Git status shows you the current state of your repository. It can be used to obtain a summary of which files have changes that are staged for the next commit or even untracked files.

git status
States

A git repository is a list of commits, where each commit represents the full state of the repository at a given point in time.

The git log command shows a history of commits in a repository.
It shows who made the commit, when the commit was made, what was changed and the commit hash.

git log

You can see just the last commit and its description

git log --oneline

The last ten commits.

git log -n 10
git log

Everything in Git is checksummed before it is stored and is then referred to by that checksum, hence each commit has commit hash.

The mechanism that Git uses for this checksumming is called a SHA-1 hash. This is a 40-character string composed of hexadecimal characters and calculated based on the contents of a file or directory structure in Git.

You can refer to any commit by using the first 7 characters of its hash.

git log 1234abc
Commit hash

Git is made up of objects that are stored in the .git/objects directory. For instance, a commit is just a type of object.
Git stores the commits in directories named with the first 2 characters of the hash to prevent inode busting. If git would't do that you could ended up having too many files in the the same directory which turns to run out of inodes. If you try to cat a git object you will realize that it is illegible because git compresses things. That's why git is so small.

Technologies such as CDN do also this kind of stuff.

git objects

Provide contents or details of repository objects.

To read the content ( or blob ) of a git object

git cat-file -p <SHA1>

You can also read its type

git cat-file -t <SHA1>
git cat-file