Skip to content

Instantly share code, notes, and snippets.

@rafaelune
Last active September 17, 2021 19:05
Show Gist options
  • Save rafaelune/3804162 to your computer and use it in GitHub Desktop.
Save rafaelune/3804162 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

Initialize local git repository

  $ git init

Clone remote server git repository

  $ git clone git@github.com:username/Hello-World.git

Change username for your user name.

Change Hello-Word for your git repository name.

Add files from file directory to staging area

  $ git add

Add unstaged contents to staging area

  $ git add -i

Adds part of a file to stage area

  $ git add -p

Basic commit to local git repository

  $ git commit -am "Commit description"

Interactive commit to local git repository

This command let's you mark what you want to commit.

  $ git commit --interactive

After all type your commit message on vi, 'ESC' and ':wq'.

Local git directory status

  $ git status

Local modified files

  $ git ls-files -m

Local git directory log

  $ git log

List remote servers

  $ git remote -v

Add a remote git repository called "origin"

  $ git remote add origin https://github.com/username/Hello-World.git

You can change "origin" to what you prefer. Origin is just an example name.

Removes remote git repository called "origin"

$ git remote rm origin

Origin is just an example name. You may have other remote git repository name.

Send committed change to remote git repository

$ git push origin master

Change origin for your remote git repository name. Change master for your desired working branch name.

Get pushed files from git remote repository

$ git pull origin master ~> "origin" is remote server nickname

Gets the most recent modifications from remote repository.

Create a new local branch

$ git checkout -b 'newfeature'

The above command, creates a branch called "newfeature".

Create a new branch from git remote repository branch

$ git checkout -b "branch_local" origin/"branch_remote"

The above command, creates a new local branch called "branch_local" from "branch_remote" on "origin" remote git repository.

Create a new branch based on a tag version

$ git checkout -b newbranch v1.0

Destroy git remote repository branch

$ git push origin --delete newfeaturebranchname

Change newfeature to your remote branch name.

Destroy git local repository branch

$ git branch -d newfeature

Change newfeature for your local branch name.

Checkout a specific file

$ git checkout -- init.php
$ git checkout HEAD init.php ~> obtem a ultima versao

HEAD - last modified version.

Commits log

$ git log

Commit differences between files

$ git log -p

Display the two last commits

$ git log -p -2

Each commit is displayed in a different line

$ git log --pretty=oneline

Display the last two days commits

$ git log --since=2.days

Display log history graph

$ git log --graph --oneline --decorate --date=relative --all --pretty=format:"%h %s"

Display modified files on remote

$ git fetch
$ git diff --name-status a8e5e4e..295bf31

Save local changes into a different area

$ git stash

Useful if you made changes that you don't wanna commit but you need to switch to another branch.

Display stash list

$ git stash list

Applies a stash

$ git stash apply

Reset changes

$ git reset --hard HEAD

Discard unstaged changes

$ git clean -df

Discard staged changes

$ git checkout -- .

Merge branches

$ git checkout master // goes back to master branch
$ git pull // get updates
$ git merge newfeature //merges newfeature branch into master branch

Copy SSH

$ clip < ~/.ssh/id_rsa.pub

Git tool GUI

$ git gui

GUI for un-staging and committing.

Git tool gitk

$ gitk

GUI for visualizing commits through the time.

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