Git commands

unpublished draft
Git

General#

Global setting
# Get to see global git config
git config --list

# Set some global config
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Log
git log --graph -- decorate -- oneline
# --graph: give the tree graphic
# --decorate: show the current position of head
# --oneline: format things in one line, beautiful

git log HEAD~5..HEAD^ --oneline
/*
  Show the the commits from 5 commits of the current HEAD to the parent of the current head.
  Example: 1 2 3 4 5 6 7 8 9 10
  => current head is commit 10
  => the result is: 5 6 7 8 9
*/
Submodule
git submodule add [repository link]
# Add Repository to current repo module.

git clone --recurse-submodules [The repo Url]
# Cloning the project including sub module

git submodule update --init --recursive
# When submodule lose its repo, init and re-udpate it (remember to stash whatever u need)
Git patch
# How many commit we want to create patch file
git format-patch -1

# Apply them on destination (only apply change, not commit)
git apply [fileName]
Git fetch branches not showing up
# Get to see git config on remote (depend on the name of remote, might be origin, might be external for example)
git config --get remote.origin.fetch
# The result could be +refs/heads/develop:refs/remotes/origin/develop

# Assign new value to the specific remote as wildcard, which will match 1-1 with remote upstream
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

# Check the config again
git config --get remote.origin.fetch
# The result should be +refs/heads/*:refs/remotes/origin/*
Log
git log --graph -- decorate -- oneline
# --graph: give the tree graphic
# --decorate: show the current position of head
# --oneline: format things in one line, beautiful

git log HEAD~5..HEAD^ --oneline
/*
  Show the the commits from 5 commits of the current HEAD to the parent of the current head.
  Example: 1 2 3 4 5 6 7 8 9 10
  => current head is commit 10
  => the result is: 5 6 7 8 9
*/

SSH setup#

Windows#

SSH
# All command was run on git bash

# Generate key pair
ssh-keygen -t ed25519 -C "your_email@example.com"
# -t: ssh key type
# -C: comment

# ===============================
# Checking if ssh-agent is running
eval "$(ssh-agent -s)"

# ===============================
# Add host public key on local machine (aka Generate knows-host)
ssh -T git@github.com
# -T: test

# ===============================
# Example config file:
# HostName property must correct.
# ---------File Content----------
# Host github.com
HostName github.com
 IdentityFile ~/.ssh/[your_ssh_private_key_file_name]

Ubuntu#

Detailed here

  1. Ensure openssh-client was installed, check by ssh -V
  2. Create user's ssh directory and a sub directory where your dedicated GitHub ssh key will be stored
mkdir -p ~/.ssh/github
chmod 700 ~/.ssh ~/.ssh/github
  1. Generate the SSH key (the output key will have octal permissions 600):
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github/id_organizationOne
  1. Copy the content of the file id_organizationOne.pub and put it on server ssh key
  2. Create the ~/.ssh/config file, if it doesn't already exist:
touch ~/.ssh/config
chmod 600 ~/.ssh/config

Edit the config file and add the following entry for the new SSH key:

Host github.com
    IdentityFile ~/.ssh/github/id_rsa
  1. Test the setup. Use the following command:
ssh -T git@github.com

Khanh Nguyen

Web developer & .Net lover