This document provides an introduction and overview of Git and GitHub. It discusses version control systems and the differences between centralized and distributed version control. Key Git concepts and commands are explained such as configuring Git, creating repositories, adding and committing files, branches, pulling and pushing changes. GitHub is introduced as a platform for hosting Git repositories and collaborating on projects. The document provides instructions for adding collaborators to a GitHub repository.
3. 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
4. Version Control System
- A way to manage files and directories
- Track changes over time
- Recalls previous versions
6. 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
7. 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
⚙
8. 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
9. 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
10. 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
11. 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
12. 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.
13. 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
14. 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.
15. 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
16. Synchronising changes
$ git fetch
Downloads all history from the remote tracking branches
$ git merge
Combines remote tracking branches into current local branch
17. 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
18. 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
19. 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.
20. 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.
git init turns any directory into a Git repository.
To initialize a repository, Git creates a hidden directory called .git. That directory stores all of the objects and refs that Git uses and creates as a part of your project's history. This hidden .git directory is what separates a regular directory from a Git repository.
The git clone command is used to create a copy of a specific repository or branch within a repository.
To start a repository, use either git init or git clone - not both.
git init: One Person Starting a New Repository Locally
git clone: The Remote Already Exists
git commit creates a commit, which is like a snapshot of your repository. These commits are snapshots of your entire repository at specific times. You should make new commits often, based around logical units of change. Over time, commits should tell a story of the history of your repository and how it came to be the way that it currently is. Commits include lots of metadata in addition to the contents and message, like the author, timestamp, and more.
git commit -m "descriptive commit message": This starts the commit process, and allows you to include the commit message at the same time.
git commit -am "descriptive commit message": In addition to including the commit message, this option allows you to skip the staging phase.
git commit --amend: Replaces the most recent commit with a new commit.
What Kind of Files Should You Ignore?
Log files
Files with API keys/secrets, credentials, or sensitive information
Useless system files like .DS_Store on macOS
Generated files like dist folders
Dependencies which can be downloaded from a package manager
And there might be other reasons (maybe you make little todo.md files)
git push updates the remote branch with local commits. It is one of the four commands in Git that prompts interaction with the remote repository. You can also think of git push as update or publish.
By default, git push only updates the corresponding branch on the remote. So, if you are checked out to the master branch when you execute git push, then only the master branch will be updated. It's always a good idea to use git status to see what branch you are on before pushing to the remote.
Common usages and options for git push
git push -f: Force a push that would otherwise be blocked, usually because it will delete or overwrite existing commits (Use with caution!)
git push -u origin [branch]: Useful when pushing a new branch, this creates an upstream tracking branch with a lasting relationship to your local branch
git push --all: Push all branches
Common usages and options for git pull
git pull: Update your local working branch with commits from the remote, and update all remote tracking branches.
git pull --rebase: Update your local working branch with commits from the remote, but rewrite history so any local commits occur after all new commits coming from the remote, avoiding a merge commit.
git pull --force: This option allows you to force a fetch of a specific remote tracking branch when using the <refspec> option that would otherwise not be fetched due to conflicts. To force Git to overwrite your current branch to match the remote tracking branch, read below about using git reset.
git pull --all: Fetch all remotes - this is handy if you are working on a fork or in another use case with multiple remotes.