Introduction to Git

Whoami

Alwin Arrasyid

Back-end developer, DyCode

What's Git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Version control? What?

Version Control System

System that records changes to a file or set of files over time.

It allows you to:

  • revert files back to a previous state
  • revert the entire project back to a previous state
  • compare changes over time
  • see who last modified the files

 

Why git?

Easy to learn

There's a lot of good learning resources. Also, git is very well-documented! It is also available in many languages

Free and open source

Git is released under GNU General Public License 2.0

You have freedom to use, share and change.

Distributed VCS

You clone a repository on a remote server to your local machine and work on it without affecting the remote server's repository.

Fast

Almost every operations are run locally with almost no network latency. This is a huge advantage over centralized VCS that needs to communicate with a remote server.

 

Also, git is written in C.

 

 

benchmark: https://git-scm.com/about/small-and-fast

Data assurance

Everything in git is check-summed before it is stored and is then referred to by that checksum.

It is impossible to change the contents of any file without git knowing it.

Git uses SHA-1 hash to calculate the checksum.

Staging area

It is a "place" where you can review what are changed before you apply that changes to the repository.  

Branching Model

Git allows and encourages you to have multiple local branches that can be entirely independent of each other.

 

The creation, merging, and deletion of those lines of development takes seconds.

Now what?

Open git bash (windows)

Terminal for GNU/linux and macOS

 

Initialize git repository

git init try-git

Ways of getting a git repository

# will initialize empty git repository

git init awesome-project

# will initialize a git repository inside existing project

cd awesome-project
git init .

# Clone from remote repository

git clone https://github.com/alwint3r/somegitrepository.git

Configure your git

git config user.name "Your Name Here"
git config user.email "youremailhere@domain.com"
git config --global user.name "Your Name Here"
git config --global user.email "youremailhere@domain.com"

Or locally, once you've initialized / cloned a git repository

You can also config your editor for git

git config --global core.editor vim
git config core.editor vim

Or

Checking your configuration

git config --list --global
git config --list

Or

You might notice something...

ls -al

You'll find a directory called .git

It contains all of necessary repository files.

Start making changes!

Commiting changes

git add index.html

Track the new file, first. This will add the index.html to the staging area.

Now commit

git commit

Recording Changes...

Knowing files state

You can use git status command to see the states of your files inside your repository

git status

Use it whenever you add new files, modify one, or committing changes to see what's happening.

echo -e "# Hello World\n\n We are learning git!" > README.md

git status

# See what happens

git add README.md
git status

# See what happens

git commit -m 'Added README.md'
git status

# See what happens
echo -e "\n\nIt is fun, isn't it?" >> README.md

git status

# See what happens

git diff README.md

# We'll see what are changed inside README.md file
# since the last commit.

git add README.md
git commit -m 'Append new line to README.md'
# We don't expect this change

echo -e "\n\nHello" > README.md

# We just accidentally re-write the README.md file!
# How to undo this?

git checkout README.md

# Happy?

# Another mistake, but committed.

echo -e "#Hello World\n" > README.md

git add README.md
git commit -m 'Re-write README.md'

# How to undo this?

git reset HEAD~1 --soft

# or 

git reset HEAD~1 --hard
git log

What we've done so far...

Now, what?

Let's create a remote repository on github.com

Working with remotes...

git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git


# now see the list of remotes

git remote

# or

git remote -v

Working with remotes...

# Push to a remote repository called origin.

git push origin master

# Pull changes from remote repository.

git pull origin master

# is equivalent

git fetch origin master && git merge origin/master

Branching

A branch in Git is simply a lightweight movable pointer to one of these com-
mits.

Creating branch

git branch testing
git checkout testing # move to branch "testing"


# equivalent with

git checkout -b testing

Working on new branch

git add index.html
git commit -m 'Added email field input with display element'


Back to master

git checkout master

Working on another branch

git checkout -b hotfix
git add index.html
git commit -m 'Fixed button click event handler'

# merge back to master
git checkout master
git merge hotfix
git branch -d hotfix # we can delete branch if we don't need it anymore

Back to our working branch

git checkout testing
git add index.html
git commit -m 'Finished handling email input'

Merging, but...

git checkout master
git merge testing

Resolving conflict

# resolve the conflicting code.
git add index.html
git commit

We have learned the basic. What next?

Thank you!

Made with Slides.com