git
What is git?
Git is an Open Source Distributed Version Control System. Now that’s a lot of words to define Git.
Let me break it down and explain the wording:
Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.
Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel. So Version Control System helps in handling this by maintaining a history of what changes have happened.
Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer.
How to use git - practical examples
Creating a repository
Note that part of the commands can only be done once a remote repository has been created. This can be done after signing up for an account github or gitlab
Branches
What are git branches?
For an idea about what git branches are, you can compare git branching to a tree. Every branch of the tree goes their own way. The same is actually happening for git branches in git. However, the difference with branches in git is that you can eventually merge the git branches back to the master branch.
An example;
You've created an index.html page on the master branch. After that you create a new branch for the contact page, you call this branch contact and create a contact.html file. After you pushed your contact page to their branch, you notice that the master branch contains a bug. You accidentally made a typo with
<<head>
instead of<head>
. You create a new branch for the bug and you name that branch bugfix1From this point you have 3 branches; master, contact and bugfix1.
The next thing you need to do is merging the branches together. back to the master branch.
How to use git branches
Configure tooling
Set a tag/version for a project
Create repositories
When starting out with a new repository, you only need to do it once; either locally, then push to GitHub, or by cloning an existing repository.
$ git init
After using the git init
command, link the local repository to an empty GitHub repository using the following command:
$ git remote add origin [url]
Turn an existing directory into a Git repository
$ git clone [url]
Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits
The .gitignore file
Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore
. You can find helpful templates for .gitignore
files at github.com/github/gitignore.
Synchronize changes
Synchronize your local repository with the remote repository on GitHub.com ---
$ git fetch
Downloads all history from the remote tracking branches
$ git merge
Combines remote tracking branches into current local branch
$ git push
Uploads all local branch commits to GitHub
$ git pull
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull
is a combination of git fetch
and git merge
Make changes
Browse and inspect the evolution of project files
$ git log
Lists version history for the current branch
$ git log --follow [file]
Lists version history for a file, including renames
$ git diff [first-branch]...[second-branch]
Shows content differences between two branches
$ git show [commit]
Outputs metadata and content changes of the specified commit
$ git add [file]
Snapshots the file in preparation for versioning
$ git commit -m "[descriptive message]"
Records file snapshots permanently in version history
Redo commits
Erase mistakes and craft replacement history
$ git reset [commit]
Undoes all commits after [commit]
, preserving changes locally
$ git reset --hard [commit]
Discards all history and changes back to the specified commit
CAUTION! Changing history can have nasty side effects. If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at github.community or contact support.
Glossary
git: an open source, distributed version-control system
GitHub: a platform for hosting and collaborating on Git repositories
commit: a Git object, a snapshot of your entire repository compressed into a SHA
branch: a lightweight movable pointer to a commit
clone: a local version of a repository, including all commits and branches
remote: a common repository on GitHub that all team members use to exchange their changes
fork: a copy of a repository on GitHub owned by a different user
pull request: a place to compare and discuss the differences introduced on a branch with reviews, comments, integrated tests, and more
HEAD: representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits when us
NHL Stenden
Reference(s)
git cheat-sheet
Last updated
Was this helpful?