List your current Git remotes
To display a list of your current Git remotes and their endpoints, you can say
git remote -v
The output looks like this:
brady8 https://github.com/brady8/aegis.git (fetch)
brady8 https://github.com/brady8/aegis.git (push)
origin git@github.com:makandra/aegis.git (fetch)
origin git@github.com:makandra/aegis.git (push)
Git error: "badTimezone: invalid author/committer line - bad time zone"
You might get the above error message when cloning certain git repositories (for example the rails repository). It indicates that there is a malformed timestamp in some commit, and your git installation is configured to validate it.
As a workaround, you can disable the validation using
git config --global fetch.fsckobjects false
This settings seems to be the default for most git installations anyways.
git "fatal: bad config file line" after checking out branch
If git gives you an error message such as "fatal: bad config file line 123 in .git/config" after you tried to checkout a branch with a very long branch name, you very likely come across a bug in git version < 1.8.
You should ask someone with a newer git version (someone pushed the branch right?) to rename the branch to something shorter:
git -m old-very-very-long-branch-name new-short-branch-name
Git: Amending older commits
Lets say you need to make a change to a commit OLD_COMMIT
, but this is not the most recent. If you have neither pushed nor merged it, you can do this:
- Make a new commit now, with a message like "fix".
- Do a
git rebase -i OLD_COMMIT~
- In the editor window that opened, move the "fix" commit directly after the one you want to amend (so it should be the second from the top), and mark it as "fixup". Save the file.
- If there are conflicts, solve them, add them, and do
git rebase --continue
Check out a remote branch in git
If you would like to checkout the branch groups
, you can simply say this in recent versions of Git:
git fetch origin
git checkout groups
This will automatically track origin/groups
from a local branch groups
.
In older Git versions you need to say this:
git fetch origin
git checkout --track origin/groups
Git: Stash unstaged changes
This will stash all modifications that you did not git add
:
git stash -k
Note that newly created (and non-added) files will remain in your working directory unless you also use the -u
switch.
git stash -k -u
Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop
later on.
Git: How to stash with a custom message
If you say git stash
, your stashed changes will be identified with an automatically generated message:
$ git stash
Saved working directory and index state WIP on master: 77af0df Merge branch 'production'
While this is okay to temporarily stash away stuff, you may want a better identifier for your changes so you can find them more easily if you stash often.
Of course, there is a way to do it with git:
$ git stash save doing crazy things
Saved working directory and index state On master: doing crazy things
Note that you n...
Git: Revert one or more commits
Reverting a commit means creating a new commit that undoes the old changes.
Imagine the following commit history:
* commit_sha3 [Story-ID 1] Fixup for my feature
* commit_sha2 [Story-ID 5] Other feature
* commit_sha1 [Story-ID 1] My feature
You can revert a single commit using the following syntax:
git revert commit_sha2
To revert changes that are split across multiple commits, use the --no-commit
flag.
git revert --no-commit commit_sha3
git revert --no-commit commit_sha1
git commit -m "Revert Story 1"
Convert a Subversion repository to Git
- To retain all branches you can try the
svn2git
tool. However, this tool has some bugs. - To only import the trunk (with complete history):
git svn clone http://host/svn/...
- Create a .gitignore after the conversion.
- Turn the folder into a git repository as described here.
Git: Retrieve a file from a different branch or commit
To access files from another branch or past commit without doing a complete checkout, you can either use
git show branch:file
git show commit:file
to display, or check out the file into your working directory with
git checkout branch -- file
git checkout commit -- file
How to "git diff" with a graphical diff tool
If you are fine with the default console diff most of the time but only sometimes want to use an external tool for viewing a diff, you can use git difftool
.
E.g. viewing git diff
with meld:
git difftool --tool=meld
For each file in the diff you will be asked if you want to view it using meld.
Unstage an added file in Git
If you added a file by mistake, you can unstage it (but keep local changes) by saying
git reset HEAD path/to/file
This is also what git status
will tell you.
NB: It works for conflicts, too, if git checkout -- <file>
does not (although git tells you it would).
Git: Ignore whitespace when merging or cherry-picking
You can tell git to ignore different kinds and amounts of whitespace when merging or cherry-picking. This often occurs when you changed indentation, or converted tabs to spaces.
Simply use
git merge <REV> -Xignore-space-change
or
git cherry-pick <REV> -Xignore-space-change
Git: Twelve Curated Tips And Workflows From The Trenches
This article contains:
- Making ‘git diff’ wrap long lines
- Set a global proxy
- Clone only a specific branch
- Diff file against remote branch
- List all deleted files in the repository
- Search for a string in all revisions of entire git history
- Apply a patch from another (unrelated) local repository
- Making a more recent branch the new master
- Adding an initial empty commit to a branch to allow full rebase
- Zero a branch to do something radically different
-...
Show the current Git branch on your Bash prompt
Append this to your ~/.bashrc
:
export PS1='\[\033[01;32m\]\h\[\033[01;34m\] \w\[\033[31m\]$(__git_ps1 "(%s)") \[\033[01;34m\]$\[\033[00m\] '
Reload the changes by saying
source ~/.bashrc
For some customized prompts that also show the current Git prompt, see the examples section at the bottom of our bash prompt customizing card.
How to fix a corrupt git index
If your git index for some reason becomes invalid, no need to worry.
Your index is corrupt when you see this error running usual git commands like git pull
, git status
, etc.:
error: bad index file sha1 signature
fatal: index file corrupt
Though it sounds bad, your changes are still there. Fix it by first removing the index file, then resetting the branch:
rm .git/index
git reset
You should be all good now.
To be safe, make a backup of .git/index
before you delete it.
Git: Diff per word or character
By default git diff
highlights whole lines as changes.
To diff on a word-by-word basis you can say:
git diff --color-words
To diff on a character-by-character basis you can say:
git diff --color-words=.
How to tidy up your Git mess
Git is hard: screwing up is easy, and figuring out how to fix your mistakes is fucking impossible. Git documentation has this chicken and egg problem where you can't search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem.
Maybe this flowchart may be of help...
mojombo/grit - GitHub
Grit gives you object oriented read/write access to Git repositories via Ruby.
gerrit - Project Hosting on Google Code
Gerrit is a web based code review system, facilitating online code reviews for projects using the Git version control system.
Git: Listing branches with their latest author
Run this command to list the authors of the most recent commit of each branch:
git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n
Credits go to DarVar on SO.
Shell script to generate a Git commit with Pivotal Tracker story ID and title
We usually generate our commit messages from Pivotal Tracker IDs and titles, like
[#15775609] Index view for conflicts
The geordi command commit
automates this. (See: Pretty Commit messages via geordi).
Just run geordi commit
and it will connect to PT and let you select from a list of all started and finishes stories. Then it runs git commit
with the generated message (i.e. all staged changes will be commited).
When running for the first time, Geordi will request your PT...
A few git tips you didn't know about
Random list of useful git commands for advanced users. I found some of them useful.