What is GitHub?

Installing Git

On MacOS

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew install git

git --version

git version 2.7.0

On Linux

sudo apt-get install git

sudo yum install git

pacman -S git

On Windows

Basic Functionality

How Git Works

Repositories

Commits

Typical Workflow

Select the modified files that we want to submit. Commit them locally. Pull (download) the previous commits that might have been created while we were working. Push (upload) our changes to the repository.

Creating a Git Project

Initialization

cd my-project

git init

Initialized empty Git repository in /Users/gecko/Web/Code/tutorials/git-tutorial/.git/

.git

Creating a Repository

+

github.com/[owner]/[repo-name]

Linking the Repository to Our Git Project

Adding an Origin

git remote <command> <name> <url>

git remote add origin [email protected]:codepicky/git-tutorial.git

.git/config

.git/config

Selecting Files

add

git status

nothing to commit (create/copy files and use "git add" to track)

git status

git status

.

git add .

git add

git status

git status

Creating a Commit

commit

git commit -m 'The message for this commit.'

-m

git commit -m 'Added the readme file.'

[master (root-commit) fc08a39] Added the readme file. 1 file changed, 1 insertion(+) create mode 100644 README.md

Pushing Our New Commit

push

git push

[email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

git remote add origin <URL>

.git/config

~/.ssh

id_rsa.pub

cat ~/.ssh/id_rsa.pub

git push

No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. error: failed to push some refs to '[email protected]:codepicky/git-tutorial.git'

git push -u origin master

Cloning

git clone <repo URL>

cd

Pulling

git pull

git push

Merge Conflicts

To [email protected]:codepicky/git-tutorial.git ! [rejected] master -> master (fetch first) error: failed to push some refs to '[email protected]:codepicky/git-tutorial.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull …') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git pull

remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:codepicky/git-tutorial fc08a39..6bed7de master -> origin/master Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result.

git diff README.md

<<<<<<< HEAD

=======

>>>>>>> 6bed7de86901df48084a4762480e938443849bbc

# Git & GitHub Tutorial Ignore this: Just fixed a merge conflict.

git status

Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md

Branches

As feature branches, meaning that you are able to develop something that might not be done very soon. You can test it out in that separate branch until you consider it ready to get into the mainline. As dev branches, meaning that every dev has their own branch which nobody else messes with. This practice is used to keep things a little more organized in a team.

Creating Branches

git branch <name>

git branch test-branch

test-branch

git switch test-branch

test-branch

README.md

$ rm README.md $ ls $ git add . $ git commit -m 'Removed the Readme file.' [test-branch 2b52e5c] Removed the Readme file. 1 file changed, 2 deletions(-) delete mode 100644 README.md $ git push Everything up-to-date

$ ls $ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. $ ls README.md $ cat README.md # Git & GitHub Tutorial Ignore this: Just fixed a merge conflict.

README.md

Merging Branches

$ git branch add-new-file $ git checkout add-new-file Switched to branch 'add-new-file' $ echo 'This is just a new file.' > new.file $ git add . $ git commit -m 'Added the new file.' [add-new-file 2eaed1c] Added the new file. 1 file changed, 1 insertion(+) create mode 100644 new.file $ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. $ git merge add-new-file Updating d9ed601..2eaed1c Fast-forward new.file | 1 + 1 file changed, 1 insertion(+) create mode 100644 new.file $ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working directory clean $ git push Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 342 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:codepicky/git-tutorial.git d9ed601..2eaed1c master -> master

Pushing a branch

git push -u origin <branch-name>

$ git checkout add-new-file Switched to branch 'add-new-file' $ git push -u origin add-new-file Total 0 (delta 0), reused 0 (delta 0) To [email protected]:codepicky/git-tutorial.git * [new branch] add-new-file -> add-new-file Branch add-new-file set up to track remote branch add-new-file from origin.

Reverting a Commit

revert

git revert <commit hash>

git log

Resetting to a Commit

git reset <commit hash>

git log

reset

git reset --hard <commit hash>

Stashes

.git

git stash save 'Message'

git stash list

:

git stash apply '[email protected]{0}'

git tag -a 'v1.0' -m 'Message.'

-m 'Message'

git tag

git show v1.0

Conclusion