OS: Ubuntu 16.04.02 x64
Let’s talk about how to use git to restore your codes in Ubuntu 16.04 .
[TOC]
init, add and commit
First and foremost, cd to your workspace and run:
git init
Then a .git file would be created at that repository. On purpose of adding files to your Git repository, use
git add FILE_NAME1 FILE_NAME2
You can add several files by git add. Moreover, * and . can be used to stand for all files in this repository. You can also use .gitignore to ignore some files.
However, git commit follows git add to truly store your files into Git repository:
git commit -m 'OPERATION NOTE' --author='NAME <EMAIL>'
Some mistakes you made when commit? –amend may help you:
git commit --amend -m 'NEW OPERATION NOTE' --author='NAME <EMAIL>'
After doing all the steps above, you can check your files:
gitk
Now you have learned the basic usage of Git. For more details or professional techniques, go ahead.
git config
There are 3 levels of configuration:
- config file in folder .git in the current workspace
- .gitconfig file in the home directory (–global)
- etc/gitconfig file in the install folder of Git (–system)
Anyway, the priority of the three levels are : 1>2>3. You can check all the configurations of the current workspace and the system/global configuration by:
git config -l
Then the terminal window will print all the working configurations with descent priorities. You can also check the configuration of some level by:
git config --system -l
So how the config file works? For example, if you wanna set the author’s information automatically when commit files to the Git repository, you can write your name and email in the config files:
git config user.name 'NAME'
git config user.email 'EMAIL'
Then the author’s information will be set in level 1. You can also try:
git config --global user.name 'NAME'
to set the author’s information in other levels. However, by doing:
git config --unset user.name
you can unset the configuration.
.gitignore
It is usually the case that some temporary files are not necessary to restore. .gitignore will help you to ignore those kinds of files.
To do that you ought to create a .gitignore file in some folder:
touch .gitignore
Then open the .gitignore file and edit it:
.gitignore
FILE_NAME/FOLDER_NAME
By doing that the file or folder in the .gitignore file would be ignored when you commit the current workspace. Note that every folder may contain its own .gitignore file and will be affected by the .gitignore file in its upper level folder.
In .gitignore files, you can use “#” to start a commentary line, “*” to represent all the letters and “!” to exclude the followed name:
*.txt
# never ignore note.txt
!note.txt
git rm and git reset
When you have done “git add”, chances are that you regret immediately before doing “git commit”. Then how can it be dealt with?
Case 1: If you have NOT done “git commit” after “git init”.
git rm --cached FILE_NAME
“git rm” is used to remove the files in Git repository, Git index or current workspace. If you have done “git add”, the file would be in the Git index and you cannot remove it. Moreover, if the file in Git repository differs from the one in the current workspace, you cannot remove it, either. However, after checking those two states, “git rm” would remove the file in the Git index and current workspace. Anyway, for truly doing that you need to use “git commit” after.
Note that if you really want to remove some file in the current folder and Git repository, you can follow these steps:
rm FILE_NAME
git add -A
git commit
“-A” means “–all”. Git will record the files that do not exist in the Git index and remove these files after “git commit”.
However, if you use
git rm --cached FILE_NAME
the file will be set “untracked” and removed from the Git index. So if this file has not been committed to Git repository, the file would be removed from Git index only.
Case 2: If the file has been committed to Git repository.
In this case, you must use:
git reset HEAD FILE_NAME
“HEAD” is a tag for the newest commit node. You can use
git show HEAD
to see the newest node. “reset” can be viewed as a command to take the Git repository to some node.
git checkout
Do you want to get your files back from Git repository? git checkout may help you.
git checkout TAG/NODE_TAG FILE_NAME1 FILE_NAME2 ...
Note that the correlated file in your folder will be replaced. Meanwhile, if the file just “checked out” differs from the correlated file to be committed, then it would be written in Git index. To avoid that case, execute:
git reset HEAD
after executing “git checkout”. For simplicity, you can use
git checkout FILE_NAME1 FILE_NAME2 ...
to check out the newest version of some files.
git gc
In order to keep the work efficiency of Git, it is recommended to collect and clean some garbage in Git repository after a long time of coding. “git gc” will help you:
git gc --auto
There are alternatives namely “–aggressive” and “–no-prune”. “–auto” is the fast garbage collection choice, while “–aggressive” is much too slow. “–no-prune” means just trimming never cleaning.
git branch
Create new branches
When you execute “git init”, you have create a branch called master. If you would like to create a new branch, run:
git branch BRANCH_NAME commit TAG_NAME
However, if you omit “commit TAG_NAME”, the new branch will grow on the “HEAD” tag, i.e. the latest tag.
Then you can use
git checkout BRANCH_NAME
to switch from any branch to some branch.
git branch --list
will list all the existing branches. Anyway, when you switch to a new branch, Git will check the file lists between the two branches. Then the “new” files in the new branch will be checked out from the Git repository. Meanwhile, files of the “old” branch that do not exist in the new branch will be deleted from the current workspace.
To look through your branches, execute :
gitk --all
Remove a branch
Just run :
git branch -d BRANCH_NAME
In general, a branch to be deleted ought to be merge to another branch. As a result, if you delete the branch that has not been merged to another branch, Git would throw an error. In order to force to delete, use “-D” instead of “-d”.
In addition, if you feel like renaming an existing branch, run:
git branch -m NEW_BRANCH_NAME
git merge
In most cases, branches are certain to be merged together. So if you want to do it, run:
git merge BRANCH_NAME -m 'COMMENTS'
There is problem easy to be neglected: Once the branch to be merged contains anything of the “base” branch, the “merge” operation just moves the latest node of the “base” branch to the latest node of the branch to be merged, which is called fast-forward merge, without adding a new node. To avoid that case, just run :
git merge --no-ff BRANCH_NAME -m 'COMMENTS'
git rebase
There is a case that you want one of your branches “synchronize” with another branch. “rebase” will help you:
git rebase BRANCH_NAME -m 'COMMENTS'
Then the current branch will synchronize with the base branch, just like you have moved the “root” of the current branch to the latest node of the base branch.
If some trouble occurs, use :
git rebase --abort
to abort this process.