GIT
Git is a distributed version control and source code management system.
It does this through a series of snapshots of your project, and it works with those snapshots to provide you with functionality to version and manage your source code.
Versioning Concepts
What is version control?
Version control is a system that records changes to a file(s), over time.
Centralized Versioning vs. Distributed Versioning
Centralized version control focuses on synchronizing, tracking, and backing up files.
Distributed version control focuses on sharing changes. Every change has a unique id.
Distributed systems have no defined structure. You could easily have a SVN style, centralized system, with git.
Why Use Git?
Can work offline.
Collaborating with others is easy!
Branching is easy!
Branching is fast!
Merging is easy!
Git is fast.
Git is flexible.
Git Architecture
Repository
A set of files, directories, historical records, commits, and heads. Imagine it as a source code data structure, with the attribute that each source code āelementā gives you access to its revision history, among other things.
A git repository is comprised of the .git directory & working tree.
.git Directory (component of repository)
The .git directory contains all the configurations, logs, branches, HEAD, and more. Detailed List.
Working Tree (component of repository)
This is basically the directories and files in your repository. It is often referred to as your working directory.
Index (component of .git dir)
The Index is the staging area in git. Itās basically a layer that separates your working tree from the Git repository. This gives developers more power over what gets sent to the Git repository.
Commit
A git commit is a snapshot of a set of changes, or manipulations to your Working Tree. For example, if you added 5 files, and removed 2 others, these changes will be contained in a commit (or snapshot). This commit can then be pushed to other repositories, or not!
Branch
A branch is essentially a pointer to the last commit you made. As you go on committing, this pointer will automatically update to point to the latest commit.
Tag
A tag is a mark on specific point in history. Typically people use this functionality to mark release points (v1.0, and so on).
HEAD and head (component of .git dir)
HEAD is a pointer that points to the current branch. A repository only has 1 active HEAD. head is a pointer that points to any commit. A repository can have any number of heads.
Stages of Git
Modified - Changes have been made to a file but file has not been committed to Git Database yet
Staged - Marks a modified file to go into your next commit snapshot
Committed - Files have been committed to the Git Database
Conceptual Resources
Commands
init
Create an empty Git repository. The Git repositoryās settings, stored information, and more is stored in a directory (a folder) named ā.gitā.
config
To configure settings. Whether it be for the repository, the system itself, or global configurations ( global config file is ~/.gitconfig
).
help
To give you quick access to an extremely detailed guide of each command. Or to just give you a quick reminder of some semantics.
ignore files
To intentionally untrack file(s) & folder(s) from git. Typically meant for private & temp files which would otherwise be shared in the repository.
status
To show differences between the index file (basically your working copy/repo) and the current HEAD commit.
add
To add files to the staging area/index. If you do not git add
new files to the staging area/index, they will not be included in commits!
This only adds a file to the staging area/index, it doesnāt commit it to the working directory/repo.
branch
Manage your branches. You can view, edit, create, delete branches using this command.
tag
Manage your tags
checkout
Updates all files in the working tree to match the version in the index, or specified tree.
clone
Clones, or copies, an existing repository into a new directory. It also adds remote-tracking branches for each branch in the cloned repo, which allows you to push to a remote branch.
commit
Stores the current contents of the index in a new ācommit.ā This commit contains the changes made and a message created by the user.
diff
Shows differences between a file in the working directory, index and commits.
grep
Allows you to quickly search a repository.
Optional Configurations:
Google is your friend; for more examples Git Grep Ninja
log
Display commits to the repository.
merge
āMergeā in changes from external commits into the current branch.
mv
Rename or move a file
pull
Pulls from a repository and merges it with another branch.
push
Push and merge changes from a branch to a remote & branch.
stash
Stashing takes the dirty state of your working directory and saves it on a stack of unfinished changes that you can reapply at any time.
Letās say youāve been doing some work in your git repo, but you want to pull from the remote. Since you have dirty (uncommitted) changes to some files, you are not able to run git pull
. Instead, you can run git stash
to save your changes onto a stack!
Now you can pull!
...changes apply...
Now check that everything is OK
You can see what āhunksā youāve stashed so far using git stash list
. Since the āhunksā are stored in a Last-In-First-Out stack, our most recent change will be at top.
Now letās apply our dirty changes back by popping them off the stack.
git stash apply
does the same thing
Now youāre ready to get back to work on your stuff!
rebase (caution)
Take all changes that were committed on one branch, and replay them onto another branch. Do not rebase commits that you have pushed to a public repo.
reset (caution)
Reset the current HEAD to the specified state. This allows you to undo merges, pulls, commits, adds, and more. Itās a great command but also dangerous if you donāt know what you are doing.
reflog (caution)
Reflog will list most of the git commands you have done for a given time period, default 90 days.
This give you the chance to reverse any git commands that have gone wrong (for instance, if a rebase has broken your application).
You can do this:
git reflog
to list all of the git commands for the rebase
Select where to reset to, in our case its
2e6c386
, orHEAD@{5}
āgit reset āhard HEAD@{5}ā this will reset your repo to that head
You can start the rebase again or leave it alone.
revert
Revert can be used to undo a commit. It should not be confused with reset which restores the state of a project to a previous point. Revert will add a new commit which is the inverse of the specified commit, thus reverting it.
rm
The opposite of git add, git rm removes files from the current working tree.
blame
Examine specific parts of the codeās history and find out who was the last author to modify that line.
Further Information
Last updated