Guide to initializing git repositories and maintaining them
Setting Up Local and Remote Repositories
- Create a new project.
- Go to your command line (Git-bash) and navigate to your project directory.
- 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. - Now create a git repository in your project directory:
git init
. - 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. - 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. - In git bash, create a .gitignore file with the command
touch .gitignore
in the root of your project - Go to gitignore.io and select whatever IDE (Eclipse, Visual Studio, etc) and language (Java, C#, Node, etc) and then click the create button.
- Select all in the file that was generated and copy it.
- 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. - 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 isgit add .
. git status
again. All of your files should have changed color from red to green.- 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"
. - With all of your changes committed, you're ready to push them up to your remote repository:
git push
- 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.
- In your browser, navigate to Github, hit the plus sign in the top right corner and create a new repository.
- 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.
- Push the button at the bottom Create repository to create a new repository.
- 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. - Your local repository and remote repository are now connected.
- 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! - 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.
- 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. - Track your files with
git add .
- Execute a
git status
again to double check that everything is being tracked. - Now commit your changes:
git commit -m "Another descriptive message to let you/other devs know what happened since the last change"
. - 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. - 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.