Installation & Tools
- Download and install (to use command lines): git’s homepage.
- Git client tools: Github Desktop (Windows and MacOS), GitKraken (Windows, Linux, MacOS).
- Read
html
files in repository on Github: use this tool.
Rules to be more effective
- Do commit early and often.[ref]
- Do make useful commit messages.
- Create a new branch for every new feature.
- Use
pull
requests to merge code tomaster
.[ref]
Settings on local machine
Set the global information on local:
git config --global user.name "Anh-Thi DINH"
git config --global user.email "dinhanhthi@gmail.com"
Save github account as default (so that we don’t need to log in again every time we use):
git config credential.helper store
# then (for the first time usage)
git pull
If you meet an error like Could not resolve host: github.com
, try this:
git config --global --unset http.proxy
git config --global --unset https.proxy
Formatting the display:
git config color.ui true # add colors to the results
git config format.pretty oneline # disply only one line of each commit
Repositories
Create a repository:
git init <repo-name>
Clone a repository (using HTTPS method)
git clone <repo-link>
Git GUI
Open a Git GUI: git gui
or gitk
(they are different).
Staged & Commits & Push & Pull
Staged
Add the modifications to the staged:
git add * # add all the changes
git add <file-name> # only add the <file-name> to the staged
Unstage the file to current commit (HEAD):
git reset HEAD <file>
Unstaged everthing -retain changes:
git reset
Commit & Push
Make a commit (move from staged to commit):
git commit -m "<comment-for-this-commit>
git commit -a # commit any files
Push the commits to the remote:
git push origin <branch-name> # push only <branch-name>
git push --all origin # push all branches
If you wanna come to a <commit-id>
for testing:
git checkout <commit-id>
# after testing
git checkout <current-branch>
Pull
List all currently configured remote repositories:
git remote -v
Copy a copy from the remote:
git fetch origin <branch-on-remote>
Compare the current branch to this copy:
git diff --stat FETCH_HEAD
Update from remote to local:
git pull origin <branch-on-remote>
Check the status
git status
If one wants some colors:
git log --oneline --graph --color --all --decorate
# --graph: draw text-based branches
# --decorate: display names and tags
Check some commit:
git log -- <file> # check commits containing <file>
git log --prep="abc" # look for commits containing "abc" in their name
git log <from>..<to> # display commints from <from> to <to> (commit's id, branch's name,...)
Branches
Create
Create a new branch (but stay in the current one)
git branch <branch-name>
Create a new branch (and move to this new one)
git checkout -b <branch-name>
Two branches
Change to another branch:
git checkout <branch-name>
List all branches on local:
git branch
List all branches on local and remote:
git branch -a
Comparing two branches:
git diff <source-branch> <compared-branch>
Delete
Delete a local branch:
git branch -d <branch-name>
Delete a remote branch:
git push origin :<branch-name>
# or
git push --delete origin <branch-name>
Merge
Merge <branch>
to current branch
git merge <branch>
Merge a <sub-branch>
to the master
branch and replace the master
branch:
git checkout <sub-branch>
git merge -s ours master
git checkout master
git merge <sub-branch>
Merge a file from <branch-1>
to <branch-2>
:
git checkout <branch-1> # first, go back to <branch-1>
git checkout --patch <branch-2> <file>
# choose "y" if asked
git add <file>
git commit
Others
Add a description (using Vim editor):
git branch --edit-description
In the case you wanna exit Vim, press ESC then type :q
to quit or :wq
to write and quit.
Discard the changes
Discard changes on current directory:
git checkout -- . # for all changes
git checkout -- <file-name> # for a specific file (go back the last commit of this file)
Discard all local changes to all files permanently:
git reset --hard
In the case you want to discard the changes but want to make a save before moving to another branch to test. You can use below line.
git stash
If you wanna get back to the place you saved (and remove it from the stashed list), just go back to the branch you make the save and use
git stash pop
Restore
Restore a file to the last commit:
git checkout -- <file>
Discard all the changes on local and get from the remote:
git fetch origin
git reset --hard origin/master
Erase all commits and back to <commmit-id>
:
git reset --hard <commit-id>
Alias
Normally, we use git branch
. If we wanna use git br
instead, we use an alias:
git config alias.br branch