Skip to content

Instantly share code, notes, and snippets.

@theowlsden
Last active May 26, 2019 23:35
Show Gist options
  • Save theowlsden/b777bb02507572312e61c6ae61147fce to your computer and use it in GitHub Desktop.
Save theowlsden/b777bb02507572312e61c6ae61147fce to your computer and use it in GitHub Desktop.

Basic Commands --

Configure Git

$ git config --global user.name 'username'
$ git config --global user.email 'email'

Create Repository

$ git init <project/folder-name> --> initialize local git repos
$ git clone <url> --> clone remote repository into a new directory

Make Changes

$ git status -->  lists all new or modified files to be committed
$ git diff --> show difference in file that is not yet staged
$ git add <file> --> create snapshot of file in order to commit later
$ git diff --staged --> show difference between staged and last file version
$ git reset <file> --> unstage file
$ git commit -m "descriptive message" --> create records of file snapshots permanently in version history

Group Changes

$ git branch -a --> list all local in current repos
$ git branch <branch-name> --> create new branch
$ git checkout -b <branch-name> --> create and switch to a new branch
$ git checkout <branch-name> --> switch to mentioned branch, updates the working directory
$ git merge <branch> --> combine mentioned branch history to current branch
$ git fetch <remote-repo> <remote-branch>:<local-branch> --> fetch a branch from remote repos
$ git branch -d <branch-name> --> delete mentioned branch
$ git checkout --track <remote-repo>/<remote-branch> --> track remote repos or branch for easily pulling and pushing
$ git branch -vv --> verify tracking of remote branch

Refactor Files

$ git rm <file> --> delete file from working directory
$ git rm --cached <file> --> delete file from version control but keeps it locally
$ git mv <file-original> <file-renamed> --> change file name

Save Fragments

$ git stash --> temporarily stores all modified tracked files
$ git stash pop --> restore the most recent stashed files
$ git stash list --> list all stashed changesets
$ git stash drop --> discard the most recent stashed changeset

Review History

$ git log --> list version history of the current branch
$ git log --follow <file> --> list version history of a file including renames
$ git diff <first-branch><second-branch> --> show difference between two branches
$ git show <commit> --> show metadata and content changes of mentioned commit

Redo Commits

$ git reset <commit> --> undo all commits after mentioned commit, keeping changes locally
$ git reset --hard <commit> --> discard all history and changes back to the mentioned commit

Synchronize Changes

$ git fetch <bookmark> --> download all history from repos bookmark
$ git merge <bookmark>/<branch> --> combine bookmark branch to current branch
$ git push <alias> <branch> --> upload all local branch commits to remote (Github) repos
$ git pull --> download latest commit and combine changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment