Skip to content

Instantly share code, notes, and snippets.

@zachallaun
Last active December 24, 2015 14:49
Show Gist options
  • Save zachallaun/6815205 to your computer and use it in GitHub Desktop.
Save zachallaun/6815205 to your computer and use it in GitHub Desktop.
A series of questions that someone might ask if they wanted to learn more about git.

A bit beyond git basics: Q&A edition

Questions:

  • How do I create a new branch?
  • How do I switch to another branch?
  • How do I create and switch to a new branch in a single command?
  • I've made whatever changes I want on my new branch. How do I merge these changes back into master?
  • After merging, git told me that I made a "fast-forward merge." What does that mean?
  • After merging, git told me that there were auto-merge conflicts. What does that mean and how do I fix it?
  • I committed in error. How do I undo a commit without losing my changes?
  • I hate my last commit. How do I undo it completely without keeping the changes?
  • I forgot to add some change to my last commit. How can I fix it?
  • I messed up my last commit message. How can I fix it?
  • How can I stage only some of the modifications to a file?
  • How do I ignore files?
  • I accidentally began tracking a .pyc file in a git commit -a before I realized that it was a terrible, terrible thing to do. How do I begin ignoring it?

Q: How do I create a new branch?

git branch <my-branch>

This creates a new branch that points to whichever commit you're currently on. Remember though that it doesn't change your current branch – it only creates a new one.

Q: How do I switch to another branch?

git checkout <my-branch>

This will change your current branch to <my-branch>, updating your filesystem to reflect the contents of whichever commit that branch points to.

Q: How do I create and switch to a new branch in a single command?

git checkout -b <my-branch>

Q: I've made whatever changes I want on my new branch. How do I merge these changes back into master?

(Make sure you're on whichever branch you're merging into – in this case, master.)

git checkout master
git merge <my-branch>

Q: After merging, git told me that I made a "fast-forward merge." What does that mean?

You branched off of the same branch that you're merging into, and it hasn't moved forward in the meantime. Your history might look something like this:

  2 - <my-branch>
  |
  1
 /
0 - master

When you merged <my-branch> into master, all that had to happen was to update master to point at the same spot as <my-branch>.

2 - <my-branch>, master
|
1
|
0

Q: After merging, git told me that there were auto-merge conflicts. What does that mean and how do I fix it?

Git tried to merge your two branches together, but found that some of the changes conflicted. If you type git status, you'll see that git was unable to merge some (or all) of the changed files. You'll need to manually resolve conflicts in the un-merged files.

If you open one of these files, you'll see something like this:

<<<<<<< HEAD
print "good luck"
=======
print "!!!"
>>>>>>> <my-branch>

This indicates the site of a conflict, including the contents of the file on the branch you're merging into (print "good luck"), and the contents of the file on the branch you're mergine (print "!!!"). To fix the conflict, just edit the file as you see fit.

print "good luck!!!"

As you fix conflicting files, add them to the staging area as usual with git add, and then use git commit to complete the merge.

Q: I committed in error. How do I undo a commit without losing my changes?

git reset --soft HEAD~1

A "soft reset" will move your branch back a number of commits (in this case 1), leaving the changes those commits introduced in your staging area.

Q: I hate my last commit. How do I undo it completely without keeping the changes?

git reset --hard HEAD~1

A "hard reset" will move your branch back a number of commits (in this case 1). None of the changes introduced by those commits will be in your staging area. (Note though that you don't ever lose those changes. If you do a hard reset in error, stop sweating and look up git reflog.)

Q: I forgot to add some change to my last commit. How can I fix it?

Stage the changes you want to be a part of the last commit and create an amend commit.

git commit --amend

An amend commit will add new changes to the changes of the previous commit and give you an opportunity to re-craft the commit message, ultimately creating a new commit containing all the changes you've specified.

Q: I messed up my last commit message. How can I fix it?

git commit --amend

If you have no changes in your staging area, doing a commit --amend will still allow you to rewrite the last commit message, creating a new commit.

Q: How can I stage only some of the modifications to a file?

git add -p <my-file>

The patch flag (-p / --patch) allows you to stage parts of a single file. @akaptur already has a great blog post about git add -p.

Q: How do I ignore files?

Let's say you want git to ignore those pesky .pyc files that Python generates. Create a .gitignore file in the root of your repository, containing this:

*.pyc

Stage and commit the file. This tells git to begin ignoring all files (even those contained in subdirectories) with the extension .pyc. If you later decide that there are other files or directories that need to be ignored, simply add them on new lines.

*.pyc
deps/
notes-tmp

Q: I accidentally began tracking a .pyc file in a git commit -a before I realized that it was a terrible, terrible thing to do. How do I begin ignoring it?

Git won't forget about a file that's already being tracked, even if it's extension is contained in your .gitignore. You have to tell git to "forget" about the file.

git rm --cached *.pyc

The --cached flag means that you don't mean to delete it from your file system, just that you want git to stop tracking it. This command added one or more deletions to your staging area, which you'll have to commit to finish up the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment