Code Version Control (Git)
By Ren Sothearin
Prerequisite
● Basic Linux Command (Optional)
● Familiar with Command Prompt/ Terminal
● Have GitLab/ GitHub/ Bitbucket Account
● Commitment to solve the conflict ^^
IDE
● (Windows) : required to use GitBash.
○ Download link: https://git-scm.com/downloads
● (Mac) : can use Mac Terminal, (optional) install “ohmyz.sh”
○ Go to terminal and add “$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-
zsh/master/tools/install.sh)"”
What is Git?
Git (/ɡɪt/) is a version control system (VCS) for tracking changes in computer files and
coordinating work on those files among multiple people. It is primarily used for
software development, but it can be used to keep track of changes in any files. As a
distributed revision control system it is aimed at speed, data integrity, and support for
distributed, non-linear workflows.
When was it invented?
Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with
other kernel developers contributing to its initial development. Its current maintainer
since 2005 is Junio Hamano.
What are the specialities in Git?
As with most other distributed version control systems, and unlike most client–server
systems, every Git directory on every computer is a full-fledged repository with
complete history and full version tracking abilities, independent of network access or a
central server.
Note: Git is free software distributed under the terms of the GNU General Public
License version 2
Git Web Interface
1. Famous
a. GitHub
b. GitLab
c. Bitbucket
2. Not familiar
Gitweb, Wit, Cgit, Gitorious,
Trac, Kallithea, Stash,
Springloops, Bonobo Git
Server
How to use
--distributed-even-if-your-workflow-isnt
Why GitLab?
https://about.gitlab.com/
● Free unlimited (Private)
repositories
● Free unlimited collaborators
● Friendly Interface
Basic Keyword
master - the repository’s main branch. Depending on the work flow it is the one people
work on or the one where the integration happens
clone - copies an existing git repository, normally from some remote location to your
local environment.
commit - submitting files to the repository (the local one); in other VCS it is often
referred to as “checkin”
fetch or pull - is like “update” or “get latest” in other VCS. The difference between fetch
and pull is that pull combines both, fetching the latest code from a remote repo as
Basic Keyword
push - is used to submit the code to a remote repository
remote - these are “remote” locations of your repository, normally on some central
server.
head - is a reference to the node to which our working space of the repository currently
points.
branch - is just like in other VCS with the difference that a branch in Git is actually
nothing more special than a particular label on a given node. It is not a physical copy
of the files as in other popular VCS.
Git Configuration
$ git config --global user.name "username": to config username with the git remote
repository
$ git config --global user.email example@example.com: to config username and email
with the git remote repository
$ git init : is used to initialize git in your local repository
Cloning project from a remote repository
$ git clone <remote_repo> : is used to clone the remote repository into local (clone
means copy)
Example: $ git clone git@gitlab.com:user/myproject.git
$ git clone -b <branch> <remote_repo> : is used to clone from a specific branch at the
remote repository into local
Example: $ git clone -b feature git@gitlab.com:user/myproject.git
This example means that we clone from branch “feature” at the remote
repository name “myproject”.
Adding file to a remote repository
$ git add <file_name> : is used to add the specific file name to the remote repository
$ git add . : is used to add all the changed file to the remote repository (Git will take a
snapshot of the contents of all files under the current directory (note the .))
$ git add -a/ $ git add -A : it will find new files as well as staging modified content and
removing files that are no longer in the working tree.
$ git add --all : the same as git add -a
$ git add * : the same as git add .
Committing file to a remote repository
$ git commit -m “message” : is used to commit the changes to head (but not yet to the
remote repository)
$ git commit -a : Commit any files you've added with git add, and also commit any files
you've changed since then
Pushing files to a remote repository
$ git push -u origin <branch_name> : is used to push the branch to your remote
repository, so others can use it.
Example: git push -u origin master
● $ git push origin <branch_name> : is used to push the branch to your remote
repository, so others can use it.
Example: git push origin master
● $ git push --all origin : is used to push all branches to your remote repository
Pulling files from a remote repository
$ git pull : is used to pull the fast-forwards codes, files from the our own branch
Example: myproject (master) $ git pull
This means that we pull our own code from the remote repo into local
● $ git pull origin <branch_name> : is used to pull the fast-forwards codes, files from
the remote branch into our local
Example: myproject (master) $ git pull origin feature
Meaning, stands on “master’ branch and pull from “feature” branch
Git branch
$ git branch “branch_name” : is used to create a new branch name, after create we still
stand on the current branch
Example: myproject (master) $ git branch “feature” : means that we stands on
master and create a new branch called “feature”
● $ git checkout -b “branch_name” : is used to checkout and create a new branch
name, after create we change the current standing branch to the new branch
Example: myproject (master) $ git checkout -b “feature”
myproject (feature) $
Git branch
$ git branch : List all the branches in your repo, and also tell you what branch you're
currently in
Example: myproject (master) $ git branch
feature
*master
● $ git checkout <branch_name> : Switch from one branch to another branch
Example: myproject (master) $ git checkout feature
Git branch
$ git branch -d <branch_name> : is used to delete branch in the remote repo
Merging branch together
$ git merge <branch_name> : is used to merge a different branch into your active
branch.
Example: myproject (master) $ git merge feature
Means that we are merging “feature” into “master”
● $ git diff <source branch> <target branch> : is used to preview change on branch
before merge
Git fetch
$ git fetch : is used to fetch the changes in other branch into your current active branch
$ git fetch <branch_name> : is used to fetch the changes in the specific branch into your
current active branch
Undo local change
$ git checkout --<file_name> : is used to undo your local change in the file. If you mess
up, you can replace the changes in your working tree with the last content in head.
Changes already added to the index, as well as new files, will be kept.
Instead, to drop all your local changes and commits, fetch the latest history from the
server and point your local master branch at it, do this
$ git fetch origin
$ git reset --hard origin/master
Git log
$ git log : is used to see the changing history
$ git log -p : is used to see complete diffs at each step
Stash local change
$ git stash : is used to stash the local change that you have made in your local
repository. When you stash your code, your local change will remove and your tree
will look the same as what you have committed. After you stash dont worry that you
will lose your code, it just put somewhere in your computer.
$ git stash pop : is used to get what you stash back into your local repository. You can
get it back, but if you “git status” you won’t see any change file.
$ git reset HEAD <file> : is used get what you stash back after “stash pop”
Contact
Sothearin Ren
ren.sothearin@gmail.com
https://gitlab.com/Sothearin

