This document provides an overview of basic Git commands and workflows. It discusses what Git is, why it's useful for version control and collaboration, and how to install, set up, initialize and commit code to a Git repository. It also covers branching, merging, tagging, forking repositories, and resolving merge conflicts. The document recommends setting up a remote repository on a hosting service like GitHub and pushing and pulling changes between local and remote repositories. It challenges readers to participate in Hacktoberfest and suggests some GUI tools for Git.
3. @rebelwebdev
What is it
● Git is a tool to keep track of every change made to your code
● Git is a distributed source control (unlike TFVS which is centralized)
● Git itself is not a issue tracker, the tools build on top of git are issue trackers
(i.e. Github, Gitlab, TFS)
3
4. @rebelwebdev
Why?
● Git provides tools to make working in a team easier compared to working
without.
● Keep a history of changes
● Versioning (release points)
● Code Reviews
4
5. @rebelwebdev
Installing Git
● Windows
○ Installer Available at: https://git-scm.com/download/win
● Linux
○ Install using your favorite package manager
● Mac
○ Install using MacPorts, Homebrew, https://git-scm.com
● BSD
5
6. @rebelwebdev
Git Setup
● Windows will need extra setup for using Git over SSH
● Setup global config options
○ User Name
○ User E-Mail Address
○ Commit Message Editor
git config --global user.name “Ryan Condron”
git config --global user.email “rebelwebdevelopment@gmail.com”
git config --global core.editor vim
6
7. @rebelwebdev
Initialize Your Project
● Open command line and change to the root directory of your project.
● Run the initialize command
● Prepare for your first commit
cd /Users/ryan/code/project
git init
7
8. @rebelwebdev
Preparing For Your First Commit
● Check for secrets (i.e. API Keys, encryption keys, passwords, etc.)
● .gitignore - ignores files and directories that should not be changed
○ Starter Templates: https://github.com/github/gitignore
○ Should be in every project
● Add a README file (general overview of the project)
● Run git status to make sure files and directories that need to be ignored are
ignored (i.e. secrets)
git status
8
9. @rebelwebdev
Things To Add To Source Control (git)
● Code
● Documentation
● Database Schema
● Automated Tests
● Photoshop Files
9
10. @rebelwebdev
Things Not to Add To Source Control (git)
● Secrets - anything that could compromise the end product if revealed
○ Passwords
○ API Keys
○ Encryption Keys
● Log files
● SSH Keys
● Dependencies (Add documentation Instead)
10
12. @rebelwebdev
Staging files
Before you can commit you will need to stage files you want to commit. This is
performed by using the add subcommand followed by the directory or filename
with the full path. If the ‘--all’ switch is used it will stage all changed and new
files.
git add docs/setup.md
git add --all
12
13. @rebelwebdev
Git Commit
This can be performed a couple different ways with the ‘-m’ switch which
commits with a basic message and without which will open the default editor to
edit the commit message, used for more detailed messages.
git commit -m “Initial Commit”
git commit
git status
13
15. @rebelwebdev
Good vs Bad Commit Messages
Updated setup docs to include dependencies
Setup docs was missing what dependencies are
required for development. I added a section on
how to setup those dependencies and where to
download them from.
Changed setup doc
15
16. @rebelwebdev
Git History
Each time you make a commit there is a entry in your history. You can view your
commit history. Your history keeps track of changes you have made to your
project all the way back to the initial commit. You can view history in the web
interface of your remote or using git log.
Remember to work in small commits, to make your history meaningful
git log
16
17. @rebelwebdev
Removing files and history
● History will show removed code as well as added code.
● Keep in mind that when you remove a file, code, or that password you forgot
to ignore, IT WILL STILL BE IN HISTORY AND VISIBLE!!!
● I have a tool for you at the end, to help remove these from history.
17
18. @rebelwebdev
Setup a Remote to Track Changes
After committing you should set up a remote to manage changes.
Several options exist for hosting for little to no cost.
● https://github.com (free public repos, paid private repos)
● https://gitlab.com (free repos public & private, pay for premium features)
● https://www.visualstudio.com/team-services/ (free for small teams)
● https://bitbucket.com (free for small teams)
git remote add origin https://github.com/username/repo.git
18
19. @rebelwebdev
Pushing
Once your remote is setup you can push your local changes to it using the push
subcommand followed by the remote name (central repository) and branch.
git push origin master
19
20. @rebelwebdev
Pulling
Pulling allows you to bring changes down from the remote to your local machine.
Just like pushing you can pull from a remote to your local repository. It will pull
changes from the remote branch to the current working branch on your local
machine.
git pull origin master
20
21. @rebelwebdev
Branching
Branching allows you to make code changes in an isolated environment, so you
won’t affect your production code base. This allow you to push bug fixes without
deploying partially built new features or waiting until those features are
complete.
● Goal is to keep master (main branch) stable
● Make changes in branches and merges changes into master once complete.
● Freedom to experimentgit branch experiment
git checkout -b experiment
21
22. @rebelwebdev
Merging
So we made changes in a branch and are ready to put those changes in
production. We need to merge. Most of the time you will merge using your
remote repository’s web interface. The exception would be merge conflicts. To
merge using the command line interface you first checkout out the target branch
and use the merge subcommand and passing the name of the merging branch.
git checkout master
git merge experiment
22
24. @rebelwebdev
Merge Conflicts
Merge conflicts are when the changes made in the merging branch conflict with
changes made in the target branch since the merging branch was created. In this
case you/your team will need to go through the conflicts and decide how to
move forward.
24
25. @rebelwebdev
Resolving Merge Conflicts
Most git hosting services will guide you through the process. You will have a list
of conflicts (sometimes there is more than one) and you will need to go to each
affected file and decide which branches version to keep, which to remove,
sometimes a mix between both.
If doing on a local machine my favorite tool is the merge-conflicts plugin for
Atom. It will give you a list and you pick and choose the changes you want or a
combination of both.
25
26. @rebelwebdev
Tagging
When you deploying to production you should generate a tag to show deploy
points in your code base. A tag is a name for a specific commit. This works
similar to git commit. It is recommend that your tags name are a semantic
version number.
git tag -a ‘0.0.1’ -m ‘First release’
git tag -a ‘0.0.1’
26
27. @rebelwebdev
Semantic Versioning
Semantic Versioning is a standardized way to version software. It is three set of
numbers separated by (.) - 5.4.0
● The first number is a major version (5)
○ Introduce major features or changes
● The second number is the minor version (4)
○ Introduce minor features or changes
● The third number is the patch number (0)
○ Big fixes
27
28. @rebelwebdev
Forking
Most remote servers allow you to fork or copy a repository to a new location with
a new name. The main use of this is when you want to work with a project that
isn’t yours and you want to make a change i.e. a bug fix or a new feature. You
can do a pull request to allow the forked repository to pull in your change.
28
29. @rebelwebdev
Challenge: Hacktoberfest
Typically Digital Ocean runs a program during the month of October, to commit
to open source software. Last year you had to open 4 pull/merge requests on
github, during the month of October. When you complete the mission you get
goodies, typically a T-Shirt and stickers.
My challenge to you, is to participate!
29
30. @rebelwebdev
Bonus: Git GUI Tools
If command line is not your cup of tea, there are a few gui clients to help you out
● https://www.gitkraken.com/pro (cost for commercial use)
● https://www.sourcetreeapp.com/ (free)
● Many editors have git tools built in!
30
Editor's Notes
Welcome
Discussing Git Basics
Goal is to be able to use the core concepts and commands to use git for source control.
Distributed
No Central Repository (meaning no true master copy)
Ability to work without a connection to a central repo
Great working behind firewalls or without internet connection
Elaborate on TFVS distributed.
Available on most platforms
Integrated into most IDE’s (Visual Studio, Atom, etc.)
The --global sets the default for all projects, if it is removed it will only apply to the project of your current working directory
Windows make sure you add git to your PATH for the command will not be available in cmd and setup .ssh folder inside your user directory
Will generate a .git folder at the root of your project for meta data.
Think about your folder structure, you may need a parent folder containing a folder with code and a folder with documentation/configuration. I.e. static websites
There are certain things you want to add to source control or git, but not everything
AWS API Keys in Github (cost users $1000’s)
One exception with dependencies is working with legacy code bases and dependencies are hard to find.
Remember depending on your repository settings you code may searchable.
This will be the starting point to your repository. I.e. the beginning of time
Note that if changes to a file after the add subcommand, the command will need ran again to include those changes.
You should work in small commits.
Empty directories will not be committed, to keep the directory structure in git, simple add a .keep or .gitkeep (empty file) to the directory
Describe your changes, don’t be afraid to explain yourself.
Other developers love when you drop context about the code you changed in the commit body
For there to be information in the log you need to make commits more often and have good commit messages.
Remote hosting services often come with an application to handle some git functions and track issues
The best remote will depend on your setup.
Origin is the name of the remote
Master is the name of the branch
Experiment without breaking production
Master typically is the holy grail and should be kept stable.
Protected Branches
Example using gitlab
Plan to have a future presentation on tips and tricks handling merge conflicts.
Input release notes in tag body to help keep track of history
Tags are typically done after a merge or a new feature is added to the code base.
If you are working on a library that is used my multiple projects, you shouldn’t introduce new features on a patch release.
Essentially a clone and push
Show website
While we discussed using basic of git from the command line, gui tools are available.