Skip to content

Instantly share code, notes, and snippets.

@reflexdemon
Last active March 25, 2016 21:54
Show Gist options
  • Save reflexdemon/fc5a7c3e6eefd7cc4e1e to your computer and use it in GitHub Desktop.
Save reflexdemon/fc5a7c3e6eefd7cc4e1e to your computer and use it in GitHub Desktop.
Git Goodies

#Useful goodies for GIT Command-line

Dangerous commands (e.g. you can lose your work) are labelled ⚠.

##Making local copy of a repo

$ git clone git@github.com:reflexdemon/demo.git

##Checkout to a Specific Branch/Tag/commit

$ git checkout <branch-name>/Tag/#commit-hash#

##Create a new branch with a specific commit on existing branch ⚠

$ git checkout <existing-branch>
$ git checkout #commit-hash#
$ git checkout -b <new-branch-name>

List all files touched since a certain commit

$ git diff --name-only #commit-hash#

or

$ git diff --stat #commit-hash#

List my remote branches

$ git branch -r

Delete a remote branch

$ git push origin :my-branch

Delete a remote TAG

$ git tag -d 12345
$ git push origin :refs/tags/12345

Remove untracked files from your git working copy

$ git clean -f -d

Delete a branch

$ git branch -d my-branch

Push a local branch to git

$ git push origin newfeature

Rename a branch

$ git branch -m old_branch new_branch

Remote branch into local repository

git fetch followed by git checkout -b my-branch origin/my-branch

Revert to a specific commit on a branch

$ git pull
$ git checkout <trgetted-branch>
$ git pull
$ git reset --hard <commit you want to be>
$ git push -force

Mirroring a remote Repo - One time

See: https://help.github.com/articles/duplicating-a-repository/

$ git clone --bare https://github.com/exampleuser/old-repository.git
# Make a bare clone of the repository

$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
# Mirror-push to the new repository

$ cd ..
$ rm -rf old-repository.git
# Remove our temporary local repository

Mirroring a remote Repo - Ona regular basis

See: https://help.github.com/articles/duplicating-a-repository/

Do this for the first time

$ git clone --mirror https://github.com/exampleuser/repository-to-mirror.git
# Make a bare mirrored clone of the repository

cd repository-to-mirror.git
$ git remote set-url --push origin https://github.com/exampleuser/mirrored
# Set the push location to your mirror

Add the below to the cron

$ git fetch -p origin
$ git push --mirror
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment