Skip to content

A collection of GIT commands that I personally use because they're really useful

Notifications You must be signed in to change notification settings

Gurenax/really-useful-git-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 

Repository files navigation

Really Useful GIT commands

A collection of GIT commands that I personally use because they're really useful. This is a work in progress, please feel free to Contribute.

Contents

Basic Add, Commit, Push

git add .
git commit -m "Description"
git push origin master

Basic Pull

git pull origin master

Create new branch

git checkout -b new-branch

Merge new branch to master

git checkout master
git merge new-branch

Rebase master onto new branch

Warning: Never rebase a public/shared branch.

git checkout new-branch
git rebase master

Merge vs Rebase

  • Merge just means you are combining two branches together.
  • Rebase on the otherhand means you are moving the base of the branch to a different position. (e.g. When you checkout to a new branch, you basically copy all the commits from the source branch. That separation is called the base of the new branch. Now, Rebasing means you will no longer use that separation because you will move the base to the latest commit of the source branch a.k.a. Rebase).
  • Anyways, here is The best explanation

See all branches

git branch -a

Delete new branch

git branch -d new-branch

Show list of files changed in a Commit

git diff-tree --no-commit-id --name-only -r <commit hash>

Show contents of a Commit

git show <commit hash>

Revert a Commit

git revert <commit hash>

Revert a Commit from History

git revert --strategy resolve <commit hash>

See a summary of changes between the Branch and the Origin

# You can replace master with your branch
git diff --stat origin/master
# or just use the following to dynamically check your current branch
git diff --stat @{u}

See a log of Incoming Changes before you do a git pull

# You can replace master with your branch
git fetch && git log ..origin/master
# or just use the following to dynamically check your current branch
git fetch && git log ..@{u}

See a log of Outgoing Changes before you do a git push

# You can replace master with your branch
git fetch && git log origin/master..
# or just use the following to dynamically check your current branch
git fetch && git log @{u}..

Undo the last git commit

Warning: Create a new branch before rebasing to undo the last commit. Never rebase a public/shared branch.

git rebase -i head~2
# Then delete the 2nd line (e.g. pick XXXXXXX desription)

Undo the last git commit without rebasing

git reset --soft head~1

Remove git commited files or folders

(e.g. sensitive data which were accidentally pushed)

git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch <name of file or folder>' --prune-empty -f -- --all

Tell git to remember your git login

# Mac Only
git credential-osxkeychain
git config --global credential.helper osxkeychain
# Windows Only
git config --global credential.helper wincred
# Linux Only
git config --global credential.helper cache
# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

Pretty Git Log

# Variation 1
git log --date-order --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"
# Variation 1 with --all
git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"
# Variation 2
git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit
# Variation 2 with --all
git log --all --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit

Stash Changes

git stash

Re-apply Stashed Changes and delete from Stash List

git stash pop

Re-apply Stashed Changes but do not delete from Stash List

  • Useful when applying the stash to multiple branches
git stash apply

View Stashed Changes

git stash list

Stash Changes and Pull Updates

git stash
git pull
git stash pop

Stash Changes and Merge a Branch

git stash
git merge <branch name>
git stash pop

Push to current branch without specifying the name

Instead of using git push origin really-long-name-for-a-branch, use:

git push origin HEAD

Push to all remote branches

git remote | xargs -L1 git push --all

Push a specific branch to all remotes

git remote | xargs -L1 -I R git push R master

Create an alias for pushing to all remote branches

git config --global alias.pushall '!git remote | xargs -L1 git push --all'
git pushall

Bundle a GIT repo to a zip file

git bundle create <Destination Folder> --all

Overwrite a branch entirely with another branch

For example, we want to overwrite master entirely with dummy

git checkout dummy
git merge -s ours master
git checkout master
git merge dummy

Exclude a file from the git repo without using .gitignore

Edit the exclude file and add the folders/files you wish to ignore similar to .gitignore

vi .git/info/exclude

Exclude a file from the git repo which is already tracked

git update-index —assume-unchanged path/to/file.txt
git update-index —assume-unchanged path/to/folder/

...more to come

About

A collection of GIT commands that I personally use because they're really useful

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published