Skip to content

Instantly share code, notes, and snippets.

@tylertadej
Last active May 29, 2023 01:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylertadej/d3fdc6e8465fb3f9105a to your computer and use it in GitHub Desktop.
Save tylertadej/d3fdc6e8465fb3f9105a to your computer and use it in GitHub Desktop.
Pro Git commands for Git users

Git Your Act Together

Git Setup

Global settings stored in a system .gitconfig file; view file using $ cat ~/.gitconfig
Single repo settings stored in /.git/config in your project folder
View current configurations in the terminal window
$ git config --list
$ git config <level> --list
Pass in specific levels of configuration
# Single repo     --local
# Current user    --global
# All users       --system
Assign username, email, text editor and color settings
$ git config --global user.name "John Doe"
$ git config --global user.email "johndoe@users.noreply.github.com"
$ git config --global core.editor <editor-symlink>
$ git config --system color.ui true
Explicity tell Git to be case-sensitive (OS X and Windows)
$ git config --global core.ignorecase false
Rebase by default when doing git pull
$ git config --global pull.rebase true

Remote Control

Remote repositories are versions of your project hosted on the Internet or a network
View remotes with fetch and push info
$ git remote -v
View remotes to see if tracked, up-to-date or stale
$ git remote show origin
$ git remote show upstream
$ git remote show <remote>
Set origin repo using SSH - set to your GitHub repo (read/write access)
$ git remote set-url origin git@github.com:username/repository.git
Set upstream - repo that you forked so you can pull/sync upstream updates (read access)
$ git remote add upstream git://github.com/user/repo.git
Remove refs to stale remote branches
$ git remote prune origin
$ git remote prune upstream
$ git remote prune <remote>
Rename your remote
$ git remote rename <original-name> <new-name>
Remove/Delete your remote
$ git remote rm <remote>

Branching Out

The default branch name in Git is master. Use feature branches as part of your workflow.
View Branches
$ git branch      # Locals
$ git branch -r   # Remotes
$ git branch -a   # All
Create a Branch
$ git branch <new-branch>       # Create new-branch but stay on current branch
$ git checkout -b <new-branch>  # Create new-branch and checkout 'new-branch'
Rename Branches
$ git branch -m <new-name>                  # Rename branch you are currently in
$ git branch -m <current-name> <new-name>   # Rename from any branch
Delete Branches
$ git branch -d <branch>                    # Local (uppercase '-D' removes uncommited changes too)
$ git push <remote> :<branch>               # Remote (the colon can be substituted for '--delete ')
Checkout a new remote branch
$ git fetch <remote> <branch>               # Pull down latest changes into the index
$ git checkout <new-remote-branch-name>     # Checkout local version of new remote branch

Nice 'Stash

Stashing allows you to save uncommited changes (staged and unstaged)
View Stashes
$ git stash list
$ git stash list --stat   # View details of stashes listed
$ git stash show          # View details of most recent stash
Add a message to your stash
$ git stash save 'message goes here'
Create a new branch with your most recent stash
$ git stash branch <new-branch> stash@{0}
Only stash unstaged changes and preserve the staging area
Note: this will not stash untracked files
$ git stash --keep-index
Stash everything, including untracked files
$ git stash --include-untracked     === git stash -u
Shave that stash list
$ git stash clear   # Deletes all saved stashes
Shortcuts
$ git stash         === git stash save
$ git stash apply   === git stash apply stash@{0}
$ git stash drop    === git stash drop stash@{0}
$ git stash pop     === git stash apply AND git stash drop

Re'set Yourself

Make changes to or remove previous commits
These commands move HEAD back 1 commit as indicated by HEAD^
$ git reset --soft HEAD^    # Undo last commit and keep modified files staged
$ git reset --hard HEAD^    # Undo last commit and delete changes
$ git reset --mixed HEAD^   # Undo last commit and unstage everything (default if type not specificed)
Move back further using the tilde HEAD~2, HEAD~6, etc
Reset to a specific point in history
$ git reset --hard <SHA>

Alternatives to $ git reset

Revert: remove a specific commit but preserve the history of surrounding commits
$ git revert <SHA>
Amend: add changes and updated commit message to last commit
$ git commit --amend -m 'Updated message'
Running this command without any changes is a quick way to update the last commit message

Cherry Picking FTW!

Copy commits from other branches into current branch
$ git cherry-pick <SHA>

# Opens editor so you can update commit message
$ git cherry-pick --edit <SHA>

# Combines changes and adds to staging area uncommited (so you can make changes)
$ git cherry-pick --no-commit <SHA-1> <SHA-2>
$ git commit -m 'squashing some cherries'

# Track which commit you cherry picked from (since new SHA is generated)
$ git cherry-pick -x <SHA>

# Track original committer and the cherry picker
$ git cherry-pick --signoff <SHA>

Holy Reflog.. Git never forgets.

View your "deleted" history of each time HEAD moved
$ git reflog
$ git log --walk-reflogs    # View detailed history of change

Ungit yo-self

After cloning a repo you might want to create a fresh one for yourself
$ rm -rf .git
$ git init
$ git add .
$ git commit -am "First commit"

Git scenarios

Updating your fork

Add remote for upstream
$ git remote add upstream <path_to_repo>
Fetch changes
$ git fetch upstream
Merge them into master
$ git merge upstream/master master
Push them to your remote
Adding the -u sets up tracking
$ git push -u origin master
Push to remote with new branch name
$ git push -u origin branch1:feature
Reset your local repo to mirror the remote
$ git reset --hard upstream/master

Using .gitignore

Ignore uncommitted changes in a file that is already tracked
$ git rm --cached
$ git rm --cached /directory
$ git rm --cached *.filetype
$ git rm --cached <filename>
Ignore uncommitted changes in a file that is already tracked
$ git update-index --assume-unchanged
$ git update-index --assume-unchanged <file>
Remove untracked files
$ git clean -n          # Dry run
$ git clean -f          # Deletes untracked files
$ git clean -fd         # Deletes untracked files & directories
Adding and committing files
$ git commit -m         # Create commit message to staged files
$ git commit -am        # Add tracked files with changes and create commit message
$ git add -A            # Add all files including untracked
$ git add *name*        # Add all files that contain 'name' to staging
$ git add dir/folder    # Add all files in directory dir/folder to staging
Remove branches that have already been merged with master
Add as command, eg 'delete merged'
dm = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d; git remote -v update -p"
@SuvamPrasd
Copy link

Thanks for helping me out. Your repo is really awesome for those who are looking to learn advanced git commands.

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