Understanding about git

  • 1.
    Code Version Control(Git) By Ren Sothearin
  • 2.
    Prerequisite ● Basic LinuxCommand (Optional) ● Familiar with Command Prompt/ Terminal ● Have GitLab/ GitHub/ Bitbucket Account ● Commitment to solve the conflict ^^
  • 3.
    IDE ● (Windows) :required to use GitBash. ○ Download link: https://git-scm.com/downloads ● (Mac) : can use Mac Terminal, (optional) install “ohmyz.sh” ○ Go to terminal and add “$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my- zsh/master/tools/install.sh)"”
  • 4.
    What is Git? Git(/ɡɪt/) is a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for software development, but it can be used to keep track of changes in any files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.
  • 5.
    When was itinvented? Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. Its current maintainer since 2005 is Junio Hamano.
  • 6.
    What are thespecialities in Git? As with most other distributed version control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version tracking abilities, independent of network access or a central server. Note: Git is free software distributed under the terms of the GNU General Public License version 2
  • 7.
    Git Web Interface 1.Famous a. GitHub b. GitLab c. Bitbucket 2. Not familiar Gitweb, Wit, Cgit, Gitorious, Trac, Kallithea, Stash, Springloops, Bonobo Git Server
  • 9.
  • 10.
    Why GitLab? https://about.gitlab.com/ ● Freeunlimited (Private) repositories ● Free unlimited collaborators ● Friendly Interface
  • 11.
    Basic Keyword master -the repository’s main branch. Depending on the work flow it is the one people work on or the one where the integration happens clone - copies an existing git repository, normally from some remote location to your local environment. commit - submitting files to the repository (the local one); in other VCS it is often referred to as “checkin” fetch or pull - is like “update” or “get latest” in other VCS. The difference between fetch and pull is that pull combines both, fetching the latest code from a remote repo as
  • 12.
    Basic Keyword push -is used to submit the code to a remote repository remote - these are “remote” locations of your repository, normally on some central server. head - is a reference to the node to which our working space of the repository currently points. branch - is just like in other VCS with the difference that a branch in Git is actually nothing more special than a particular label on a given node. It is not a physical copy of the files as in other popular VCS.
  • 13.
    Git Configuration $ gitconfig --global user.name "username": to config username with the git remote repository $ git config --global user.email [email protected]: to config username and email with the git remote repository $ git init : is used to initialize git in your local repository
  • 14.
    Cloning project froma remote repository $ git clone <remote_repo> : is used to clone the remote repository into local (clone means copy) Example: $ git clone [email protected]:user/myproject.git $ git clone -b <branch> <remote_repo> : is used to clone from a specific branch at the remote repository into local Example: $ git clone -b feature [email protected]:user/myproject.git This example means that we clone from branch “feature” at the remote repository name “myproject”.
  • 15.
    Adding file toa remote repository $ git add <file_name> : is used to add the specific file name to the remote repository $ git add . : is used to add all the changed file to the remote repository (Git will take a snapshot of the contents of all files under the current directory (note the .)) $ git add -a/ $ git add -A : it will find new files as well as staging modified content and removing files that are no longer in the working tree. $ git add --all : the same as git add -a $ git add * : the same as git add .
  • 16.
    Committing file toa remote repository $ git commit -m “message” : is used to commit the changes to head (but not yet to the remote repository) $ git commit -a : Commit any files you've added with git add, and also commit any files you've changed since then
  • 17.
    Pushing files toa remote repository $ git push -u origin <branch_name> : is used to push the branch to your remote repository, so others can use it. Example: git push -u origin master ● $ git push origin <branch_name> : is used to push the branch to your remote repository, so others can use it. Example: git push origin master ● $ git push --all origin : is used to push all branches to your remote repository
  • 18.
    Pulling files froma remote repository $ git pull : is used to pull the fast-forwards codes, files from the our own branch Example: myproject (master) $ git pull This means that we pull our own code from the remote repo into local ● $ git pull origin <branch_name> : is used to pull the fast-forwards codes, files from the remote branch into our local Example: myproject (master) $ git pull origin feature Meaning, stands on “master’ branch and pull from “feature” branch
  • 19.
    Git branch $ gitbranch “branch_name” : is used to create a new branch name, after create we still stand on the current branch Example: myproject (master) $ git branch “feature” : means that we stands on master and create a new branch called “feature” ● $ git checkout -b “branch_name” : is used to checkout and create a new branch name, after create we change the current standing branch to the new branch Example: myproject (master) $ git checkout -b “feature” myproject (feature) $
  • 20.
    Git branch $ gitbranch : List all the branches in your repo, and also tell you what branch you're currently in Example: myproject (master) $ git branch feature *master ● $ git checkout <branch_name> : Switch from one branch to another branch Example: myproject (master) $ git checkout feature
  • 21.
    Git branch $ gitbranch -d <branch_name> : is used to delete branch in the remote repo
  • 22.
    Merging branch together $git merge <branch_name> : is used to merge a different branch into your active branch. Example: myproject (master) $ git merge feature Means that we are merging “feature” into “master” ● $ git diff <source branch> <target branch> : is used to preview change on branch before merge
  • 23.
    Git fetch $ gitfetch : is used to fetch the changes in other branch into your current active branch $ git fetch <branch_name> : is used to fetch the changes in the specific branch into your current active branch
  • 24.
    Undo local change $git checkout --<file_name> : is used to undo your local change in the file. If you mess up, you can replace the changes in your working tree with the last content in head. Changes already added to the index, as well as new files, will be kept. Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this $ git fetch origin $ git reset --hard origin/master
  • 25.
    Git log $ gitlog : is used to see the changing history $ git log -p : is used to see complete diffs at each step
  • 26.
    Stash local change $git stash : is used to stash the local change that you have made in your local repository. When you stash your code, your local change will remove and your tree will look the same as what you have committed. After you stash dont worry that you will lose your code, it just put somewhere in your computer. $ git stash pop : is used to get what you stash back into your local repository. You can get it back, but if you “git status” you won’t see any change file. $ git reset HEAD <file> : is used get what you stash back after “stash pop”
  • 27.