GIT Docs
GIT Docs
What is Git?
Git is a popular version control system. It was created by Linus Torvalds in
2005, and has been maintained by Junio Hamano since then.
It is used for:
Tracking code changes
Tracking who made changes
Coding collaboration
Why Git?
Over 70% of developers use Git!
Developers can work together from anywhere in the world.
Developers can see the full history of the project.
Developers can revert to earlier versions of a project.
What is GitHub?
Git is not the same as GitHub.
GitHub makes tools that use Git.
GitHub is the largest host of source code in the world, and has been
owned by Microsoft since 2018.
Git Install
You can download Git for free from the following website: https://www.git-
scm.com/
To start using Git, we are first going to open up our Command shell.
For Windows, you can use Git bash, which comes included in Git for
Windows. For Mac and Linux you can use the built-in terminal.
The first thing we need to do, is to check if Git is properly installed:
Example
$ git --version
$ git version 2.30.2.windows.1
Change the user name and e-mail address to your own. You will probably
also want to use this when registering to GitHub later on.
Note: Use global to set the username and e-mail for every repository on
your computer.
If you want to set the username/e-mail for just the current repo, you can
remove global.
Initialize Git
Navigate to the correct folder, you can initialize Git on that folder:
Example
$ git init
Initialized empty Git repository in /Users/user/myproject/.git/
You just created your first Git Repository!
Note: Git now knows that it should watch the folder you initiated it on.
Git creates a hidden folder to keep track of changes.
Git Commit
Since we have finished our work, we are ready move
from stage to commit for our repo.
Adding commits keep track of our progress and changes as we work. Git
considers each commit change point or "save point". It is a point in the
project you can go back to if you find a bug, or want to make a change.
When we commit, we should always include a message.
By adding clear messages to each commit, it is easy for yourself (and others)
to see what has changed and when.
Example
$ git commit -m "First release of Hello World!"
Git Help
If you are having trouble remembering commands or options for commands,
you can use Git help.
There are a couple of different ways you can use the help command in
command line:
git command -help - See all the available options for the specific
command
git help --all - See all possible commands
Git Branches
In Git, a branch is a new/separate version of the main repository.
Branches allow you to work on different parts of a project without impacting
the main branch.
When the work is complete, a branch can be merged with the main project.
You can even switch between branches and work on different projects
without them interfering with each other.
Branching in Git is very lightweight and fast!
Why do we need Branches?
New features could break code
Code collaboration
Create a new branch:
Example
$ git branch new-local-repo
We can see the new branch with the name "new-local-repo ", but
the * beside master specifies that we are currently on that branch.
checkout is the command used to check out a branch. Moving us from the
current branch, to the one specified at the end of the command:
Example
$ git checkout new-local-repo
Switched to branch 'new-local-repo'
Note: Using the -b option on checkout will create a new branch, and move to
it, if it does not exist.
Merge Branches
Example
$ git checkout master
Switched to branch 'master'
Now we merge the current branch (master) with emergency-fix:
Example
$ git merge emergency-fix
Deleting Branches
After merging branch into main codebase, you can delete branch without
leaving any history.
Example
$ git branch –d new-local-repo
Fork a Repository
A fork is a copy of a repository. This is useful when you want to contribute to
someone else's project or start your own project based on theirs.
fork is not a command in Git, but something offered in GitHub and other
repository hosts.
Configuring Remotes
Basically, we have a full copy of a repository, whose origin we are not
allowed to make changes to.
Let's see how the remotes of this Git is set up:
Example
$ git remote -v
origin https://github.com/abc-test/abc-test.github.io.git (fetch)
origin https://github.com/abc-test/abc-test.github.io.git (push)
We see that origin is set up to the original "abc-test" repository, we also want
to add our own fork.
First, we rename the original origin remote:
Example
$ git remote rename origin upstream
$ git remote -v
upstream https://github.com/abc-test/abc-test.github.io.git (fetch)
upstream https://github.com/abc-test/abc-test.github.io.git (push)
Example
$ touch .gitignore
# ignore ALL .log files
*.log