FAQ¶
This page contains a number of frequently asked questions (at the moment only related to Git, but we may add more FAQs to this page)
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-20-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.
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
What does it mean when Git tells me my 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-20/username.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@mit.cs.uchicago.edu:cmsc12100-aut-20/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-20-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 on 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.