Making a GIT Repository

You've installed git, and you want to create a repository. You can do this in an empty directory, but probably you already have a project you think should be under version control. cd into the project directory, and type git init. This isn't dangerous: it makes the folder a git repo ... with no committed files, and without changing any of the contents of the folder. Mechanically, all it does is add a .git/ folder to the project folder (there's a bunch of stuff in there, but it's best to ignore the man behind the curtain).

Before you go any further, let's do one basic bit of configuration. By default on Linux systems, the git editor is nano - and I'm not having that. So let's change that: git config --global core-editor "vim" will change git's default editor to something better.

Now start adding files to the repo. You don't have to add all of them: use git add <filename> <foldername> <...> to "stage" stuff. To "stage a file" (or everything in a folder) means that you consider it ready to be added to the repository: you haven't actually put it into the repo yet. Then git commit -a will get you into the git editor, which will show you what you're committing (and what you're not committing) and ask you to add a commit message. If you modify the file and save it, a commit will be made - but you can cancel the commit by exiting the editor without saving anything.

git status is always your friend. Use it. Frequently. This will tell you what's changed and what's staged. And commit early and often: making lots of commits is NOT a problem, and may save you later.

Git is an incredibly complex source code management system - it was designed for thousands of developers to work on the Linux kernel, including multiple simultaneous branches. Honestly, I'm only scratching the surface of what it can do. If you're new to it, work with this first: work on your project and commit frequently. Get to branching and servers when you need it, no rush.

But ... I need a server, so that's the next step in this little tutorial. This is actually pretty easy, although what's happening may not be terribly clear. I have the server set up, so I'm not covering that here - just how to get a new project onto an existing server. Go to the parent directory of your git-controlled project, and type git clone --bare projectX projectX.git . This will create a new folder called projectX.git/, a "bare" copy of the repo meant for a server (not for you to work in: things are stored differently). Next, run scp -r projectX.git gituser@myserver.com:/home/gituser. And you're done. After that, on any other machine, git clone gituser@myserver.com:/home/gituser/projectX.git new_folder ("new_folder" is optional, by default it will create "projectX" - notice without the ".git"). I would recommend removing the local copy: rm -rf projectX.git ("-f" is needed as many of the items in .git/ are read-only). The repo you just cloned from doesn't know you've added an upstream, so you have to tell it: cd into the repo and run git remote add origin gituser@myserver.com/home/gituser/projectX.git . And in projectX/ you can now type git push origin master.

I highly recommend Pro Git v2: it's detailed, well structured, and readable and is about as good as technical documentation comes ... plus it's free and online. If you need a server, read Git on the Server.

Update

2017-05-06: I've found myself using this entry a lot to create repos. So I'm going to add this in a stripped form, but with a bit more detail ... (and excessively colourful HTML).

~$ cd interestingproject
interestingproject$ ls
README.txt      code.py      morecode/
interestingproject$ git init
Initialized empty Git repository in /home/giles/interestingproject/.git/
interestingproject$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

      README.txt
      code.py
      morecode/

nothing added to commit but untracked files present (use "git add" to track)
interestingproject$ git add README.txt code.py morecode/**
interestingproject$ git status
On branch master

Initial commit

Changes to be committed:
    (use "git rm --cached ..." to unstage)

        new file:   KeyboardMessagePullup/KeyboardMessagePullup.ino
        new file:   KeyboardMessagePullupWithBlink/KeyboardMessagePullupWithBlink.ino
        new file:   README.txt
        new file:   StretchBlink/StretchBlink.ino
        new file:   UglyBlink/UglyBlink.ino
        new file:   libraries/readme.txt

interestingproject$ git commit
[master (root-commit) 977a07f] Initial commit of Interesting Project(TM).
 6 files changed, 229 insertions(+)
 create mode 100644 README.txt
 create mode 100644 code.py
 create mode 100644 morecode/README.txt
 create mode 100644 morecode/backup.py
interestingproject(master)$ cd ..
~$ git clone --bare interestingproject interestingproject.git
Cloning into bare repository 'interestingproject.git'...
done.
~$ scp -r interestingproject.git gituser@myserver.com:~/
config                                                                    100%  137    16.1KB/s   00:00
exclude                                                                   100%  240    10.8KB/s   00:00
pre-push.sample                                                           100% 1348   105.3KB/s   00:00
pre-rebase.sample                                                         100% 4951   268.3KB/s   00:00
update.sample                                                             100% 3610   330.2KB/s   00:00
pre-applypatch.sample                                                     100%  424    43.8KB/s   00:00
commit-msg.sample                                                         100%  896    69.3KB/s   00:00
post-update.sample                                                        100%  189    17.5KB/s   00:00
prepare-commit-msg.sample                                                 100% 1239    70.6KB/s   00:00
applypatch-msg.sample                                                     100%  478    46.9KB/s   00:00
pre-commit.sample                                                         100% 1642   153.0KB/s   00:00
d5024a125e81002454258282b0a80ce18e3bf8                                    100%   58     6.5KB/s   00:00
ebcc21c9e9c1bad4c769968569b85f383f3064                                    100%  359    33.1KB/s   00:00
2011668008a8d81c181c90048ad7a0d2edad92                                    100%   79     7.9KB/s   00:00
dd11653f8f363e663119a01795de31a8f7ff90                                    100%   70     7.0KB/s   00:00
64bcbdb209f092b6a5c21f5165fa7d079ae7d1                                    100% 1068   106.5KB/s   00:00
201cebb4de048a019f0f6a267875fe33d28622                                    100%   61     5.3KB/s   00:00
f2cdb97709d840239de8ff6737d1367b85d40f                                    100%  464    54.3KB/s   00:00
7a07fb1a41c00cbdbd6155ed4ae7a2c0a9052d                                    100%  144    16.0KB/s   00:00
7bcf6e8c58be9f4208b976af1666746984f33e                                    100%   55     4.7KB/s   00:00
ce674fe0cc3be65c6d8872a3b109caaad73f07                                    100%   92    10.2KB/s   00:00
b83e825e795820ff65e1798619bb9e0792b843                                    100%  951    61.8KB/s   00:00
cdf5bc81ae6c3866e28d988fefe002148a0dd2                                    100%  229    27.2KB/s   00:00
HEAD                                                                      100%   23     2.6KB/s   00:00
packed-refs                                                               100%   98    11.1KB/s   00:00
description                                                               100%   73     8.5KB/s   00:00
~$ rm -rf interestingproject.git/
~$ cd interestingproject/
interestingproject(master)$ git remote add origin gituser@myserv.com:~/interestingproject.git   # TYPO!!!
interestingproject(master)$ git pull
ssh: Could not resolve hostname myserv.com:~: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
interestingproject(master)$ git remote -v
origin  gituser@myserv.com:~/interestingproject.git (fetch)
origin  gituser@myserv.com:~/interestingproject.git (push)
interestingproject(master)$ git remote add origin gituser@myserver.com:~/interestingproject.git
fatal: remote origin already exists.
interestingproject(master)$ git remote set-url origin gituser@myserver.com:~/interestingproject.git
interestingproject(master)$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

interestingproject(master)$ git push --set-upstream origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date
interestingproject(master)$