Git FAQ

Here you can find the questions I commonly ask myself while managing my source code, over and over again. This serves as a reference to those tasks which are common, but not quite common enough to remember easily. If any of the terms used here are unclear, you can refer to the Git Glossary.

How do I set up a repo for some already-existing files?

How do I set up a remote repo for some already-existing files?

Same as above, plus:

How do I "fork" a repo without using GitHub?

Sometimes I want to create a private copy of a repo, which GitHub does not allow. For example, how would I "fork" something from GitHub to alternative service Bitbucket? This is almost equivalent to just cloning the repository, with some small additions. This is different from purely duplicating the repository.

Let's say you want to create some new-invention on Bitbucket based on some prior-art from GitHub. In essence, what you want is (1) a "mirror clone" of the prior art, and (2) a way to pull changes from the upstream prior art and merge them with your own new invention.

To set up the new repository (mirror clone):

Whenever you want to grab the latest changes from the prior art:

You must specify the branch you want to pull. This merges upstream/master into whatever local branch you're currently on (the output of git branch). Don't accidentally merge two branches that aren't supposed to be the same!

How do I list all tracked files?

How do I undo something?

You have (at least) four choices.

    • To erase changes in your working copy.
    • “I made some changes but I don’t want them anymore.”
    • To fix just the most recent commit.
    • “I forgot to mention something in the commit message.”
    • “I forgot to add this one file to the commit.”
    • To fix local errors and errors in your working copy.
    • “Oops, I didn’t mean to ‘git add’ those changes.”
    • “I messed up a merge, I want to start over and try again.”
    • “I decided the last few commits were all wrong. I want to destroy them forever.”
    • “I decided the last few commits were all wrong. I want to modify them and re-commit.”
    • “I accidentally merged from the wrong branch, or merged the wrong commits. Where’s the reset button on this thing?!”
    • To fix already-pushed errors.
    • “Someone discovered something seriously wrong with my bugfix. We need to revert the change right away.”
    • “Someone ported a patch from a previous version of the software, but it doesn’t belong there. We just need to undo it.”

Just reset everything. Get me back in sync with everyone else.

Please, God, why is this so hard?! I just want what’s in the repository. I don’t care what’s in here now, I don’t care that it would be “overwritten by merge”—kill them with fire—nuke them from orbit—I JUST WANT WHATEVER EVERYONE ELSE HAS!

The only annoying thing about this is it still won't delete all your untracked files; you'll have to do that yourself.

Oopsie, I didn’t mean to ‘git add’ those changes.

See also the FAQ about different ways to undo things.

How do I undo an accidental commit?

See also the FAQ about different ways to undo things.

I moved some directories and I want to mark them as renames.

No need! Git will detect this automatically!

How do I pull down all remote branches?

Method 1:

For each remote branch in git branch -r:

Then execute once:

This method will leave your working copy where it is.

Method 2:

For each remote branch in git branch -r:

OR

This method will change your working copy to the checked-out branch.

Method 3:

You can also see how this appears in your .git/config, and even set it up manually.

How do I start my own branch?

To start a new branch at the current HEAD:

To start a new branch at <start-point> (can be branch name, commit-id, or tag):

If <start-point> is a remote branch then it will automatically be set up to track this branch.

How do I share my branch with others?

Once you've started your branch and you're ready to share it with the world, use this to push it for the first time:

This is telling git two things:

  1. Push this branch to origin under the name <remote-branch-name>
  2. Set up this branch to track the remote branch.

<remote-branch-name> will typically be the same as your local branch name. You only need to specify the destination the very first time; afterwards you can call simply git push.

Start a new branch based on my current changes.

I made a whole ton of changes, and all of a sudden I now realize that this is too big to go into a few commits, it really needs its own branch. How do I start a new branch after the fact?

Answer: the same way you start a branch normally!

Commit as normal, you are now on the branch.

Same as above, but I already committed the changes to master. Oops. How do I pull them out of master and into a new branch?

As long as you haven’t pushed the changes, you can do this: Commit everything first, and then:

You’re now on the new branch.

How do I diff my current branch with master?

My diff shows nothing but I have made changes! Where are they?

If you git add your changes then they will no longer show up in git diff. This is because diff shows the difference between the working copy and the index. If you want to see the difference between the index and the current branch, you need to:

How do I see the affects of a single commit?

(In other words, how do I diff any commit with its parent?)

How do I diff any two things?

This works between any two commits, or any two refs, in the whole repository. If you specify only one commit/ref, then newthing defaults to HEAD. To diff any two files, you can use:

How do I find the source of a buggy commit?

This command is super cool!

Or, if you already know the faulty line(s) of code, but you want to know why they were committed:

What are the ways to refer to a commit?

How the hell do I refer to one of these “commitishes”? Do I really have to copy-paste this whole damn SHA-1 hash??

Luckily, no. There are many ways of specifying a commit:

  • By its SHA1 name, which you can get from git log.
  • By the first few characters of its SHA1 name.
  • By a head. For example, HEAD refers to the commit object referenced by HEAD. You can also use the name, such as master.
  • Relative to a commit. Putting a caret (^) after a commit name retrieves the parent of that commit. For example, HEAD^ is the parent of the current head commit. origin/master^ would be the parent of wherever the remote repository currently is at.
  • Relative to a commit. You can also use the notation HEAD~1 to walk an arbitrary number of steps back in history. HEAD~1 == HEAD^ and HEAD~3 == HEAD^^^
  • You can also say: HEAD@{-1}. But this is a very special case! It refers to wherever HEAD was last, which could be backward or forward or on a whole other branch! Read about the reflog before you use it.

How do I commit a file once and ignore it thereafter?

This often comes up when you have a template file that programmers will customize to their liking. You don't want their customizations to make it into the team's central repo. However, you do want some kind of default version, for those who don't customize it.

If you place this file in your .gitignore, then you'd have to create it from scratch every time you clone a fresh copy of the repo. That's no good. Instead, commit the default version of the file, then do this:

After that any changes to this file will no longer show up in git status. But you'll still be able to commit new changes to the default by explicitly adding them to the index:

Recommended graphical clients?

  • GitX on Mac, TortoiseGit on Windows, ?? on Linux
  • Versions on Mac, TortoiseSVN on Windows, ?? on Linux

Recommended graphical differs?

Beats me.

Well then what do you use?

I use an IDE, but lately I’ve been using them only for hairy conflicting merges. IntelliJ IDEA and Eclipse both work well.

But I don’t wanna use an IDE!

Well then use vimdiff, you hippie.

I already have an SVN repository but I want to start using Git.

If migrating from SVN, don't use git-svn, git-svn slow and terrible; use SubGit instead, SubGit is fast and awesome.

Comments are closed.