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.
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.
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?
Undo a commit and redo
$ git commit ... (1) $ git reset --soft HEAD^ (2) $ edit (3) $ git add .... (4) $ git commit -c ORIG_HEAD (5)
This is what you want to undo
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".
Make corrections to working tree files.
Stage changes for commit.
"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.
What's the difference between
git pull and
In the simplest terms,
git pull does a
git fetch followed by a
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).
git pull is what you would do to bring your repository up to date with a remote repository.
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?
You can also
git reset FILE, which will remove it from the current index (the "about to be committed" area) without changing anything else.
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.
$ git branch -D bugfix Deleted branch bugfix (was 2a14ef7).
$ 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?
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.
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 firstname.lastname@example.org: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
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).
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.
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
$ 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 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?
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.
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.
Given a branch
foo and a remote
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
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.
How do I remove a Git submodule?
And by the way, is there a reason I can't simply do
git submodule rm whatever
Via the page Git Submodule Tutorial:
To remove a submodule you need to:
git rm --cached path_to_submodule(no trailing slash).
rm -rf path_to_submodule
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.
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.
A git repository is simply a directory containing a special
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.
git init in the directory which contains the files you wish to track.
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!