Github
Working space Stage area and git local
Installation
For Ubuntu
sudo apt install git
Commands
Note
If first time use github, you must commands below
git config --global user.name "John"
git config --global user.mail "nguyevana@gmail.com"
git config credential.helper store # to store user name and password to your computer
Write access to repository not granted. fatal: unable to access
Visit this link stackoverflow
Initiate git local
git init
Note
If you mistakenly use git init
in wrong the folder, then on Windows, you can invoke
dir /a
to find .git
file. Then delete it with
rmdir /s /q .git
Git ignore
Git will not ignore files/folders that have already been tracked, even if they have been added to .gitignore
You have to remove files/folders from Git’s tracking status, here i will remove folder Debug
:
git rm -r --cached Debug
Then, commit the changes, and Git will ignore the Debug folder according to the .gitignore
.
Add all file to local
git add .
Commit code to local
git commit -m "message"
Link to remote repository
git remote add origin <Url_repo>
Push code to branch
Push code to branch master
of remote repository, later then just use git push
git push -u origin master
Update code from branch to local
Update code from branch master
to local
git pull origin master
Change to another version (version: code committed to git remote)
git checkout <commit_id>
Create a new branch and move to it (-b: create a new branch)
git checkout -b <branch_name>
Merge a branch to master
git merge <branch_name>
List branch
git branch -a
Assumed you committed c1 c2 c3 c4 c5. If you use the command below, then we will return to c2, code in c3 c4 c5 will be deleted (as never exist)
git reset --hard <id_c2>
Force to push code to remote due to difference in commit history (not recommend)
git push -f
Show id commit
git log
Return to a commit in history
git revert <id_commit>
Git clone
If you don’t need the entire commit history, you can perform a shallow clone, which only fetches the latest state of the repository. Use the –depth option:
git clone --recurse-submodules --depth 1 https://github.com/cetic/6lbr.git
Recover code from git repository
If you want to change current code and recover it from git repository
git fetch --all
git reset --hard origin/<branch-name>