+ +Presented by
Introduction to Git and GitHub
AGENDA
1. Introduction to Version Control System
- What is Version Control System
- Centralised vs Distributed Version Control Systems
2. Introduction to Git
- What is Git
- Configuring Git and Creating repositories
- Add, commit, push
- The .gitignore file
- Branches
- Synchronising changes and Make changes
3. Introduction to GitHub
- What is GitHub
- Collaborating using Git and GitHub
Version Control System
- A way to manage files and directories
- Track changes over time
- Recalls previous versions
Centralised vs Distributed VCS
Git
What is Git?
Git is distributed version
control software.Git
repository has a copy of
that entire repository -
every commit, every
branch, every file.
Why use Git
● Speed
● Merge Conflicts
● Cheap Branches
● Ease of roll back
Configure Tooling
Configure user information for all local repositories
$ git config --global user.name "[name]"
Sets the name you want attached to your commit transactions
$ git config --global user.email "[email address]"
Sets the email you want attached to your commit transactions
⚙
Creating Repository
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
Or
$ git clone [url]
Clone (download) a repository that already exists on GitHub,
including all of the files, branches, and commits
git add
The git add command adds new or changed files in your working directory to the Git
staging area.
Few commonly used git add commands are:
$ git add <path>
Stage a specific directory or file
$ git add .
Stage all files (that are not listed in the .gitignore) in the entire repository
$ git add -p
Interactively stage hunks of changes
git status
$ git status shows the current state of your Git working directory and staging
area.
Why use git status?
The git status command only outputs information, it won't modify commits or changes
in your local repository.
What kind of information do we get?
Where HEAD is pointing, whether that is a branch or a commit .
If changed files are staged or not
git commit
git commit creates a commit, which is like a snapshot of your repository.
These commits are snapshots of your entire repository at specific times. It
should tell a story of the history of your repository and how it came to be the
way that it currently is.
How to use git commit?
git commit -m "descriptive commit message"
git commit -am "descriptive commit message"
git commit --amend
The .gitignore file
A .gitignore file is a plain text file where each line
contains a pattern for files/directories to ignore.
Generally, this is placed in the root folder of the
repository. You can also have multiple .gitignore files.
Why use .gitignore?
It is too easy to accidentally commit files containing
sensitive information or unnecessary files. The .gitignore
file comes in handy, escaping from these accidents.
https://www.toptal.com/developers/gitignore -> This link
can be used to generate .gitignore files effortlessly.
git push
$ git push uploads all local branch commits to the corresponding
remote branch. You can think of git push as “update” or “publish”. It only
updates the corresponding branch on the remote.
Common usages for git push
● $ git push -f
● $ git push -u origin [branch]
● $ git push --all
Branches
A branch in Git is simply a lightweight movable pointer to one of these commits. The
default branch name in Git is master . As you start making commits, you're given a
master branch that points to the last commit you made. Every time you commit, the
master branch pointer moves forward automatically.
Creating a New Branch
$ git branch <branch name>
Switching Branches
To switch to an existing branch, you run the git checkout command.
$ git checkout <branch name>
This moves HEAD to point to the <branch name> branch.
git pull
git pull updates your current local working branch, and all of the remote
tracking branches.
Common usages and options for git pull
● $ git pull
● $ git pull --rebase
● $ git pull --force
● $ git pull --all
Synchronising changes
$ git fetch
Downloads all history from the remote tracking branches
$ git merge
Combines remote tracking branches into current local branch
Make changes
$ 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
How to Undo Commits in Git
$ git revert
git revert looks at the changes introduced in a specific commit,
then applies the inverse of those changes in a new commit.
$ git reset
git reset is a very powerful command that may cause you to lose
work. By resetting, you move the HEAD pointer and the branch
pointer to another point in time - maybe making it seem like the
commits in between never happened
Introduction to GitHub
GitHub is a repository hosting platform for version
control system git. It lets you and others work together
on projects from anywhere.
Collaborating with GitHub
Adding collaborators in GitHub repository
A. On GitHub, navigate to the main
page of the repository.
B. Under your repository name,
click Settings.
C. In the left sidebar, click Manage
access.
D. Click Invite a collaborator and
Add NAME to REPOSITORY.
E. The user will receive an email
inviting them to the repository.
Presented by
The End