Basic Git Commands

Common git commands I tend to use.

Features are added, files are refactored and bugs are removed. For this reason seasoned developers use version control. There are plenty of benefits to using version control. Below you can find the main reasons I, as well as many others, love it.

Benefits of version control

As much as I love Git. It never fails that I forget a command or two. It doesn't help that there's many ways to do the same thing. Regardless, when I forget one command I reference the list below.

Creating Repositories

Create a local repo

git init

Download a repository and its history

git clone https://github.com/user/repo

Change the URI or URL for a remote repo

git remote set-url origin https://your.new.url

Making Changes

List all files to be committed

git status

View file differences that are unstaged

git diff

Stage single file

git add path/of/file

Stage all files

git add .

Unstage single file, but retains contents

git reset path/of/file

Commit changes to repository

git commit -m "message"

Managing Branches

View local branches

git branch

View remote branches

git branch -r

View all local and remote branches

git branch -a

Switch branches

git checkout your-branch-name

Merge specific commit to current branch

git cherry-pick your-sha

Merge a range of commits to current branch

git cherry-pick your-first-sha^..your-second-sha

Stashing and Unstashing

Save changes as stash

git stash save "message"

Apply stash

git stash apply stash@{#}

View all stashes

git stash list

Others

Change URL for remote git repository

git remote set-url origin https://github.com/user/repo

View existing remotes

git remote -v

Remove cached files

git rm -r --cached .

Overwrite all local files and update to latest commit

git reset --hard origin/master