Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save version-control/7fc87416437fd1f721c449abf39dd0c1 to your computer and use it in GitHub Desktop.
Save version-control/7fc87416437fd1f721c449abf39dd0c1 to your computer and use it in GitHub Desktop.
git real world commands

git real world commands

Checking out

Review unstaged changes

git add -p

Discard a file, a folder or a path

git checkout -- <path>

The parameter path can be equal . to discard all the changes.

Staging

Stage a file, a folder or a path

git add [-A] [<path>]

Unstage a file, a folder or a path

git reset <path>

If no path is provided, all the changes are staged.

The parameter -A means:

Update the index not only where the working tree has a file matching but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

Interactively stage changes

git add -p

Review staged changes

git diff --staged

More information about this one can be found on Stack Overflow.

Fetching

Retrieve commits from a remote repository into your local repository

git fetch <remote> [<branch>]

Update a local branch without check it out

As long as you're doing a fast-forward merge, then you can simply use:

git fetch <remote> <sourceBranch>:<destinationBranch>

Source on Stack Overflow.

Merging

Merge a branch into the current branch

git merge <branch>

Pulling

Pull a remote branch

git pull [<repo>] [<branch>]

A git pull operation is equal to a git fetch followed by a git merge. If the local branch contains non pushed commits, the command creates an intermediate commit that performs the merge operation.

Rebase a remote branch

git pull --rebase <repo> <branch>

Similar to git pull, but rebase the branch before applying the remote commits. In other words, the local branch will apply the remote commits before applying the local commits. Thus, this prevents creating an intermediate commit.

Pushing

Push a branch

git push [<repo>] [<branch>]

If the branch name is not provided, the global configuration push.default is used to determine the branches to push. More information about the value of this paramater can be found on (Stack Overflow)[http://stackoverflow.com/a/21866819/2780334].

Push the tags

git push --tags

Push a branch not currently checked out

git push <repo> <branch>:<branch>

It's possible to have multiple <branch>:<branch> declarations in a same command.

Branching

Display local branches

git branch

Display remote branches

git branch -r

Create a local branch

git branch <branch>

Delete a local branch

git branch -d <branch>

Delete a remote branch

git push origin --delete <branch>

Change the current branch

git checkout <branch>

Push all the local branches

git push <remote> --all

Committing

Display the commit of a branch

git log (--oneline) <branch>

Display the difference of commits between two branches (to see newer commits)

git log (--oneline) master..origin/master

Stashing

Create a stash

git stash save <message>

View stash diffs

git stash show <name>

Apply your stashed changes and remove the stash

git stash pop [<name>]

Apply your stashed changes and keep the stash

git stash apply [<name>]

Remove a named stash

git stash drop [<name>]

Rebase

Interactive rebase

git rebase -i HEAD~<n>

Interactively rebase the branch on <n> commits en enable the user to keep/squash/edit/delete them.

Reverting

Revert a given commit

git revert <sha>

Where <sha> is the identifier of the commit to revert.

Diffing

Display the diff between the HEAD and the working directory.

git diff

Exclude specific paths while diffing

git diff -- . ':!*.css' ':!*.scss'

Where we ignore *.css and *.scss extensions. More information on StackOverflow.

Utilities

Display the tree of the commits

git lg

Compare the versions of local and remotes branches

Use this utility available on Github. To install it, run the following command:

wget -P /usr/local/bin/ https://raw.githubusercontent.com/bill-auger/git-branch-status/master/git-branch-status

Git flow

The documentation can be found on Github.

Configure Beyond Compare as merge tool

Set the Windows PATH with the folder of Beyond Compare, e.g. C:\Program Files\Beyond Compare 4.

Set it as the default difftool/mergetool:

In Cygwin

git config --global diff.tool bc3
git config --global difftool.bc3.trustExitCode true
git config --global difftool.bc3.cmd "\"bcomp.exe\" \"\$(cygpath -w \$LOCAL)\" \"\$REMOTE\""

git config --global merge.tool bc3
git config --global mergetool.bc3.trustExitCode true

In Bash for Windows

git config --global diff.tool bc
git config --global difftool.bc.trustExitCode true
git config --global difftool.bc.cmd "\"bcomp.exe\" \"\$LOCAL\" \"\$REMOTE\""

git config --global merge.tool bc
git config --global mergetool.bc.trustExitCode true
git config --global mergetool.bc.cmd "\"BComp.exe\" \"\$LOCAL\" \"\$REMOTE\" \"\$BASE\" \"\$MERGED\""

ZSH specific configuration

Fix command slowness for some large repo

# Disable git status in the prompt
git config --add oh-my-zsh.hide-status 1
# Disable the tracking of dirty files
git config --add oh-my-zsh.hide-dirty 1

References on SO and on marc-abramowitz.com.

Fix autocompletion slowness for some large repo

Add the code below in .zshrc.

__git_files () { 
    _wanted files expl 'local files' _files     
}

Reference on SO.

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