Beginner question: setting up Git with Terminal on Mac

Dear all,

this is my first ever post and, please, forgive me if this sounds as a silly question but I have browsed many guides and posts and not found any solution to this.

As a complete beginner with Git I am studying the ’ Pro Git’ book by Scott Chacon and Ben Straub.

The authors suggest doing everything via the Command Line (I am on macOS High Sierra 10.13.4) so I am following their instructions but … when I get to 

Initializing a Repository in an Existing Directory

I get asked to go to the directory I want to initialise in Terminal and to write its path followed by ‘git init’. 

When I do that on an Xcode project folder I get the error: ‘it is a directory’. Fine.

Then I simply write $ cd \Users&lt;username>&lt;nameOfAFolder> git init and press Return and I just get the title of the Terminal window changed to <nameOfAFolder> … while the book said I should get a ‘.git’ empty folder somewhere. 

Could you tell me what am I making wrong? 

I have already installed GitHub Desktop so I already have a GitHub folder into my Documents directory but I wanted to try the Command Line way anyway. 

My doubt is also: is Git wanting a ‘home’ directory where all of my repositories will be stored or once I drag a folder to GitHub Desktop / manage to initialise it via Terminal it will be connected directly?

Thank you very much for yourtime and patience.

I’d like to try an revise this comment later when I have more time however here is as much as I can get out.

In whatever directory you are in, are you typing ls -A when you look for the .git folder in the terminal? Finder won’t show it by default, and you must specific with terminal commands as the -A means show all files.

dotfiles like .git are any file with a dot before the name, which aren’t shown in Finder by default, and must be specified to show with the ls command in the terminal.

1 Like

Thank you Jahhein!

I have actually made some progress in the meantime.

Managed to show the hidden folders with the shortcut ‘Cmd-Shift-.’ and see all the mess that I had been doing! 

Using the ‘cd’ command before the desired directory I managed to focus the Terminal on that very directory.

Then I ran ‘git init’ which created the .git folder inside.

Then ‘git add .’ to add all the files to the repository.

Then ‘git commit’ which prompted me to write a message, which I did in the last line after the lot of #### and file paths, but then it seems I am stuck. 

How do I get out of there?

Thank you for your time!

Did you actually create any files that could be added to the commit?

Whatever folder/directory you are in, wether it be on a USB, inside a file in your Downloads folder, or wherever, whatever Folder/Directory you are in when you type git init is the top level of the repository.

Whats missing from your explaination is any file that could have been commited. type these commands out inside your repo folder and try again.

git init; echo “Hello World!” >> README.md ; git add . ; git commit -am “Test commit”;

copy the above series of terminal commands inside the folder you’ve been doing it, and then if it doesn’t work, do it in a new one somewhere.

I chose a folder of an Xcode project I am using to learn Swift.

So now there is a .git folder with all of the files inside. 

When I type ‘git status’ it gives me a list of files in green color.

Just I could not get out of the Message part at the end when I entered ‘git commit’. 

But maybe it was because I had not set the Editor properly. 

It was set to Atom from within GitHub Desktop but it didn’t want to launch it from Terminal.

I changed it via the --global command to be Emacs and it worked … just it didn’t launch the app (and, from GitHub Desktop’s Preferences it seems Atom is still the preferred one without any chance to change it … maybe this is the issue?).

Then type the commit command like this:

git commit -am “here is the git commit message which bypasses git opening up an editor”

Sadly this message board doesn’t support code formating in the posts. anything between backticks `` is the command in previous posts of mine. Whats between the backticks is what the command is. don’t include them.

To change the git configuration for the editor the command is…

git config --global core.editor TheEditorYouWantHere

Just In case you changed it while inside the repo which takes presidence over global git config, run this while inside the repo directory:

git config --local core.editor TheEditorYouWantHere

personally i’d just use nano/terminal emacs in the terminal because you’re already in the terminal.

2 Likes

Thank you very much!

This solved it!

Thank you for guiding me through my first step in this new beautiful world!

You’re very welcome! Glad I could help you.

You will in time learn how it all works. Just realize all the commands you type are like mini programs. cd change directory, ls list, pwd print working directory, etc.

Git is just simply a program without an interface. Don’t be intimidated. Its all repitition and memorization. When in doubt always remember the man command for manual pages.

man git

man git-commit

man git-add

or…

git help commit

1 Like