Git Fundamentals

Open Up A World Of Community and Collaboration

Paul Broadwith
  • Paul Broadwith, Glasgow Scotland
  • 25+ years in IT in financial, government, manufacturing and services sectors
  • Lead Engineer on Boxstarter and cChoco DSC Resource

MVP Logo
Chocolatey Logo
Scottish PowerShell and DevOps User Group Logo

Questions At The End?

  • What actually is Git?
  • What tools do I need for Git?
  • Who is using Git and why do I need to use it?
  • How does Git work?
  • Git commands you need to know!
  • What is a Pull Request and what they are used for;
  • How to work with Open Source projects using Git;

What Is Git?

Wikipedia defines Git as:

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.

Wikipedia defines Git as:

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.

Wikipedia defines Version Control System as:

… version control is the management of changes to documents, computer programs, large web sites, and other collections of information. Changes are usually identified by a number or letter code, termed the “revision number“ … Each revision is associated with a timestamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.

Wikipedia defines Version Control System as:

version control is the management of changes to documents, computer programs, large web sites, and other collections of information. Changes are usually identified by a number or letter code, termed the “revision number“ … Each revision is associated with a timestamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.

Which means:

Git is a version control system which manages changes to collections of information. The changes are identified by a number, timestamp and the person making the change. It is aimed at speed and data integrity and supports distrubuted workflows.

or

What Tools Do I Need?

Chocolatey Logo

Chocolatey Home Page - https://chocolatey.org

Install Chocolatey - https://chocolatey.org/install

Git Tools

Download from https://gitforwindows.org

> choco install git -y

Git Tools

Download from https://git-scm.com/download/mac

$ brew install git

Git Tools

Ubuntu Logo
$ sudo apt-get install git
CentOS Logo
$ sudo yum install git

Visual Studio Code Logo

Visual Studio Code

https://code.visualstudio.com/docs/setup/setup-overview

> choco install vscode -y
$ brew cask install visual-studio-code
$ # See setup URL for options

Gitex Logo
GitEx

> choco install gitextensions -y

Git Logo
Git Credential Manager For Windows

> choco install git-credential-manager-for-windows -y
Gitlens Logo
> choco install vscode-gitlens -y

GitHub Logo
GitHub Pull Requests

posh-git

https://github.com/dahlbyk/posh-git

posh-git PowerShell Prompt

ps-git

last updated 2017

https://github.com/PoshCode/PSGit

Who Uses Git?

Chocolatey logo
Visual Studio Code Logo
IBM Logo
Facebook Logo
Microsoft Logo
Netflix Logo
Red Hat Logo

Git Cloud Providers

GitHub Octocat Logo

Gitlab Logo
Bitbucket Logo

 
GitHub Octocat Logo
Gitlab Logo
Bitbucket Logo
Version Control Systems
Mercurial Logo
Unlimited Public Repos
Unlimited Private Repos *
CI / CD *
(Subjective) Popularity

What Can I Use Git For?

Developers, Coders and Scripters.

Writers and Authors.

Websites.

CI / CD, Runbooks, Cookbooks and Playbooks.

What Can Git Do For Me?

Change history.

The who, when and what.

Undo changes made you or others.

Collborate with others across your organisation, group or world.

Open Source community.

How Does Git Work?

First Building Block

repository Simply think of it as a folder that can hold your projects files and folders.

The terminology repo and repository are used interchangeably.

How Does Git Work?

Working With Remote Repositories

$ git clone
Downloads a copy of the repository to your local computer.
$ git remote
Get the remote URL(s) for the repository.

git clone
clone the repository by downloading it to your local computer.
   
   
git remote
Get the remote repository URL(s).

DEMO 1

Working With Remote Repositories

$ git clone https://github.com/psdevopsug/git-fundamentals-demo
Cloning into 'git-fundamentals-demo'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 93 (delta 0), reused 11 (delta 0), pack-reused 81
Unpacking objects: 100% (93/93), done.
$ git remote            # not helpful
origin



