GIT Jumpstart Tutorial For Beginners
Introduction
This is an incomplete tutorial for beginners. The tutorial is a quick jumpstart for user who want to learn GIT. This tutorial just cover the very basic things about GIT, it is incomplete.
SVN and CVS
Before you start with GIT, you should know a little bit about CVS or SVN. Basic command like check in (commit) and check out.
$ svn list file:///usr/local/svn/hello
branches/
tags/
trunk/
The trunk is the main repository.
$ svn co file:///usr/local/svn/hello/trunk hello
…
A hello/README
Note: co is the short form of checkout.
$ svn commit -m "add in description for beginners" README
$ cd hello
$ rm NEWS
$ svn update NEWS (retrieve back news)
The difference between SVN and CVS
CVS. Each file has its own version. SVN, few files may share the same revision number. SVN use atomic commit, eg. commit a few files together, either all success or fail.
GIT vs CVS/SVN
Git is a distributed version-control system, it has its own local repository (can do offline commit/check in). Synchronize with other repositories when the network is available.
Git has 3 stages (server/repository, local/working/untracked and staged/tracked/indexed). CVS and SVN have 2 stages, local/working and the remote repository.
GIT Tutorial
GIT configuration
$ git --version
git version 1.9.5 (Apple Git-50.3) # MacOS Mavericks 10.9
git version 2.20.1 (Apple Git-117) # macOS Mojave 10.14
example:
$ git config --global user.name "fuyichin" (commit with this name)
$ git config --global user.email fuyichin@gmail.com
$ git config --list (shows config)
user.email=fuyichin@gmail.com
user.name=fuyichin
$ git config --global core.editor "vi"
Git init
Create a (local) repository.$ mkdir hello; cd hello
hello $ git init
create a .git repository at the local directory.
Git status
Create an empty file README.
hello$ touch README
$ git status README
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
Git add
Remember there are 3 stages 1. local/untracked 2. tracked/staged/indexed 3. repository in Git. Add files move the file from untracked to tracked (or indexed). Only tracked/indexed files will be committed.
$ git add README; git status README
Changes to be committed:
new file: README
Git commit
$ git commit -m "initial commit" # this commit is a empty README
1 file changed, 0 insertions(+), 0 deletions(-)
Modified the file (the empty file) wit content hello world:
$ echo "Hello, world." > README
$ git status README
modified: README
$ git add README # tracked/indexed hello world
(local change to Hello, John.)
$ git status README
Changes to be committed:
modified: README (Hello, world. tracked)
Changes to the local (untracked) file to "Hello, John":
$ echo "Hello, John." > README
$ git status README
Changes to be committed:
modified: README (Hello, world. tracked)
Changes not staged for commit:
modified: README (Hello, John. local copy, untracked)
When I commit, "Hello, world." will be committed, not "Hello, John". To commit "Hello, John", use git add <file>, this will make local copy to tracked/staged/indexed copy.
$ git commit -m "hello world"
To commit "Hello, John"
$ git add README # add to tracked/staged/indexed
$ git commit -m "hello john"
Git edit last commit
Use Git amend
$ git commit --amend
Git use the EDITOR environment variable or the editor set in 'git config core.editor'.
Git log
Show history
$ git log
$ git log --oneline # easier to read
09d72a7 (HEAD -> master) hello john (amended)
3a0fa58 hello world
7522dee initial commit
What is HEAD and master? HEAD refers to the last commit, master is the master branch. The commit code in-front could be different.
Remove commit
Git commit can be removed or reset before synchronizing to another repository.
09d72a7 (HEAD -> master) hello john # HEAD
3a0fa58 hello world (amended) # HEAD~1, or HEAD^
7522dee initial commit # HEADE~2, or HEADE^^
$ git reset --hard HEAD~1 # 1 commit before HEAD, or
$ git reset --hard 3a0fa58 # this is the commit code
SVN uses revision number, git use commit code as the version identifier.
$ git log --oneline
# 09d72a7 (HEAD -> master) hello john # not displayed
3a0fa58 hello world (amended)
7522dee initial commit
You still can reset back to the original HEAD
$ git reset --hard 09d72a7
$ git log --oneline
09d72a7 (HEAD -> master) hello john
3a0fa58 hello world (amended)
7522dee initial commit
Git configuration: .gitignore
Ignore all the object files, binary files.
$ cat .gitignore
*.o
*.class
a.out
when $ git add * or $ git status, those files listed in .gitignore will be ignored.
Git checkout (files)
Git checkout likes svn update, to retrieve back from the previous copy. The previous copy is either the tracked copy, or from the repository.
$ git checkout [-f] <filename> # or git checkout -- <filename>, or
$ git checkout HEAD <filename> # update from last commit
Note: switch branch use git checkout as well.
Git clone
When you retrieve (a remote) repository, eg. from a server, use git clone. It makes a copy of the repository, including all the history changes to the local working directory.
format: git clone [--depth nn] <repository>
If the git repository is too big, just download the last or last few commit, $ git clone --depth 1 <repository>
Git pull/push
Any commit of changes are updated to the local repository. Use git push to update to the remote repositories. Use git pull to retrieve changes from remote repositories.
Show remote
$ git remote [-v]
Push to remote
$ git push [ origin master]
Push master branch to remote origin. (origin is remote, master is branch)
Working with branch
Working with Git branch is light-weight (compare with CVS, SVN) and easy.
Create branch
Checkout with -b
$ git checkout -b branch1
(This will create branch name branch1 and switch to branch1)
$ git branch [--list?]
Master
* branch1
To switch between master and <branch1>
$ git checkout master
*Master
branch1
$ git checkout branch1
Merge to main branch master.
$ git checkout master
Switched to branch 'master'
Merge
$ git merge branch1
You can delete the branch after it merges to master.
$ git branch -d branch1
This is a safe delete, if the branch code is not merged, git will give a warning. Force delete use -D.