A The shell

Even if you do most of your Git operations via a client, such as RStudio or GitKraken, you must sometimes work in the shell. As you get more comfortable with Git, you might prefer to do more and more via the command line.

A.1 What is the shell?

The shell (or bash or terminal or Command Prompt on Windows) is a program on your computer whose job is to run other programs, rather than do calculations itself. The shell is a very old program and in a time before the mouse it was the only way to interact with a computer. It is still extremely popular among programmers because it is very fast, concise, and is particularly powerful at automating repetitive tasks.

Here we use the shell for quite modest goals: to navigate the file system, confirm the present working directory, configure Git, and configure Git remotes.

A.2 Starting the shell

In RStudio, go to Tools > Shell. This should take you to the shell (on Mac: Terminal, on Windows: GitBash or equivalent). It should be a simple blinking cursor, waiting for input and look similar to this (white text on black background, or black text on white background):

A.3 Using the shell

The most basic commands are listed below:

  • pwd (print working directory). Shows directory or “folder” you are currently operating in. This is not necessarily the same as the R working directory you get from getwd().
  • ls (list files). Shows the files in the current working directory. This is equivalent to looking at the files in your Finder/Explorer/File Manager. Use ls -a to also list hidden files, such as .Rhistory and .git.
  • cd (change directory). Allows you to navigate through your directories by changing the shell’s working directory. You can navigate like so:
  • go to subdirectory foo of current working directory: cd foo
  • go to parent of current working directory: cd ..
  • go to your “home” directory: cd ~ or simply cd
  • go to directory using absolute path, works regardless of your current working directory: cd /home/my_username/Desktop. Windows uses a slightly different syntax with the slashes between the folder names reversed, \, e.g. cd C:\Users\MY_USERNAME\Desktop.
    • Pro tip 1: Dragging and dropping a file or folder into the terminal window will paste the absolute path into the window.
    • Pro tip 2: Use the tab key to autocomplete unambiguous directory and file names. Hit tab twice to see all ambiguous options.
  • Use arrow-up and arrow-down to repeat previous commands. Or search for previous commands with CTRL + r.

A few Git commands:

  • git status is the most used git command and informs you of your current branch, any changes or untracked files, and whether you are in sync with your remotes.
  • git remote -v lists all remotes. Very useful for making sure git knows about your remote and that the remote address is correct.
  • git remote add origin GITHUB_URL adds the remote GITHUB_URL with nickname origin.
  • git remote set-url origin GITHUB_URL changes the remote url of origin to GITHUB_URL. This way you can fix typos in the remote url.
  • we should add more

A.4 Note for Windows users

On Windows, the program that runs the shell is called Command Prompt or cmd.exe. Unfortunately, the default Windows shell does not support all the commands that other operating systems do. This is where GitBash comes in handy: it installs a light version of a shell that does support all the above commands. When you access the shell through RStudio, RStudio actually tries to open GitBash if it can find it, but it will open the default Windows Command Prompt if GitBash is not found.

If you get an error message such as 'pwd' is not recognized as an internal or external command, operable program or batch file. from any of the previous commands, that means that RStudio could not find GitBash. The most likely cause of this is that you did not install git using the recommended method from this book, so try re-installing git.