Search 

Git

Nam Hoai Nguyen
Nam Hoai Nguyen
Quick references for basic task with git
For easier tasks, just use Smart Git to make things visually. However, it’s paid

Installation & Tools

  • Git GUI for Terminal: lazygit

Setting on Local Environments

1# Info User
2git config user.name "namnh198"
3git config user.email "[email protected]"
4
5# Save token login (Don't need to login again every time we use this user)
6git config credential.helper store
7
8# Don't track file & folder permissions change
9git config core.fileMode false
10
11# Disable pull rebase
12git config pull.rebase false
13
14# Format display
15git config color.ui true
16git config format.pretty oneline 
If you want to configure for all repositories. You can use the option --global

Check the status

1# Check status
2git status
3git status -s # modifed files
1# Git remote list
2git remote -v
Check logs commit:
1# With colors
2git log --online --graph --color --all --decorate
3# --grap: draw text-based branches
4# --decorate: display name and tags
5
6git log -- <file> # Check commit containing <file>
7git log --prep='abc' # Look for commits containing "abc" in their name
8git log <from>..<to> # display commits from <from> to <to>
9
10# Check current HEAD
11git log -1 # Something likes HEAD -> <branch-name>
Check the changes of some file
1git diff <file> 
2git diff <file> > file_name.patch # Save the changes to another file 
3git show <file>
Check the list of files in the last commit
1# Get the last commit id
2git log --format="%H" -n 1
3# list of files
4git diff-tree --no-commit-id --name-only -r <commit_id>

Git ignore

Create a file .gitignore. List folder & file will not track
1# ignore
2.node_modules/
3.vendor/
4.cache
1# Ignore all except var/
2var/*
3# White list
4!var/.gitkeep 
You can use this tool to generate .gitignore file
1# add a file/folder to .gitignore
2echo file.txt >> .gitignore
3echo folder/ >> .gitignore
4
5# add ignore file only your local enviroments
6echo file.txt >> .git/config/exclude
7echo folder/ >> .git/config/exclude

Repository

1# Create Repository
2git init <repo-name>
3
4# Add another GIT remote
5git remote add <remote-name> <repo-link>
1# Clone REPO (using https or ssh)
2git clone <repo-link>
 
P/S: If you use clone with HTTPS, You need to use Personal access tokens instead of Password
1# SWITCH TO ANOTHER BRANCH
2git checkout <branch-name>
1# CREATE BRANCH ON LOCAL
2git branch -m <branch-name>

Pull, Push, Commit, Staged

Staged

1# STAGED
2git add . # add all the file changes
3git add <file-name> # only add <file-name> to the staged
4git add -f <file-name> # Force staged <file-name> although one is ignored
5
6# UNSTAGED
7git reset # everything
8git reset HEAD <file> # only <file>

Commit & Push

1# Make commit (From Staged)
2git commit -m '<commit-message-for-this-commit>'
3git commit --amend # merge staged to previously commit
4# Uncommit 
5git reset --soft HEAD~1 
6
7# Uncommit amend commit
8git reflog # get log id
9git reset --soft <log-id>
1# PUSH TO REMOTE (By default <remote-name> is origin)
2git push <remote-name> <branch-name> # push only <branch-name> to remote <remote-name>
3# PUSH AND RESET REMOTE COMMIT
4git push <remote-name> <branch-name> --force

Pull & Fetch

1# UPDATE REMOTE -> LOCAL
2git pull origin <branch-remote>
1# FETCH THE CHANGE FROM REMOTE
2git fetch

Branch

Create

1# CREATE A BRANCH
2git branch <branch-name>
3
4# CREATE BRANCH BASED `HEAD`
5git checkout -b <branch-name>
6
7# NEW BRANCH BASED ON ANOTHER ONE
8git checkout -b <new-branch> <exit-branch>

Delete

1git branch -d <branch-name>
2git branch -D <branch> # force deleted branch
3
4# DELETE BRANCH THAT MERGED TO main
5git branch --merged main | grep -v "^\* main" | xargs -n 1 -r git branch -d

Merge

Check if there are conflicts before merging?
1# merge without commit -> check the conflicted files
2git merge --no-commit <other-branch>
3
4# list only the name of conflict files
5git diff --name-only --diff-filter=U
6
7# then reset if don't wanna merge
8git reset --hard
1# MERGE <branch> TO CURRENT
2git merge <branch>
3
4# MERGE ONLY SOME FOLDER
5git checkout <branch>
6git checkout <from-branch> folder1\ folder2\
7
8# MERGE commit from ONE BRANCH to CURRENT
9git cherry-pick <commit hash>

Conflict

If there are changes from both local and remote, there will be conflicts! Something likes that
1<<<<<< HEAD
2changes on local
3======
4changes from remote
5>>>>>> somethings is changed
Smart Git or Jetbrains IDE (I used) provide GUI for you fix conflict

Reset & Revert commit

1# GET COMMIT ID
2git log
3
4# RESET A GIT COMMIT
5git reset <option>* <commit-id>
6
7# REVERT A GIT COMMIT
8git revert <commit-id>  

Git Submodules

Git submodule allow you to keep another git repository as subdirectory.
1# "git clone" & "git pull" to automatically update submodules.
2git config --global submodule.recurse true
3
4# add submodule to your repo
5git submodule add <https://github.com/git/submodule> # Ex: namnh198/notes
To stop Fetching submodule... when using git pull → change config submodule.recurse to false
To update all submodules:
1# If this is the 1st time you checkout the repo
2git submodule update --init --recursive
3
4# Update submodules
5git submodule update --recursive --remote
6
7# clone a repo with submodules
8git clone <repo> --recursive
9# or
10git clone <repo>
11git submodule init
12git submodule update
To remove a submodule
1# REMOVE a submodule (suppose that it's in a/)
2# Make a copy (just in case)
30. mv a/submodule a/submodule_tmp
41. git submodule deinit -f -- a/submodule
52. rm -rf .git/modules/a/submodule
63. git rm -f a/submodule
7# Note: a/submodule (no trailing slash)
84. git rm --cached a/submodule

Clone a new GIT provider with SSH

  1. Generated ssh key: ssh-keygen -t rsa -b 4096 -C "[email protected]"
  1. Open and copy “public_key” in ~/.ssh/id_rsa.pub
  1. If you have multiple SSH Key File, you need to identify ssh key constants in ~/.ssh/config
  1. Go to GIT Provider → Setting → SSH Key → Paste “public_key”

Git Aliases

When you run git you typically ‘git’. But it's kind of annoying to type ‘git’ every time, so I set an alias for git as g
  • In fish shell, you can do it with alias g git
  • In zsh or bash it’s alias g = 'git'
Next, I’ll register a github repository with
1g remote [email protected]:/namnh198/namhoainguyen.com.git
Next, I’ve run the Git command many times a day. I want to alias the status command to ‘st’ so you can avoid typing g st instead of g status
The location of the configuration for this is a ~/.gitconfig file, in alias section:
1[alias]
2	st = status
3	co = checkout
4	ci = commit
5	co = commit -a
6	ps = !git push origin $(git rev-parse --abbrev-ref HEAD)"
7	pl = "!git pull origin $(git rev-parse --abbrev-ref HEAD)"
8	br = branch
9	bm = branch --merged
10	bn = branch --no-merged
I have a lot of other aliases, as you can see like checkout, commit, pull, push, branch