Top git Questions

List of Tags
2521
Laurie Young

I stupidly did a Git commit while half asleep, and wrote the totally wrong thing in the commit message. How do I change the commit message?

I have not yet pushed the commit to anyone.

Answered By: bueno ( 2708)
git commit --amend -m "New commit message"

Used to amend the tip of the current branch. Prepare the tree object you would want to replace the latest commit as usual (this includes the usual -i/-o and explicit paths), and the commit log editor is seeded with the commit message from the tip of the current branch. The commit you create replaces the current tip -- if it was a merge, it will have the parents of the current tip as parents -- so the current top commit is discarded.

It is a rough equivalent for:

$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD

but can be used to amend a merge commit.

2170
Hamza Yerlikaya

I accidentally added the wrong directory containing my files in Git. Instead of adding a .java file, I added the directory containing the .class file. How can I undo this action?

Answered By: Esko Luontola ( 2020)

http://git-scm.com/docs/git-reset

Undo a commit and redo

$ git commit ...              (1)
$ git reset --soft HEAD^      (2)
$ edit                        (3)
$ git add ....                (4)
$ git commit -c ORIG_HEAD     (5)
  1. This is what you want to undo

  2. This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".

  3. Make corrections to working tree files.

  4. Stage changes for commit.

  5. "reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.

1353
J. Pablo Fernández

What's the difference between git pull and git fetch?

Answered By: Greg Hewgill ( 1221)

In the simplest terms, git pull does a git fetch followed by a git merge.

You can do a git fetch at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn't recommend doing this).

A git pull is what you would do to bring your repository up to date with a remote repository.

1313
ceretullis

I mistakenly added files using the command

git add file

I have not yet run git commit. Is there a way to undo this or remove these files from the commit?

Answered By: genehack ( 1204)

You can also git reset FILE, which will remove it from the current index (the "about to be committed" area) without changing anything else.

1272
Matthew Rankin

I created a bugfix branch to fix a bug on a project that I had forked on GitHub. I issued a pull request to the developer to incorporate my fix, but the developer decided to implement a different fix for the problem. At this point, I want to delete the bugfix branch both locally and on my project fork on GitHub.

Successfully Deleted Local Branch

$ git branch -D bugfix
Deleted branch bugfix (was 2a14ef7).

Attempts to Delete Remote Branch

$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.
$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).
$ git push
Everything up-to-date
$ git pull
From github.com:gituser/gitproject
 * [new branch]         bugfix  -> origin/bugfix
Already up-to-date.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?

Answered By: Matthew Rankin ( 1682)

Updated Answer on 1-Feb-12

As of Git v1.7.0, you can delete a remote branch using

git push origin --delete <branchName>

which is easier to remember than

git push origin :<branchName>

which was added in Git v1.5.0 "to delete a remote branch or a tag."

Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.

Original Answer from 5-Jan-10

From Chapter 3 of Pro Git by Scott Chacon:

Deleting Remote Branches

Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s master branch (or whatever branch your stable codeline is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your serverfix branch from the server, you run the following:

$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
 - [deleted]         serverfix

Boom. No more branch on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”

I issued git push origin :bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on StackOverflow).

1122
Peter Coulton

I have a master and a dev branch, both pushed to github, I've cloned, pulled, fetched, but I remain unable to get anything other than the master branch back.

I'm sure I'm missing something obvious, but I have RTM and I'm getting no joy at all.

Answered By: emk ( 1520)

First, clone a remote git repository and cd into it:

$ git clone git://example.com/myproject
$ cd myproject

Next, look at the local branches in your repository:

$ git branch
* master

But there are other branches hiding in your repository! You can see these using the -a flag:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental

Now, if you look at your local branches, this is what you'll see:

$ git branch
* experimental
  master

You can actually track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets

At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &

Is there a good way to explain how to resolve merge conflicts in Git?

Answered By: Peter Burns ( 655)

Try: git mergetool

It opens a GUI that steps you through each conflict and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. Much better than doing the whole thing by hand certainly.

905
Pat Notz

I know how to make a new branch that tracks remote branches. But how do I make an existing branch track a remote branch. I know I can just edit the .git/config file, but it seems there should be an easier way.

It looks like this can't currently be done in a convenient way with the current (1.6.1.x) version of Git.

Git version >= 1.7.0 supports this. See the accepted answer.

Answered By: Dan Moulding ( 1162)

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of Git 1.7.0:

git branch --set-upstream foo upstream/foo

Notes:

All of the above commands will cause local branch foo to track remote branch foo from remote upstream. The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.


See also: Git: Why do I need to do `--set-upstream` all the time?

879
R. Martinho Fernandes

How do I remove a Git submodule?

And by the way, is there a reason I can't simply do

git submodule rm whatever

?

Answered By: John Douthat ( 1318)

Via the page Git Submodule Tutorial:

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit
  5. Delete the now untracked submodule files
    rm -rf path_to_submodule
859
Adam Davis

Ok, after seeing this post by PJ Hyett, I have decided to skip to the end and go with Git.

So what I need is a beginner's practical guide to Git. "Beginner" being defined as someone who knows how to handle their compiler, understands to some level what a Makefile is, and has touched source control without understanding it very well.

"Practical" being defined as this person doesn't want to get into great detail regarding what Git is doing in the background, and doesn't even care (or know) that it's distributed. Your answers might hint at the possibilities, but try to aim for the beginner that wants to keep a 'main' repository on a 'server' which is backed up and secure, and treat their local repository as merely a 'client' resource.

So:

Installation/Setup

Working with the code

Tagging, branching, releases, baselines

Other

Other Git beginner's references

Delving into Git

I will go through the entries from time to time and 'tidy' them up so they have a consistent look/feel and it's easy to scan the list - feel free to follow a simple "header - brief explanation - list of instructions - gotchas and extra info" template. I'll also link to the entries from the bullet list above so it's easy to find them later.

Answered By: dbr ( 118)

How do you create a new project/repository?

A git repository is simply a directory containing a special .git directory.

This is different from "centralised" version-control systems (like subversion), where a "repository" is hosted on a remote server, which you checkout into a "working copy" directory. With git, your working copy is the repository.

Simply run git init in the directory which contains the files you wish to track.

For example,

cd ~/code/project001/
git init

This creates a .git (hidden) folder in the current directory.

To make a new project, run git init with an additional argument (the name of the directory to be created):

git init project002

(This is equivalent to: mkdir project002 && cd project002 && git init)

To check if the current current path is within a git repository, simply run git status - if it's not a repository, it will report "fatal: Not a git repository"

You could also list the .git directory, and check it contains files/directories similar to the following:

$ ls .git
HEAD         config       hooks/       objects/
branches/    description  info/        refs/

If for whatever reason you wish to "de-git" a repository (you wish to stop using git to track that project). Simply remove the .git directory at the base level of the repository.

cd ~/code/project001/
rm -rf .git/

Caution: This will destroy all revision history, all your tags, everything git has done. It will not touch the "current" files (the files you can currently see), but previous changes, deleted files and so on will be unrecoverable!