Alwin Arrasyid
Back-end developer, DyCode
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?
System that records changes to a file or set of files over time.
It allows you to:
There's a lot of good learning resources. Also, git is very well-documented! It is also available in many languages
Git is released under GNU General Public License 2.0
You have freedom to use, share and change.
You clone a repository on a remote server to your local machine and work on it without affecting the remote server's repository.
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
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.
It is a "place" where you can review what are changed before you apply that changes to the repository.
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.
Open git bash (windows)
Terminal for GNU/linux and macOS
git init try-git# 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.gitgit 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
git config --global core.editor vimgit config core.editor vimOr
git config --list --globalgit config --listOr
ls -alYou'll find a directory called .git
It contains all of necessary repository files.
git add index.htmlTrack the new file, first. This will add the index.html to the staging area.
Now commit
git commitYou can use git status command to see the states of your files inside your repository
git statusUse 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 happensecho -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 --hardgit logLet's create a remote repository on github.com
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
# now see the list of remotes
git remote
# or
git remote -v# 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/masterA branch in Git is simply a lightweight movable pointer to one of these com-
mits.
git branch testing
git checkout testing # move to branch "testing"
# equivalent with
git checkout -b testinggit add index.html
git commit -m 'Added email field input with display element'
git checkout mastergit 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 anymoregit checkout testing
git add index.html
git commit -m 'Finished handling email input'git checkout master
git merge testing# resolve the conflicting code.
git add index.html
git commit