Git 101: Starting a New Project with the Command Line
Git is a widely used version control system that allows developers to work together on a project efficiently. It is an essential tool for any developer, and the command line is one of the most powerful ways to use Git. In this blog post, we will discuss how to start a new Git project using the command line. We will cover creating a Git repository, adding files to the repository, making commits, and some tips for using Git on the command line.
CREATING A GIT REPOSITORY
The first step to starting a new Git project is to create a Git repository. To do this, navigate to the directory where you want to create the repository and enter the following command:
$ git init
This command initializes a new Git repository in the current directory. Git will create a hidden .git directory that contains all the information required to track changes to your project. You can also create a new Git repository on a remote server, such as GitHub or GitLab, and then clone the repository to your local machine.
ADDING FILES TO THE REPOSITORY
After creating the repository, the next step is to add files to the repository. To add a file to the repository, use the following command:
$ git add file_name
This command stages the file for inclusion in the next commit. If you want to add all the files in the current directory, you can use the following command instead:
$ git add .
It is a good practice to add only the files that are required for your project and exclude any unnecessary files, such as build artifacts or temporary files. You can use a .gitignore file to specify which files or directories to exclude from the repository.
Once you have added files to the repository, you need to make a commit to record the changes. A commit is a snapshot of the changes made to the repository. To make a commit, use the following command:
$ git commit -m "Commit message"
This command creates a new commit with a message describing the changes made in the commit. It is important to write a clear and concise message that describes the changes made in the commit. A good commit message should include a summary of the changes, the reason for the changes, and any relevant information about the changes.
LINK THE REPOSITORY TO GITHUB
To link a git repository with a GitHub repository using the git command line, you will first need to create a new repository on GitHub and retrieve the repository URL. (Ex: https://github.com/semfionetworks/project.git)
Type the following command to add the remote repository URL to your local repository, replacing
<remote repository URL> with the URL of your GitHub repository:
$ git remote add origin remote_repository_URL
To show a list of all the remote repositories associated with your local repository and verify the remote repository URL by typing the following command:
$ git remote -v
Push your local repository to the remote repository by typing the following command:
$ git checkout -b master $ git push -u origin master
This will push your local repository to the remote repository on GitHub. The “-u” flag tells Git to remember the remote repository and branch, so you can use the simpler “git push” command in the future.
Your local repository is now linked to your GitHub repository. You can push changes to the remote repository using the “git push” command, and pull changes from the remote repository using the “git pull” command.
TIPS FOR USING GIT ON THE COMMAND LINE
Using Git on the command line can be intimidating for beginners, but it is a powerful tool that can improve your productivity and workflow. Here are some tips for using Git on the command line:
- Use tab completion to save time typing commands and file names.
- Use aliases to create shortcuts for commonly used commands.
git statusto check the status of your repository and see which files have been modified or staged.
git logto view the commit history of your repository and see who made changes and when.
git showto view the details of a specific commit.
Starting a new Git project using the command line is a straightforward process. By following the steps outlined in this blog post, you can create a new Git repository, add files to the repository, and make commits to track changes to your project. Git is an essential tool for any developer, and mastering the command line interface is a great way to become more efficient and productive. With practice and experience, you can become a Git power user and take advantage of the many features and benefits that Git has to offer.