0% found this document useful (0 votes)
53 views

GIT Docs

Uploaded by

umarfarooque869
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

GIT Docs

Uploaded by

umarfarooque869
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

GIT

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

What does Git do?


 Manage projects with Repositories
 Clone a project to work on a local copy
 Control and track changes with Staging and Committing
 Branch and Merge to allow for work on different parts and versions of
a project
 Pull the latest version of the project to a local copy
 Push local updates to the main project

Working with Git


 Initialize Git on a folder, making it a Repository
 Git now creates a hidden folder to keep track of changes in that folder
 When a file is changed, added or deleted, it is considered modified
 You select the modified files you want to Stage
 The Staged files are Committed, which prompts Git to store
a permanent snapshot of the files
 Git allows you to see the full history of every commit.
 You can revert back to any previous commit.
 Git does not store a separate copy of every file in every commit, but
keeps track of changes made in each commit!

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

If Git is installed, it should show something like git version X.Y


Configure Git
Now let Git know who you are. This is important for version control systems,
as each Git commit uses this information:
Example
$ git config --global user.name "abc-test"
$ git config --global user.email [email protected]

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 Adding New Files


You just created your first local Git repo. But it is empty.
So let's add some files, or create a new file using your favourite text editor.
Then save or move it to the folder you just created.
Then we check the Git status and see if it is a part of our repo.
Now Git is aware of the file, but has not added it to our repository!
Files in your Git repository folder can be in one of 2 states:
 Tracked - files that Git knows about and are added to the repository
 Untracked - files that are in your working directory, but not added to
the repository
When you first add files to an empty repository, they are all untracked. To
get Git to track them, you need to stage them, or add them to the staging
environment.

Git Staging Environment


One of the core functions of Git is the concepts of the Staging
Environment, and the Commit.
As you are working, you may be adding, editing and removing files. But
whenever you hit a milestone or finish a part of the work, you should add the
files to a Staging Environment.
Staged files are files that are ready to be committed to the repository you
are working on.

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!"

The commit command performs a commit, and the -m "message" adds a


message.
The Staging Environment has been committed to our repo, with the
message:
"First release of Hello World!"

Git Commit Log


To view the history of commits for a repository, you can use
the log command:
Example
$ git log

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

Let's confirm that we have created a new branch:


Example
$ git branch
new-local-repo
* master

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

Add to Someone Else's Repository


At the heart of Git is collaboration. However, Git does not allow you to add
code to someone else's repository without access rights.

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.

Clone a Fork from GitHub


Now we have our own fork, but only on GitHub. We also want a clone on our
local Git to keep working on it.
A clone is a full copy of a repository, including all logging and versions of
files.
Open your Git bash and clone the repository:
Example
$ git clone https://github.com/abc-test/abc-test.github.io.git

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)

Then fetch the URL of our own fork:


And add that as origin:
Example
$ git remote add origin https://github.com/xyz/abc-test.github.io.git
$ git remote -v
origin https://github.com/xyz/abc-test.github.io.git (fetch)
origin https://github.com/xyz/abc-test.github.io.git (push)
upstream https://github.com/abc-test/abc-test.github.io.git (fetch)
upstream https://github.com/abc-test/abc-test.github.io.git (push)

Note: According to Git naming conventions, it is recommended to name


your own repository origin, and the one you forked for upstream
Now we have 2 remotes:
 origin - our own fork, where we have read and write access
 upstream - the original, where we have read-only access
Git Ignore
When sharing your code with others, there are often files or parts of your
project, you do not want to share.
Examples
 log files
 temporary files
 hidden files
 personal files
 etc.
Git can specify which files or parts of your project should be ignored by Git
using a .gitignore file.
Git will not track files and folders specified in .gitignore. However,
the .gitignore file itself IS tracked by Git.

Example
$ touch .gitignore
# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp


temp/

You might also like