Git is a distributed version control system. Every developer maintains a local copy of the main repository and edits and commits to the local copy.
Git workflow
Basic commands
Configuration
# list all configurationsgitconfig--list# config user name & emailgitconfiguser.emailphuong@example.comgitconfiguser.namephuong
Clone & Remote
# clone a repo to local machinegitclonehttps://github.com/phuongnht/myapp.git# remote to multiple remote repo# git remote add [name] [url]gitremoteaddgithubhttps://github.com/phuongnht/myapp.git# list all your remote reposgitremote-v# if specified a name, when push using -u flag# git push -u [name] [branch]gitpush-ugithubmaster
Branch
What is branch?
Pointer to specific commit in your working repo.
Master is the default branch in a git repo.
# create new branchgitcheckout-b<newbranchname># or, but you need to use git checkout to switch to that new branchgitbranch<newbranchname>gitcheckout<yournewbranch># list the branchesgitbranch# to view branch both local & remote, using -a# the remote branch have prefix: remotesgitbranch-asarah (master)$ git branch -a* masterstory/frogs-and-oxremotes/origin/master# delete a branchgitbranch-d<branchname>
Rename branch
# rename a branchgitbranch-m<newname># push new branch namegitpushorigin-u<newname># delete remote branchgitpushorigin--delete<old>
Log
# list changed file with git loggitlog--name-only# display the logs in compact way (1 log per line)gitlog--oneline# list only 3 latest commits alonegitlog-3# view log as graphgitlog--graph--decorate
Revert & Reset
Revert
# undo the last commitgitrevertHEAD
Reset
--soft: keep the history
--hard: so embarrased to keep the historycod
Reflog (undo --hard reset)
# show all refloggitreflog# reset the --hard reset, the hashvalue is where # the HEAD point to before the resetgitreset--hard<hashvalue>
Merge
Fast-Forward and No-FF
There are 2 types of merge in GIT
Feature
Fast-Forward Merge
No-Fast-Forward Merge
Merge type
Simple, Fast
Complex, require merge commit
Branch history
Linear, no commit
Non-Linear, new merge commits
Merging strategy
used when changes are in SYNC
used when changes have Diverged
Command
git merge <branchname>
git merge --no-ff <branchname>
Rebasing
Cherry pick
Use case: when you do not want to merge all the changes from another branch (rebasing), and only want to pick 1 commit from another branch.