How to Set Up a Git Repository
Version control is an essential tool for developers to track changes to their code over time. With version control, you can review previous versions of your code, collaborate with others, and experiment with new features without worrying about losing working code.
One of the most popular version control systems today is Git. Git is a distributed version control system, meaning every developer has a full copy of the repository locally on their computer. This allows you to commit and manage code even when offline.
If you are new to Git and version control, setting up your first repository can seem daunting. Luckily, it is straightforward to initialize a new Git repo and start version controlling your code. In this post, we will walk through the basic steps for how to set up a Git repository.
Before you can set up and use Git, you need to make sure the following prerequisites are installed and configured on your computer:
- Git: This is the core software you need to use Git’s version control capabilities. Download and install the latest version of Git for your operating system.
- Text editor or IDE: You will write code in plain text files that Git can track. Any text editor or IDE will work, like Visual Studio Code, Sublime Text, Atom, etc.
- Command line access: To run Git commands and interact with your repos, you need access to a terminal/command line on your computer.
Once you have those three things setup, you are ready to initialize your first repository!
Step 1: Initialize a New Repository
The first step is to initialize a new Git repository. This can be done using the
git init command.
To initialize a new repo, open your terminal and navigate to the directory where you want the repository to live. This will usually be the root directory for your project.
This will create a new
.git subdirectory in that project directory. This
.git folder contains all the configuration files and objects Git needs to track and manage your repository.
You’ve now initialized a new Git repo that is ready to track changes!
Step 2: Create Your Project Directory
Now that you have a Git repository initialized, you need a place to store your project files.
Create a new directory inside your repository’s root that will contain all your code and assets. For example, if your repository is for a new website, you could create a
Step 3: Add Files to the Repository
At this point your repository is ready to start version controlling files.
Move into your new project directory and begin adding files. This can include starting a new file from scratch like
index.html, or moving existing files into the directory.
As you create and edit files, use the
git add command to begin tracking them in Git. This stages the changes to be committed.
git add index.html git add scripts/main.js git add assets/images/logo.png
You can use
git add . to add all new and updated files in the current directory to the staging area.
Step 4: Commit Changes
Once you’ve staged changes with
git add, the next step is to commit them.
Committing creates a snapshot of your repository with the files and changes you’ve added. This gives you a checkpoint to refer back to later.
To commit changes, use the
git commit command:
git commit -m "Initial website commit"
-m flag lets you include a short message describing the commit. Use descriptive messages to help find previous versions later.
Keep coding and committing as you build your project. Commits let you experiment, safe in the knowledge you can revert or view previous commits if needed.
Step 5: Connect to a Remote Repository (Optional)
At this point you have a functional local Git repository tracking your project. But what if you want to collaborate with others or backup your code?
This is where remote repositories come in. A remote repo is one hosted on an external server like GitHub or Bitbucket. You can push your code to remotes to share it and keep it backed up.
Here’s how to connect your local repo to a remote:
- Create a new empty repository on GitHub or a similar service. Follow their instructions to init the repo.
- Add the remote to your local project:
git remote add origin https://github.com/user/repo.git
- Push your code to the remote:
git push -u origin main
Now your local repository is connected to the remote. You can
pull changes between the two.
Working with remotes also allows you to collaborate via forking, pull requests, and more.
Recap of the Steps
Let’s quickly recap the steps covered to get a new Git repo setup:
- Initialize repository with
- Create project directory to store files
git addto stage new and changed files
- Commit changes with
- (Optional) Connect to a remote repository
And that’s it! By following these steps you can get started version controlling your code with Git.
Next Steps After Setting Up a Repository
Once your repository is initialized and connected, here are some recommended next steps:
- Clone the repo to create a local working copy on your computer
- Set up branching workflows to manage feature development
- Configure GitHub integrations like pull requests or actions
- Share access with other collaborators
- Create a README to document the project
- Set up a .gitignore to exclude files like logs or dependencies
- Review documentation to learn more Git commands and workflows
Resources for Learning Git
Here are some helpful resources if you want to dive deeper into Git:
- GitHub Git Cheat Sheet – Quick reference for common commands
- Git Documentation – The official Git docs
- Atlassian Git Tutorials – Comprehensive tutorials on Git
- Pro Git Book – Excellent book for learning Git in-depth
- Git Interactive Tutorial – Visual tutorial for testing Git commands
- Git Kraken – Graphical Git client with tutorials
Git has a significant learning curve, but is a must-have tool for developers. By starting with a local repository, you can begin to understand its concepts before tackling advanced workflows. The resources above provide plenty of guidance for taking your Git skills to the next level.
Common Questions about Git Repositories
Here are answers to some common questions about initializing and working with Git repositories:
How do I check the status of my repo?
git status to view the state of your working directory and staging area. This will show you which files are changed, staged for commit, etc.
How do I remove a file from being tracked?
git rm to remove a file from the staging area and your working directory. To only remove from Git tracking but keep the file locally, use
git rm --cached.
Can I have nested Git repos (a repo inside another repo)?
Yes, it is possible to nest Git repositories within one another. The inner repo will still have its own history and commits. However, Git does not actively track this relationship.
Can I remove sensitive data like passwords from a previous commit?
Yes, you can edit commits to remove sensitive data and amend the published history. Look into using
git filter-branch or removing the file from history entirely.
How do I undo local uncommitted changes?
To revert back to the state of your last commit, use
git reset --hard HEAD to remove any changes in your working directory and staging area. Be careful, as this cannot be undone!
Version controlling your projects using Git and source control is essential for any developer. Luckily Git makes it straightforward to initialize a new repository and start tracking your code.
By following the simple workflow of adding files, making commits, and optionally connecting to remotes, you can start benefiting from Git’s powerful feature set. There are many additional best practices and commands to learn, but initializing a basic local repository is an important first step.
Git enables essential features for managing and collaborating on code, like branching, merging, code reviews, backup, and more. Whether working solo or with a team, version control will improve your development workflow. Initialize a Git repository for your next project to see for yourself!
If you need help setting up a robust Git and deployment workflow for your business, check out the development and DevOps consulting services offered by devi8 consulting. Their team of experts can get your repository and infrastructure set up correctly from day one.