Git is a powerful version control system that allows developers to track changes to their code and collaborate with team members. It is an essential tool for any developer, and learning how to use it can greatly improve your workflow.
Before you start using Git, you need to set it up on your computer. This involves installing the Git software and configuring your username and email address. These details will be associated with your commits, so it’s important to use your real name and a valid email address.
Once you have Git set up, you can create a new repository for your project. A repository, or “repo” for short, is a collection of files and directories that are tracked by Git. To create a new repository, navigate to the directory where you want to store your code and run the following command:
git init
This will create a new Git repository in the current directory. You can then add files to the repository by using the git add command. For example, to add a file called main.c, you would run the following command:
git add main.c
To add all the files in the current directory, you can use the git add . command.
Once you have added your files, you can commit your changes to the repository. A commit is a snapshot of your code at a particular point in time. To commit your changes, use the git commit command, followed by a message describing the changes you have made. For example:
git commit -m “Added main.c file”
It’s important to write clear and concise commit messages, as they will help you and your team members understand the changes that have been made to the code.
Git also allows you to create branches, which are separate copies of your code that you can work on without affecting the main branch. This is useful when you want to try out a new feature or fix a bug without disrupting the main codebase. To create a new branch, use the git branch command, followed by the name of the branch. For example:
git branch new-feature
To switch to a different branch, use the git checkout command, followed by the name of the branch. For example:
git checkout new-feature
When you are ready to merge your changes back into the main branch, you can use the git merge command. For example:
git merge new-feature
These are just a few of the basic commands that you can use with Git. There are many more options and features available, and learning how to use them will greatly improve your workflow as a developer.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Interested in scaling your software startup? Check out Circuit.
Git For Beginners was originally published in JavaScript in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.