Skip to content

Instantly share code, notes, and snippets.

@torleifhalseth
Last active August 31, 2018 06:23
Show Gist options
  • Save torleifhalseth/7765480 to your computer and use it in GitHub Desktop.
Save torleifhalseth/7765480 to your computer and use it in GitHub Desktop.
Useful git commands

Git Handbook

Workflow

  1. Fork repository
  2. git clone [fork]
  3. git remote add upstream [master]

Check your remote

git remote -v

Add remote

git remote add [remote name] https://github.com/user/repo.git

Remove remote

git remote rm [remote name]

Change remote to use ssh-key

git remote set-url origin [ssh-key]

Create a new branch

git checkout -b [branch-name]

Change branch

git checkout [branch-name]

Delete branch

git branch -D [branch-name]

Create and delete branches on Github.

Delete commits

http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git

List branches

git branch --list

Push branch to remote

git push [remote-name] [branch-name]

####Push and create branch on remote

git push -u [remote-name] [branch-name]

Merge branches

git merge [branch-name]

Revert changes to modified files.

git reset --hard

Remove all untracked files and directories.

git clean -fd

Clean local changes

Check what would be deleted

git clean -n

Delete items

git clean -f

Discard changes

For a specific file use:

git checkout path/to/file/to/revert

For all unstaged files use:
```bash
git checkout -- .

Remove untracked files / directories

git clean -fdx --

-f - force
-d - directories too
-x - remove ignored files too ( don't use this if you don't want to remove ignored files)

Reset

git reset --hard [tag/branch/commit id]

Stash changes

git stash

Get stashed changes

git stash pop

The first stash hides away your changes (basically making a temporary commit), and the subsequent stash pop re-applies them. This lets git use its merge capabilities.

Closing issues via commit messages

fixes #[issue number] into a commit message

Tagging

git tag [tag(v1.0.0)]

Push tag to origin

git push origin [tag]

Display tags

git tag

Push all tags

git push origin --tags

Pull without creating merge commits

git pull --rebase [remote branch]

Pull origin branch

git pull origin [remote branch]
git cherry-pick [commit id]

Creating gh-pages from subdirectory

git add [directory] && git commit -m "Initial [name of solution] subtree commit"
git subtree push --prefix [directory] [branch (typically origin or upstream)] gh-pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment