Guide to initializing git repositories and maintaining them

Setting Up Local and Remote Repositories

  1. Create a new project.
  2. Go to your command line (Git-bash) and navigate to your project directory.
  3. In your command line, execute the command git status. This should return an error. If it doesn't you have a git repository set up in a parent directory of your current one.
  4. Now create a git repository in your project directory: git init.
  5. With a new repository initialized, re-check the status of your project: git status. This will show you all the untracked files inside of your repository.
  6. Before we start tracking files, it's important that we set up a .gitignore file that specifies the files that should not be part of our source code.
  7. In git bash, create a .gitignore file with the command touch .gitignore in the root of your project
  8. Go to gitignore.io and select whatever IDE (Eclipse, Visual Studio, etc) and language (Java, C#, Node, etc) and then click the create button.
  9. Select all in the file that was generated and copy it.
  10. Open the .gitignore file you made in your project folder with Notepad or a similar text editor and paste the generated .gitignore file contents to it. Save it and close it.
  11. Now it's time to track your files so they will be included in your commit. Generally, it's better to explicitly add things, but this time let's add everything in the current folder (represented by . in the following command) for simplicity's sake. That command is git add ..
  12. git status again. All of your files should have changed color from red to green.
  13. Now, with your green files, you are ready for your commit: git commit -m "Add a descriptive message here so you can reference it later".
  14. With all of your changes committed, you're ready to push them up to your remote repository: git push
  15. Uh oh. You got a fatal error. This is because we don't have a remote repository connected to our local repository yet. Let's do that next.
  16. In your browser, navigate to Github, hit the plus sign in the top right corner and create a new repository.
  17. Name this new repository the same thing that your project directory is named (i.e. your-projects-directory-name). Copy and paste to minimize the possibility of typos. You shouldn't change anything else in this window.
  18. Push the button at the bottom Create repository to create a new repository.
  19. Creating the repository will take you to a page that with a heading reading Quick setup — if you’ve done this kind of thing before. What we are interested in are the commands in the …or push an existing repository from the command line section. Copy these and paste them to your command line (<ctrl> + <shift> + <V>). Be sure to hit <enter> to submit the final line.
  20. Your local repository and remote repository are now connected.
  21. Try that git push once more and...well, we're almost there. Read the error message and follow it's instructions and your next push should work!
  22. If you go back to your browser and hit the refresh button you will see your project files are now housed inside of your remote repo.

Pushing Future Changes from Local to Remote

So you have changes now and you're ready to push them up. Let's get started.

  1. First, navigate to your command line and execute git status. This should produce some untracked or modified files. That's good. Let's get them tracked.
  2. Track your files with git add .
  3. Execute a git status again to double check that everything is being tracked.
  4. Now commit your changes: git commit -m "Another descriptive message to let you/other devs know what happened since the last change".
  5. Now it's time to push. You don't need the two lines you used from your first push this time. The command for all pushes after the first one is: git push. This is because your local and remote repos are connected and mapped to each other. This command just tells the local to use that path and push the new information to it.
  6. In your browser, navigate to Github and then your project repo. You should see the current version of your project housed in your remote repo now.