Git Basics

ianmcloughlin.github.io linkedin github

Create a project

~ $ mkdir my-project
~ $ cd my-project/

Add some files

my-project $ touch README.md
my-project $ touch .gitignore
my-project $ ls -al
total 8
drwxr-xr-x  2 ian ian 4096 Apr 30 14:45 .
drwxr-xr-x 13 ian ian 4096 Apr 30 14:45 ..
-rw-r--r--  1 ian ian    0 Apr 30 14:45 .gitignore
-rw-r--r--  1 ian ian    0 Apr 30 14:45 README.md

Make it a repository

my-project $ git init
Initialized empty Git repository in /home/ian/my-project/.git/
my-project $ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        README.md

nothing added to commit but untracked files present (use "git add" to track)

Add to the repository

my-project $ git add .
my-project $ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   README.md

Commit

my-project $ git log
fatal: your current branch 'master' does not have any commits yet
my-project $ git commit -m "My first commit"
[master (root-commit) 6f7b9bb] My first commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 README.md
my-project $ git log
commit 6f7b9bb50f5b55db52cae999fcf350c5a67e427e (HEAD -> master)
Author: Ian McLoughlin <mcloughlin.ian@gmail.com>
Date:   Thu Apr 30 14:46:14 2020 +0100

    My first commit
my-project $

Ignore

my-project $ echo "*.backup" >> .gitignore
my-project $ more .gitignore
*.backup
my-project $ touch yesentence.txt
my-project $ echo "Summer birds tweeting -" >> yesentence.txt
my-project $ echo "Work together to make it" >> yesentence.txt
my-project $ echo "Harmony as one." >> yesentence.txt
my-project $ more yesentence.txt
Summer birds tweeting -
Work together to make it
Harmony as one.
my-project $ ls -al
total 20
drwxr-xr-x  3 ian ian 4096 Apr 30 15:04 .
drwxr-xr-x 13 ian ian 4096 Apr 30 14:45 ..
drwxr-xr-x  8 ian ian 4096 Apr 30 14:46 .git
-rw-r--r--  1 ian ian    9 Apr 30 14:47 .gitignore
-rw-r--r--  1 ian ian    0 Apr 30 14:45 README.md
-rw-r--r--  1 ian ian   65 Apr 30 15:05 yesentence.txt
my-project $ cp yesentence.txt yesentence.txt.backup
my-project $ more yesentence.txt
Summer birds tweeting -
Work together to make it
Harmony as one.
my-project $ more yesentence.txt.backup
Summer birds tweeting -
Work together to make it
Harmony as one.
my-project $ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        yesentence.txt

no changes added to commit (use "git add" and/or "git commit -a")
my-project $ git add .
my-project $ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   yesentence.txt

my-project $ git commit -m "Added haiku"
[master 94f949a] Added haiku
 2 files changed, 4 insertions(+)
 create mode 100644 yesentence.txt
my-project $ git log
commit 94f949a5c9597238c769b65f35f0b3def68d2fb7 (HEAD -> master)
Author: Ian McLoughlin <mcloughlin.ian@gmail.com>
Date:   Thu Apr 30 15:06:08 2020 +0100

    Added haiku

commit 6f7b9bb50f5b55db52cae999fcf350c5a67e427e
Author: Ian McLoughlin <mcloughlin.ian@gmail.com>
Date:   Thu Apr 30 14:46:14 2020 +0100

    My first commit
my-project $

Remotes

Created a new blank project in github: https://github.com/new

my-project $ git remote add origin https://github.com/ianmcloughlin/my-project
my-project $ git remote -v
origin  https://github.com/ianmcloughlin/my-project (fetch)
origin  https://github.com/ianmcloughlin/my-project (push)
my-project $ git push -u origin master
Username for 'https://github.com': ianmcloughlin
Password for 'https://ianmcloughlin@github.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 610 bytes | 203.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/ianmcloughlin/my-project
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
my-project $ git pull
Already up to date.
my-project $ ls -al
total 24
drwxr-xr-x  3 ian ian 4096 Apr 30 15:05 .
drwxr-xr-x 13 ian ian 4096 Apr 30 15:09 ..
drwxr-xr-x  8 ian ian 4096 Apr 30 15:09 .git
-rw-r--r--  1 ian ian    9 Apr 30 14:47 .gitignore
-rw-r--r--  1 ian ian    0 Apr 30 14:45 README.md
-rw-r--r--  1 ian ian   65 Apr 30 15:05 yesentence.txt
-rw-r--r--  1 ian ian   65 Apr 30 15:05 yesentence.txt.backup
my-project $ touch another-file.txt
my-project $ git add .
my-project $ git commit -m "A third commit"
[master 1676b0e] A third commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 another-file.txt
my-project $ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 248 bytes | 248.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/ianmcloughlin/my-project
   94f949a..1676b0e  master -> master
my-project $