<Memo>Git Command Summary

### Git configuration file
* Show local git config, config file is in the current project  
`git config -l`
* Show global git config, config file is in usr/.gitconfig  
`git config - -global - -list`
* Show system git config, config file is in git/config/etc  
`git config - -system - -list`  
*Priority: git config > git config --global > git config --system*




### Git configuration (usually use global config)
`git config - -global user.name “your name”`
`git config - -global user.email “your mail”`




### .gitignore
* You should build .gitignore file before you push a project to GitHub repository  
ref: https://github.com/github/gitignore  
**But if you already pushed, you can run the following codes**  
`git rm -r --cached . `  
`git add .`  
`git commit -m 'update .gitignore'`  




### Local git operation 
* Init git in your project folder. Files are **untracked**.  
`git init`
* Add all files to workspace. Files are **staged**.  
`git add filename`  
`git add .`
* Commit workspace to your local repository. FIles are **unmodify** when they are not modified.   
`git commit -m "your commit message"`






### Connect to GitHub (ssh)
* Generate ssh key pairs, then you can copy the public key and paste in your github ssh keys  
`ssh-keygen -t rsa`  
*Command of copy public key in windows version*  
`clip < ~/.ssh/id_rsa.pub`
* Remove remote origin from a Git repository  
`git remote remove origin`
* Create a new repository on the command line  
`echo "# projectname" >> README.md`  
`git init`  
`git add README.md`  
`git commit -m "first commit"`  
`git branch -M main`  
`git remote add origin ssh_address`  
`git push -u origin main`  
* Push an existing repository from the command line  
`git remote add origin ssh_address`  
`git branch -M main`  
`git push -u origin main`
* Clone from GitHub  
`git clone ssh_address`








### Rollback
* Show status  
`git status`
* Modification which has not been added (files are added)  
`git checkout -- filename`  
`git checkout -- .`
* Pull files which have been added to staging area back to working directory   
`git reset HEAD -- filename`
* Rollback to refered commit version (Just take the first 7 digits of the version number)  
`git log`  
`git reflog`  
`git reset --hard commit_number`  
* Rollback to last version, ^1 means 1 previous to current ver, default is 1  
`git reset HEAD^1`

**git clean is often used in conjunction with git reset --hard. Remember that reset only affects tracked files, so you need clean to delete untracked files. Combining these two commands can make your working directory completely return The status of a specified <commit>.**

* It is a clean preview, telling you which files will be deleted. Remember that he will not actually delete files, just a reminder.  
`git clean -n`
* Delete all files in the current directory that have not been tracked. It will not delete the folders and files specified in the .gitignore file, regardless of whether these files have been tracked.  
`git clean -f`
* Delete files under the specified path that have not been tracked.  
`git clean -f <path>`
* Delete files and folders that have not been tracked in the current directory.  
`git clean -df`
* Delete all files in the current directory that have not been tracked. Regardless of whether they are the folders and files specified in the .gitignore file.  
`git clean -xf`

*git reset --hard and git clean -f,combining them can make your working directory completely fall back to the time of the most recent commit.
git clean is also very useful for newly compiled projects. For example, it can easily delete the .o and .exe files generated after compilation. This is very useful when packaging and releasing a release.*



### Branch
* Show local branch  
`git branch`
* Show remote branch  
`git branch -r`
* Create a new branch  
`git branch branchname`
* Switch branch  
`git checkout branchname`
* Create a new branch and switch to it  
`git checkout -b branchname`
* Merge branch (eg. merge dev to main)  
`git checkout main`  
`git merge dev`
* Delete branch  
`git branch -d branchname`
* Push branch  
`git push origin branchname`



### Conflict
`git pull origin dev`
* Solve the conflict manually
`git push origin dev`
* Hint:
`git pull origin dev`
is equal to
`git fetch origin dev
git merge origin/dev`







