Git Commands Guide
Post By kanra
Blogs Git Commands Guide

Setting Up Git

 

git config --list – List all current Git configurations

git config --global user.email – Check user email

git config --global user.name – Check username

git config --global user.name "myname" – Change username, for all repo

git config --global user.email "email@example.com" – Change user email, for all repo

git config user.<email |name> "email@example.com" – For only current repo

git config --global core.autocrlf <true | input | false> – Configure line ending of file

 

Line EndingIn Windows, if set it to true, then Git will auto-convert CRLF line endings (Windows) into LF (Linux) when you add a file to the index and auto-convert back to CRLF on workspace. If set it to input, then Git will Only convert CRLF to LF when commit. If you set it to false, Git will not do conversion.
https://stackoverflow.com/questions/5834014/lf-will-be-replaced-by-crlf-in-git-what-is-that-and-is-it-important

 

See Information in Git

 

git show – Show some useful info in local git

git status – Show branch, working status

git ls-files . --ignored --exclude-standard --others – List ignored files

git ls-files . --exclude-standard --others – List untracked files

 

Changes Between Commits (file changes)

git diff <files> – show what have changed for modified files that haven’t been staged (modified files in git status)

git diff --stat <files> – show statistics of files change

$ git diff --stat backup.sql
 backup.sql | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

git diff origin/master..HEAD – Show change detail of commits for unpushed (Unsynchronized Changes) commits

 

Commit History

git log – List commits made in that repository in reverse chronological order

git log -p -2 – List commits and shows the difference (the patch output) introduced in each commit. Limit the number of log entries displayed, such as -2 will show only the last two entries

git log --stat – See some abbreviated stats for each commit

git log origin/master..HEAD – Show commit message for unpushed (Unsynchronized Changes) commits

 

 

Remote’s Repository

 

git remote -v – Check remote repo this git project associate with

git remote rm origin – Remove local git project origin (then can add a remote origin later)

git remote add origin https://github.com/USERNAME/REPOSITORY.git – Add remote repo in Github to the origin of this local Git project

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git – To Http

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git – To SSH

 

Note…origin is the name of default current branch’s remote (a repo can have many remote repositories).

 

Working with Git

 

git status – Show status of current environment

git add <file | . > – Add a modified file or all files (using .) to the stage area

git reset -q HEAD [files] – Remove all staged files or specific files from stage area

git commit -m "a message" – Commit the changes of staging files to local repository

git stash – Discard all local changes, but save them for possible re-use later

git checkout -- <file> – Discarding local changes (permanently) to a file

git checkout -- . – Discarding local changes (permanently) to all files

git clean -df – Remove all untracked folders/files from the repo

git rm -r --cached <file or folder name> – Remove file or folder from being tracking

git reset --hard – Discard all local changes to all files permanently

Rollback to a previous git comment (need a commit -id)

git reset --hard <old-commit-id>
git reset --hard 57e86ca            # rollback to commit 57e86ca

# then push to remote with -force to overwrite
git push -f <remote-name> <branch-name>

To clear all updates made to the current repo:

git checkout -- .
git clean -df

 

Note….gitignore will prevent untracked files from being added (without using git add -f) to the set of files tracked by git, however git will continue to track any files that are already being tracked.

 

Cloning Repository

 

git clone git@github.com:karanokara/Hello-world.git – Clone from GitHub to current folder, using SSH key, create a new folder as the repo name as ‘Hello-world’

git clone user@123.123.0.123:/Hello-world.git hello – Clone from remote host, using SSH key, to current user’s folder ‘hello’, or create it

git clone https://github.com/karanokara/repo.git – Clone from GitHub, using https, will prompt for username and password

 

 

Pull Repository

 

git pull <remote-name> <branch-name> – Pull the latest commit from origin remote repository, usually from “master” branch

git pull github new-feature – Pull the latest commit from “github” remote repository, from “new-feature” branch

 

 

Push Repository

 

git push [-u: enter account credential] <remote-name> <branch-name> – Push local Committed updates to the origin remote repository, branch name may be “master” or others

 

 

Working with Branches

 

git fetch -a – Fetch remote branches to local

git branch [-a: local & remote; -r: remote] – Listing branches (local, -a for local & remote)

git checkout <branch-name> – Switching branch, “branch-name” or “origin/<branch-name>”

git checkout -b <branch-name> – Create new branch base on current branch

 

There are 3 different branches: Local, Local remote-tracking, Remote origin.

git branch -d <branch-name> – Delete a local branch

git branch -d -r <remote-name/branch-name> – Delete a remote-tracking branches (need to push to delete its remote branch)

git push origin -d <branch_name> – Delete a remote branch (and also its remote-tracking branch)

Removes/delete (Prunes) all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote

git remote prune origin
# or
git fetch origin --prune
git fetch origin -p

 

 

Generating SSH key and Adding to the ssh-agent

 

After adding ssh-rsa key to Github, no need to type password when there is a push update to a remote repository.
ssh-keygen -t rsa [-f <filename>] -b 4096 -C "your_email@example.com" – Generating a new SSH key ( whatever email, doesn’t matter, e.g. root@toshiba )

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again] – Hit enter without passphrase here

eval $(ssh-agent -s) – Ensure the ssh-agent is running with output as Agent pid 12345

ssh-add ~/.ssh/id_rsa – Add generated SSH private key to the ssh-agent.

Finally, Copy content of cat ~/.ssh/id_rsa.pub, the public ssh-rsa key to the Github account.

 

For automatically adding private key to the ssh agent every time login via SSH:

Config the SSH startup script sudo vim ~/.bashrc, add to the end of file:

eval $(ssh-agent -s)             # Open a ssh agent
ssh-add /home/user/.ssh/id_rsa    # Add pri key to agent 

To delete the invalid key such as after renewing a key:

ssh-keygen -R <hostname/ip>

 

 

Note…– If having problem git@github.com: Permission denied (publickey)
check if doing git pull or git push need to use sudo, if so, do sudo chmod 777 -R .git to avoid using sudo on git pull.
– The keys that generate need to be named id_rsa (private) and id_rsa.pub (public) to get communicate with Github.
– Try clearing known_host and then pull a new repo to update the known_host file


AUTHOR : kanra
EMAIL : karawakara@gmail.com
Working on Networking, Web Development, Software Development, Server-side deployment. Having fun on doing experiments on new technologies.