$ git remote --verbose  # helpful
origin https://github.com/psdevopsug/git-fundamentals-demo (fetch)
origin https://github.com/psdevopsug/git-fundamentals-demo (push)

How Does Git Work?

The Three Stages Of Git

?
$ git status
Get the status of the repository on the local computer.
$ git diff
Show the differences / changes made.
$ git add
Add changes to staging.
$ git commit
Commit changes from staging.
$ git push
Push changes committed to remote repository.

git status
git add
git commit
git push
Make changes. Stage changes. Commit changes. Push / upload changes.
unstaged staged committed pushed

DEMO 2

The Three Stages Of Git

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working 
  directory)
        modified:   setup.ps1

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
git diff setup.ps1
diff --git a/setup.ps1 b/setup.ps1
index f6eaccb..06bdde5 100644
--- a/setup.ps1
+++ b/setup.ps1
@@ -1 +1 @@
-cd c:\windows
+gci c:\windows
$ git add setup.ps1
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   setup.ps1
git commit --message 'Add setup file'
[master 10ca799] Add setup file
 1 file changed, 1 deletion(-)
git push origin
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 653 bytes | 217.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/psdevopsug/git-fundamentals-demo
   6759a23..f1f3e8b  master -> master

How Does Git Work?

Branching

branch A child branch is a point-in-time snapshot of it’s parent. A branch can exist on it’s own or can be brought back into the parent.

DEMO 3

Branching

  $ git checkout -b fix-alias
Switched to a new branch 'fix-aliases'
# modify the setup.ps1 to remove the aliases
$ git status
On branch fix-aliases
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working 
  directory)
        modified:   setup.ps1
no changes added to commit (use "git add" and/or "git commit -a")
# modify the setup.ps1 to remove the aliases
$ git commit --message 'Removed the alias'
[fix-aliases cef2274] Removed the alias
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git push origin     # we get an error message!
fatal: The current branch fix-aliases has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin fix-aliases
$ git push --set-upstream origin fix-aliases
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 674 bytes | 224.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'fix-aliases' on GitHub by 
        visiting:
remote:      https://github.com/psdevopsug/git-fundamentals-demo/
                pull/new/fix-aliases
remote:
To https://github.com/psdevopsug/git-fundamentals-demo
 * [new branch]      fix-aliases -> fix-aliases
Branch 'fix-aliases' set up to track remote branch 'fix-aliases'
  from 'origin'.

How Does Git Work?

Open Source, Community and Collboration

fork A copy of a repository that keeps a link to the original URL it was copied from.

pull request A request to a project to pull your code in for review and merging. Changes are made on a fork of the project repository and the pull request created from that fork.

DEMO 4

Forking and Pull Requests

The Boring Important Stuff

Rules, Guidelines and Etiquette

Contributing Guidelines

  • CONTRIBUTING.md

  • CONTRIBUTERS.md

  • COMMITTERS.md

See Chocolatey CONTRIBUTING.md file as an example.

Contributor License Agreement

  • A legal document;

  • Ensures you are entitled to contribute the code/documentation/translation to the project;

  • Should there be any kind of legal issue then the project has confirmation that you were permitted to make this contribution.

Etiquette

  • Match the style of the project code or documentation;

  • Project maintainers likely volunteers - manage your expectations;

  • Have respect!

  • Use common sense;

What Other Resources Are There?

#AlwaysLearning

What Have We Learned?

  • What Git is;

  • What tools I need to work with Git;

  • Who is using Git and I can use it for;

  • Fundamental building blocks of Git;

  • Basic commands to work with Git;

  • The three stages of Git;

  • What a pull request is and why we use them;

  • Pull Request process flow;

Congratulations!

You’re now a git too!

Questions?

https://blog.pauby.com
Paul Broadwith
@pauby
github.com/pauby
linkedin.com/in/paulbroadwith

pau.by/talks