### Modification
1）`git diff`：
When there is a change in the work area, the staging area is empty, and the comparison of diff is "the common file between the work area and the repository submitted by the last commit"; when there is a change in the work area, the staging area is not empty, and the comparison of diff is "the work area and Common files in the staging area".


2）`git diff --cached`,
   `git diff --staged`：
Display the addition, deletion and modification of all different files between the staging area (files that have been added but not committed) and the last commit (HEAD) (git diff --cached and git diff --staged have the same effect)

3）`git diff HEAD`：
Display the addition, deletion and modification of all different files between the working directory (tracked but not added files) and staging area (added but not committed files) and the last commit.

3.1）`git diff HEAD~X`,
`git diff HEAD^^^…` (There are X ^ signs, and X is a positive integer):
You can view all the additions, deletions and changes between the last submitted version and the previous X versions of the previous timeline

4）`git diff <branch1> <branch2>` ：
Compare the difference between the contents of the last commit on the two branches

4.1)  `git diff branch1 branch2 --stat`:
Show all files with differences (not detailed, no comparison content)

4.2)  `git diff branch1 branch2`:
Show the detailed differences of all files with differences (more detailed)

4.3)  `git diff branch1 branch2 file_path`:
Display the detailed differences of the specified file (comparison content)

4.4) `git log dev ^master`:
Check the commits in the dev log, but the commits that are not in the master log

4.5) `git log master..dev`:
Check what content is submitted by the log in dev more than the log in master (note that the content listed after the two dots ".." (here, dev) is more submitted)

4.6) `git log dev...master`:
I don’t know who submits more and who submits less. I just want to know what’s different

4.7) `git log --left-right dev...master`:
In the above case, show which branch each commit is on






### Development flow
* Get latest version  
`git checkout dev`  
`git merge main`
* Coding...
* Push the code you modifed to remote repository  
`git push origin dev`
* Develop at home  
`git checkout dev`
`git pull origin dev`
* Coding...  
`git push origin dev`
* Develop in company  
`git pull origin dev`



### Other.Make git records concise
* Merge from the current start to the version number  
`git rebase -i version_num^`
* Find the latest 3 logs to merge from the current  
`git rebase -i HEAD-3`
* Git log graph  
`git log - -graph`  
`git log - -graph - -pretty=format:”%h %s”`
* merge log  
`git checkout dev`  
`git rebase master`  
`git checkout master`  
`git merge dev`  
* conflict when git rebase,solve the conflict manually  
`git add`  
`git rebase - -continue`



### Conflict tool
tool: beyond compare
git config:
git config - -local merge.tool bc3
git config - -local mergetool.path ‘beyondcompare installation path’
git config - -local mergetool.keepBackup false

git mergetool//Automatically start the bc3 software



### Build your git server on linux
* Install git and environment (git is written in c, so you have to prepare c compile env)  
`yum -y install curl curl-devel zlib-devel openssl-devel perl epic expat-devel gettext-devel gcc cc`
* Upload git resource code package and decompress  
`put file-address`  
`tar zxvf package-name`
* Compiling  
`cd git`  
`autoconf`  
`./configure`  
*compile*  
`make`  
*install git*  
`make install`  
*install finished and check git version*  
`git --version`
* Build git account and server  
*the last git is the username*  
`adduser -r -c ‘git version control’ -d /home/git -m git`  
*set password*  
`passwd git`  
*switch to user git*  
`su git`  
*get into home directory of user git*  
`cd ~`  
`mkdir repo1`  
`cd repo1`  
`git init --bare`
* You can push your project to this git server  
`ssh://git@ip-address/home/git/repo1`



### Search in GitHub
in:name inputprojectname
in:description inputdescriptioncontent
in:readme inputreadmecontent
stars:>numofstars
forks:>numofforks
language:codinglanguage
user:authorname
size:>=projectsize
awesome xx
xx tutorial
xx starter/boilerplate
xx sample