How to

Push

Push to a remote specific branch

Assuming as an example that the remote upstream has been added and you need to send code from the local branch develop to the remote branch dev-master, to be able to execute this operation just execute the command below:

git push upstream develop:dev-master

Push ignoring verify or hooks

git push github develop:master --no-verify

Ignore all files in this folder

When you want to ignore files but want to keep a directory you can add a .gitignore file within the desired directory with the following content

*
!.gitignore

Get current branch

git branch --show-current

Cache

Clear Entire Git Cache

To clear your entire Git cache, use the “git rm” command with the “-r” option for recursive

 git rm -r --cached .

When all files are removed from the index, you can add the regular files back (the one you did not want to ignore)

git add .
$ git commit -am 'Removed files from the index (now ignored)'

Cache user and password

git config credential.helper store
git config --global credential.helper 'cache --timeout 7200'

Stash

Stash including untracked files

git stash --include-untracked

Stash only file or folder

git stash push path/to/file

Revert

Revert files from a specific hash commit

git checkout 3df73866bef5d560ef2e5s59b6deaa18001 -- wp-content/uploads/2011 wp-content/uploads/2012 

Revert to a specific commit

git revert --no-commit 0766c053..HEAD

Reset

Reset Permissions

When you accidentally modify the permissions of versioned files, it is possible to return to the initial permission state using the command below

git diff -p -R --no-ext-diff --no-color \
    | grep -E "^(diff|(old|new) mode)" --color=never  \
    | git apply

Use globally

If you want to use globally, you can do it that way:

git config --global --add alias.permission-reset '!git diff -p -R --no-ext-diff --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply'
git permission-reset

Reset by time

git reset --hard master@{"300 minutes ago"}

Undo the last commit

 git reset --soft HEAD~1

Rename

Rename your branch

git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name
  • Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

  • Reset the upstream branch for the new-name local branch.

Switch to the branch and then:

git push origin -u new-name

Logs

Find versioned files

To find files that have been versioned, regardless of whether they were removed from versioning or not.

git log --all --full-history -- "**/thefile.*"

View git logs of just one author commits

# Only commits by Vinicius
git log --author="Vinicius"

# Use "--all" to search all branches
git log --author="Vinicius" --all

View git logs of many authors commit

# Many Authors
git log --author="\(Vinicius\)\|\(Jose\)"

Exclude git commits log from one or more author

git log --author='^(?!Vinicius|Jose).*$' --perl-regexp

Proxy bypass to specific host

# Globally
git config --global add remote.{remote_name} proxy 

# Locally
git config --local --add remote.{remote_name}.proxy ""

Fixing wrong commit messages

git rebase -i HEAD~4

Deleting remote branches

git push origin --delete branchName

Keeping a fork up to date

Clone your fork:

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

Add remote from the original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream

Updating your fork from the original repo to keep up with their changes:

git pull upstream master

How to use prune to Clean Up remote branches

Using "prune" on a Remote Repository

The easiest way to use prune is to provide it as an option when fetching:

$ git fetch --prune origin

In cases where you'd like to only perform a prune and not fetch remote data, you can use it with the git remote command:

$ git remote prune origin

If you want to have prune executed with every fetch operation, you can configure Git accordingly:

$ git config --global fetch.prune true

Tags

Create and tag with a message

git tag -a 0.0.0 -m "initial Changelog" 

Last updated