Git — 10 Most frequently Use Command Line

Long Do
3 min readDec 29, 2020

--

Git — the stupid content tracker

1. Git Config

Get and set repository or global options

git config --global user.name [user_name]
git config --global user.email [user_email]
Example:
git config --global user.name "leegroup"
git config --global user.email "leegroup@gmail.com"

2. Git Init

Create an empty Git repository or reinitialize an existing one

git init [-b <branch-name> | --initial-branch=<branch-name>]
Default branch: master
Example
git init
git init -b develop // Since git version 2.28.0
git init --initial-branch=develop // Since git version 2.28.0

3. Git Clone

Clone a repository into a new directory

git clone [url] [directory_name]// clone current repo
git clone -l [-sn] . ../[clone_directory_name]
Example:
git clone https://github.com/googlesamples/mlkit.git machine_learning_kit
git clone -l -n . ../repo-copy

4. Git Branch

List, create, or delete branches

git branch            // Show local branch
git branch -r // Show remote branch
git branch -D [branch_name] // Delete branch
git branch -M [branch_name] // Rename branch
git branch [new_branch_name] [origin_branch_name] // create a branch
git branch -C [new_branch_name] // Create a branch base on current
// Set current local branch links to remote branch
git branch --set-upstream-to=origin/[branch_name] [branch_name]

5. Git Checkout

Switch branches or restore working tree files

git checkout -b [branch_name]  // Create branch_name base on current
git checkout -B [branch_name] // Create or Switch to branch_name base on current
git checkout [branch_name] // Switch to branch_name
git checkout HEAD~2 build.gradle // Revert build.gradle 2 version// Revert build.gradle 2 version base on feature_A branch
git checkout feature_A~2 build.gradle
git checkout build.gradle // Revert build.gradle to current
git checkout -- '*.kt' // Revert all 'kt' file

6. Git reset

git reset               // Undo add
git reset -- "*.kt" // Undo add all 'kt' file
git reset --hard // Reset all changed files
git reset --soft HEAD^ // Move HEAD to previous commit, keep changesgit reset --hard HEAD~3 // Revert 3 commits

7. Git log

Show commit logs

git log                   // Show commits log
git log --oneline // Show each commit by one line
git log --pretty=format:'%s' // Show commit message
git log -n // Show last n commits
// Show commit content not in develop branch
git log --cherry --pretty=format:'%s' origin/develop..[branch_name]
// Show git history
git reflog | grep [some_thing]

8. Git Add/Remove/

Add file contents to the index

git add [filename]
git add . // Add all file in current directory
git add -A [--all] // Add all file in repo
git rm [filename]
git rm '*.kt' // remove all 'kt' file in current directory

9. Git commit

Record changes to the repository

// New commit
git commit -m [commit_message]
// combine the last commit with current commit
git commit --amend -m [commit_message]

10. Git push/pull/fetch

Fetch from and integrate with another repository or a local branch

git pull
git pull origin
// Merge featureA into current branch
git pull origin featureA
git fetch [remote_name] // git fetch origin
git fetch --all
git push [remote_name] // git push origin
git push [remote_name] [branch_name] // push to a branch on remote
// A handy way to push the current branch to the same name on the remote.
git push origin HEAD
git push
git push origin

Hope this helpful to you!!!

--

--

Long Do
Long Do

Written by Long Do

Better code, better world

No responses yet