Git FAQ

I didn’t specify a commit message, and now I’m in a screen I don’t know how to get out of

When you forget to specify the -m option to the git commit command, Git will open a command-line text editor for you to specify a commit message. You will typically end up in either the nano text editor or the vim text editor. While you are welcome to learn how to use either editor (and use them to type in more elaborate commit messages), here we will simply explain how to exit the editor so you can specify the message using the -m option.

If you see something like this at the bottom of the screen:

^G Get Help      ^O Write Out     ^W Where Is      ^K Cut Text      ^J Justify       ^C Cur Pos       ^Y Prev Page     M-\ First Line   M-W WhereIs Next
^X Exit          ^R Read File     ^\ Replace       ^U Uncut Text    ^T To Spell      ^_ Go To Line    ^V Next Page     M-/ Last Line    M-] To Bracket

You’re in the nano editor. Just press Control-X to exit. You will return to the shell, and this message will appear:

Aborting commit due to empty commit message.

This is normal. Just re-run git with the -m option.

If you see something like this at the bottom of the screen:

"~/cmsc12100-aut-18-borja/.git/COMMIT_EDITMSG" 67L, 2572C

You’re in the vim editor. Press Escape, and then type in the following:

:q!

And press Enter. As with nano, you will exit into the shell and you will get a message telling you the commit was aborted.

Trying to do a git pull upstream master takes me to a screen saying “Merge branch…”

When you run git pull upstream master you may be taken to a screen that starts with something like this:

Merge branch 'master' of mit.cs.uchicago.edu:cmsc12100-aut-18/cmsc12100-aut-18

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

Doing a git pull upstream master fetches the latest changes from our upstream repository, and merges them into your repository. This merge is done by wrapping up all of our latest changes into a new commit, which must have some message. Because this commit was automatically created, Git hasn’t yet given you a chance to specify what the message should be, so it opens up a text editor for you to edit a message.

First, read our answer to the previous question to identify what editor you are using. Once you’ve done so, simply do the following:

  • If you are using nano: Press Control+O (the letter, not 0 the number), then Enter to save the message. Then, press Control+X to exit.
  • If you are using vim: Press Escape and then type :wq and press Enter.

Doing this will save the commit and will complete the pull operation.

How can I recover an accidentally-deleted file?

Let’s say you accidently deleted a file named something.py. If that file has been previously added and committed to your repository, you can recover it by running this command:

git checkout -- something.py

How do I fix a “corrupt object”?

In some cases, running Git may result in errors that refer to a “corrupt object”.

A “corrupt object” or an “empty object” is typically caused when you forcibly stop your VM (e.g., by forcibly closing the window the VM is running in, without saving its state, or by forcibly shutting down your VM or your laptop). When working on a VM, it is important that you either save the VM state or perform an orderly shutdown of the VM when you’re done with it. Otherwise, your VM image could get corrupted (hence the “corrupt” and “empty” object issues).

If you are already encountering the “corrupt object” or “empty object”, there is unfortunately no easy fix. Fortunately, this issue seems to affect only Git’s internal data, not the files themselves. Your work is not lost, you just can’t commit/push the code because Git’s internal data is corrupted.

You will need to re-run the setup script to get a fresh copy of your repository:

cd
mv cmsc12100-aut-18-username cmsc12100-aut-18-username.broken
cs-setup-script cmsc12100-aut-18

(where username should be replaced with your CNetID)

The cmsc12100-aut-18-username directory will now contain the latest version of the code that you pushed to the server. You will need to copy the files that you modified since your last push from cmsc12100-aut-18-username.broken to cmsc12100-aut-18-username.

If you want to be on the extra-safe side, you may want to re-install a fresh VM on your machine, in case the VM you’ve been using so far has become corrupted after an abrupt stop.

What does it mean when Git tells me my your current branch is behind?

When trying to do a git push, you may get the following error message:

To git@mit.cs.uchicago.edu:cmsc12100-aut-18/username.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@mit.cs.uchicago.edu:cmsc12100-aut-18/username.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
student@CS-Vbox:~/cmsc12100-aut-18-username/pa1$

This error means that there are commits on the server that you pushed from somewhere else, and did not pull to your current location. Before you can push, you need to pull those commits from the server:

git pull

To avoid this problem from happening in the future, it is generally good practice to always run git pull before you start working on your code, to make sure you always have the latest version of the code from the Git server.

What are unmerged files?

When running git, you may sometimes get an error message like this:

Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use
'git add/rm <file>' as appropriate to mark resolution,
or use 'git commit -a'

This error message means that, at some point in the past, you did a git pull that included code that conflicted with the code on your machine. This situation is called a merge conflict, and Git needs you to resolve it manually, because it cannot tell whether you want the code on the server or the code on your machine to take precedence.

You can run git status to see what files need to be updated manually (under “Unmerged paths”). If you open these file(s) with an editor, you should see some parts of the file that look like this:

<<<<<<<
foobar
=======
foobaz
>>>>>>>

What Git is basically saying is “In one version, this line contained ‘foobar’, but in another version it contained ‘foobaz’, and I don’t know how to reconcile that difference”. You need to choose one of the two and determine which one you want to keep. If you wanted to keep foobar in the example above, you would replace everything above with foobar (i.e., remove the “<<<”, “===”, “>>>” lines too).

Once you’ve resolved these conflicts, just git add the files and commit as usual.

You may also want to check out this handy guide from GitHub. Please note that it explains merge conflicts in terms of Git “branches”, which you do not need to use in this course. For the purposes of reading this guide, you can think of your local repository as one branch and the copy in the Git server as the other branch.

How do I look at a previous version of a file I pushed to the git server?

Go to your project on https://mit.cs.uchicago.edu, and then click on “Commits” on the left bar. This will show you a list of all the commits you’ve pushed to the server. In each of them, there is a “Browse code” link that will allow you to see the state of the code at that precise commit.