Skip to content

Instantly share code, notes, and snippets.

@gbroques
Last active April 18, 2018 00:56
Show Gist options
  • Save gbroques/1aee3f2dec5ff578e757f34c9ddc3881 to your computer and use it in GitHub Desktop.
Save gbroques/1aee3f2dec5ff578e757f34c9ddc3881 to your computer and use it in GitHub Desktop.
Useful Git Commands

Useful Git Commands

Commiting

Show the current status of your repo.

  • git status

Show a diff between your changes and what's in the repo.

  • git diff [filename]

Add files with changes to be committed.

  • git add filename

Add all changes to be committed.

  • git add .

Making commits.

  • git commit -m "Add my commit message"

    • -m Specify a commit message

Unstage a file.

  • git reset filename

Unstage all changes.

  • git reset HEAD

Show the commit log.

  • git log

    • -n Show the last n commits
    • --oneline Make the log more readable

Pushing

  • git push

Pushing Local Branches Upstream

  • git push -u origin my-branch

Pulling

git pull does a git fetch followed by a git merge.

If you don't want to merge a remote branch then simply git fetch some-branch.

Fetching is useful getting a remote branch for the first time, while pulling is useful for getting the latest changes.

  • git pull -r

    • -r Incorporate changes by rebasing instead of merging. Helps prevent useless merge commits.

Branching

List all branches.

git branch

Checkout a branch.

  • git checkout my-branch

    • -b Create and checkout a new branch

Merging

  • git checkout master

  • git merge my-branch

Interactive Rebasing

For rewriting history on branches only you work on. This allows for cleaner and more helpful commit history.

Important: Never rewrite history on a branch someone else pushes / pulls from.

Interactively rebase the last n commits.

  • git rebase -i HEAD~n

Stashing

Stash changes for later use when your not ready to commit.

  • git stash

Stash your changes with a meaningful name.

  • git stash save "My half-completed feature"

List all stashes.

  • git stash list

Apply changes from a stash.

  • git stash apply stash@{n}

Delete a stash.

  • git stash drop stash@{n}

Misc

List your remote repositories.

  • git remote

    • -v Verbose

Tell Git to ignore specific files and directories by specifying it in .gitignore

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