Skip to content

Instantly share code, notes, and snippets.

@midwire
Forked from scootcho/git commands.md
Last active August 23, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midwire/47ba843ca3f74e7d7918e8aaaabb1d02 to your computer and use it in GitHub Desktop.
Save midwire/47ba843ca3f74e7d7918e8aaaabb1d02 to your computer and use it in GitHub Desktop.
[Common Git Commands] #git #shell

initialize git depository in the current directory

git init .

display the git remote/origin

cat .git/config

display gitconfig

cat .gitconfig

display where the HEAD is pointing

cat .git/HEAD

git configuration

git config --global user.name "user name"
git config --global user.email "user@example.com"
git config --global core.editor "vim"

display git configuration

git config --list
git config user.name
git config core.editor

List all aliases

git config --get-regexp alias

Rename local branch

git branch -m <oldname> <newname>
# rename current branch
git branch -m <newnam>

setup line ending preferences (osx)

git config --global core.autocrlf input
git config --global core.safecrlf true

(windows)

git config --global core.autocrlf true
git config --global core.safecrlf true

combine the last 2 commits into 1

git rebase -i HEAD~2

push a specific branch to github “origin" is the remote, “feature” is the branch

git push origin feature-branch

add URL to remote Git repository

git remote add upstream git@github.com:user/repo.git

Change the URI (URL) for a remote Git repository

git remote set-url origin git://new.url.here

files to ignore tracking

.gitignore

an empty directory needs a file for git to keep track, convention way is to create a ".gitkeep" file

.gitkeep

show commit log

git log

show last 3 commits

git log -3

Show the working tree status

git status

add all changes to staging

git add .

add specific file to staging

git add file_name

move/rename the file name and make changes to the stage index

git mv file_name dir/file_name

add comment to commit

git commit -m "modification note"

add and commit all files at the same time

git commit -am "modification note"

drop file and replace with version in previous commit

git checkout file_name

show difference in modified files yet "added" to staging

git diff

show difference in modified files in staging

git diff --staged
git diff --cached

show difference files changed between two commits

git diff --name-only SHA1 SHA2
or
git diff --name-only HEAD HEAD^

un-stage the added file from staging

git reset HEAD file_name

combine staged changes with the previous commit

git --amend

squash last "n" commits

# squash last 3 commits
git rebase -i HEAD~3

grab file at a specific hash/SHA commit

git checkout hash9834ef2 —- dir/file_name

revert the specific hash/SHA commit

git revert hash82ej23

soft/mixed/hard resets have different reset results

git reset --soft/mixed/hard SHA

forcefully push to repository

git push <reponame> -f

removes untracked files from your working directory (can’t undo)

git clean -n

reflag allows you to go back to changesets even though they are not referenced by any branch or tag

git reflog

removing the file tracking from the staging index

git rm --cached file_name

Shows one or more objects (blobs, trees, tags and commits)

git show

git show SHA
git show HEAD

one commit before the current HEAD (all are equivalent)

HEAD^, 8c22db944^, Head~1, HEAD~

two commit before the current HEAD (all are equivalent)

HEAD^^^, Head~3

Lists the contents of a given tree object

git ls-tree HEAD
git ls-tree branch-name

git log display & filter options

git log --oneline
git log --format=oneline
git log --format=oneline HEAD`3
git log --format=short, medium, full, fuller, email, raw
git log --graph (show you the trees of commits in graph)
git log --oneline --graph --all --decorate
git log --since="2012-06-20"
git log --before="2012-12-12"
git log --since=2.weeks --until=3.days
git log --author="scott"
git log --grpe="temp" (grab all commit msg that has the word "temp" in it)
git log 812yfs..09jfe (grab log between two commit ranges)
git log o87423..index.html (grab all changes in the "index" file since a commit)
git log -p o87423..index.html (grab all changes in the "index" file since a commit)
git log --status --summary

returns all the difference between that commit and current directory

git diff 02er2 index.html 

shows difference between two point of commits in time

git diff 8n9ufe..09faes

show difference between one commit and current HEAD)

git diff o8yfo3..HEAD
git diff --stat --summary 821li..HEAD

ignore changes in white spaces

git diff -b 

ignore changes in ALL spaces

git diff -w

compare master branch and new_branch_name

git diff master..new_branch_name

show in one line in color compare master branch and new_branch_name

git diff --color-words master..new_branch_name

compare masters from remote to local

git diff --oneline origin/master..master

to see which branch you're working on

git branch

creating a branch name new_branch_name

git branch new_branch_name

to switch the head to a branch named new-branch_name

git checkout new_branch_name

create and switch to a new branch named new_branch_name

git checkout -b new_branch_name

show the branch that has all merged commit under a branch

git branch --merged

renaming branch name from new_branch_name to specific_branch_name

git branch -m new_branch_name specific_branch_name

delete branch named delete_branch_name

git branch -d delete_branch_name

show you branch on remote depository

git branch -r

show you branches on both remote depository and local depository

git branch -a

you want to be on the branch you want merged 'master', then merging branch_name into master

git merge branch_name

show logs in oneliners

git log branch_name --oneline -5

merge with no fastforward

git merge --no-ff_branch

merge with only fastforward, if not possible, abort)

git merge --ff-only_branch

show all branches in graphical representation

git log --graph --oneline --all --decorate

saving changes without commiting

git stash save "saved stash description"

save a named stash

git stash push -m "my-stash-name"

show a list in the stash

git stash list

get the stash by calling that number

git stash show -p stash@{1}

pull from the stash and apply to the working directory, leaving a copy

git stash apply

pull from the stash and apply to the working directory, removing copy from stash

git stash pop

apply a specific stash and remove it from the stack

git stash pop stash@{n}

apply a specific stash and keep it in the stack

git stash apply stash@{n}

deleting the specific item in stash

git stash drop stash@{1}

clearing everything in stash

git stash clear

push up 'master' branch to remote depository

git push -u origin master

always fetch before you work and push

git fetch

log on remote depository

git log --oneline -5 origin/master

git pull = git fetch + git merge

git pull

creating a branch off remote and tracking

git checkout -b file_name origin/file_name

deletes the file_name off from remote depository

git push origin --delete file_name

force the local changes to remote (shouldn't do this)

git push origin --force

setup shortcuts for certain command

git config --global alias.shortcutname "command"

creating shortcut call logg and show all the options in ""

git config --global alias.logg "logg --graphic --decorate --oneline --abbrev-commit --all"

recover deleted branch

## Pre-requisite: You have to know your last commit message from your deleted branch.
git reflog
# Search for message in the list
# a901eda HEAD@{18}: commit: <last commit message>

# Now you have two options, either checkout revision or HEAD
git checkout a901eda 
# Or
git checkout HEAD@{18}

# Create branch
git branch recovered-branch

# You may want to push that back to remote
git push origin recovered-branch:recovered-branch

Resources:

https://www.atlassian.com/
http://git-scm.com/docs
http://gitimmersion.com/
https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog
https://gist.github.com/umayr/b95e11d5f22c24a872ef95d215ba2ab1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment