Skip to content

Instantly share code, notes, and snippets.

@markcarrrr
Last active October 19, 2020 10:51
Show Gist options
  • Save markcarrrr/7fdaed00c0dff0175fe1 to your computer and use it in GitHub Desktop.
Save markcarrrr/7fdaed00c0dff0175fe1 to your computer and use it in GitHub Desktop.
It's fine remembering the common day-to-day commands but here's a list of commands I use as a reference for the lesser used commands.

GIT Commands

Shallow clone

git clone --depth 1 [REPO_PATH]

Unshallow repo

git fetch --unshallow

Checkout a branch/all branches after shallow clone

git remote set-branches origin [SPECIFIC-BRANCH or * for all branches]

git fetch


Pop specific stash

git stash pop stash@{1}


Undo revert that hasn't been pushed

git reset --hard HEAD^


Log deleted file

git log --all -- [path/to/file]


Log a file on another branch

git log [branch] -- [path/to/file]


View logs by author n.b. use quotes if there is a space in the author name

git log --author=[author]


Cherry pick specific commit

git cherry-pick [COMMIT]


View list of files in commit with status

git show --name-status [COMMIT]


Trace deleted file to commit

git log -1 --stat -- [FILE]


View unpushed commits

git log origin/master..HEAD


Restore renamed file after stash

Pop stash to retain renamed file - git stash pop --index

If you have popped stash without using the above then... git rm --cached [ORIGINAL_FILENAME]


Show files in stash

Current stash git stash show

Stash 1 level deep git stash show stash@{1}


Branch

git checkout -b [BRANCH NAME]
git push -u origin [BRANCH NAME]


See log history after file name has been changed...

git log --follow ./path/to/file


Delete all files of extension...

git rm -r *.map


Create/add to .gitignore

echo *.map >> .gitignore


Add files to latest commit

  • git add .
  • git commit --amend --no-edit

Delete branch on both remote and local

  • git push origin --delete BRANCHNAME (remote)
  • git branch -d BRANCHNAME (local)

e.g. git push origin --delete BRANCHNAME; git branch -d BRANCHNAME


Update origin

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


Compare files between branches

git diff branch1:file branch2:file - compare two files with different filenames

or

git diff branch1 branch2 -- myfile.cs - compare same file


Amend commit message

git commit --amend -am "Amended commit message"


Show latest git commit message

git log -1


Reset commits

e.g. reset past the last 2 commits

git reset --hard HEAD~2

e.g. reset from remote

git reset --hard origin/master


Unstage file

git reset HEAD <file>

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