Difference between revisions of "Git Hacks and Tricks"

From OPeNDAP Documentation
m (Cheat sheet items)
m (Git resources)
Line 1: Line 1:
== Git resources ==
== Git resources ==
* The 'Pro git' book is online at: git.com
* The [http://git-scm.com/book/en/v2 Pro GIT] book is online at: git-scm.com
* Good cheat sheet: http://ndpsoftware.com/git-cheatsheet.html#loc=workspace;
* Good cheat sheet: http://ndpsoftware.com/git-cheatsheet.html#loc=workspace;
* Info on branching from git.com: http://git-scm.com/book/en/Git-Branching-Remote-Branches
* Info on branching from git.com: http://git-scm.com/book/en/Git-Branching-Remote-Branches

Revision as of 16:54, 23 February 2015

1 Git resources

2 Setup a username and access token for GitHub

git config --global github.user <name>
git config --global github.token <token>
where the token is made using the instructions at https://help.github.com/articles/creating-an-access-token-for-command-line-use

3 When using an old version of git on Linux

[jimg@wasabi hyrax-git]$ git pull
error: Couldn't resolve host 'github.com' while accessing https://github.com/opendap/hyrax.git/info/refs

4 Cheat sheet items

These are simple things that are not really obvious from the git book or other sources

How to see a list of 'conflicted' files after a merge
git diff --name-only --diff-filter=U
How to see the difference between to commits
git diff <commit-hash-1> <commit-hash-2>, e.g., git diff 0da94be 59ff30c
...for a specific file: git diff <commit-hash-1> <commit-hash-2> -- <file>
...and don't forget the shorthand for the hashes: git diff HEAD^^..HEAD -- main.c where HEAD^ is the previous commit
How to see the different remote branches
git remote show origin
Fetch all the branches on origin
git fetch origin
How do I list the remote branches (that have been fetched)?
git branch -a
How do I switch to a branch from a remote origin?
git checkout -b test origin/test
or, with newer versions of git: git checkout test
How do I see what would be pushed to a remote repo?
git push --dry-run
git diff origin/master # Assumes you have run git fetch, I think
git diff --stat origin/master # --stat just shows the file names stats, not the diffs
To get a specific file from a specific branch
git show dap4:./gdal_dds.cc > gdal_dds.dap4.cc You can use checkout instead of show and that will overwrite the file.
the general syntax is object (that's the 'dap4:./gdal_dds.cc' part) and it can use the ^ and ~n syntax to specify various commits on the given branch. A SHA can also be used.
How to change the 'origin' for a remote repo
git remote set-url origin git://new.url.here (https URLs work too...)
How to push a local branch to a remote repo
git push -u origin feature_branch_name
How to make and track a new (local) branch
git checkout -b <branch name>
Commited my code, then made a bunch of changes that just seem like a bad idea in retrospect. How do I go back to my previous commit for everything in a directory? I don't care if I loose all my changes since the last commit.
git reset HEAD --hard (Note that this is one of the very few git commands where you really cannot undo what you have done).
How to undo a commit (that has not been pushed)
git reset --soft HEAD~1. This leaves the files in their changed state in your working dir so that you can edit them and recommit. You can also change to a different branch and commit there, then change back.
In the above case, To reuse the old commit message
git commit -c ORIG_HEAD <-- This works because 'reset' copied the old head to .git/ORIG_HEAD. If you don't need to edit the old message, use -C instead of -c.