Back to: Git Course
Git is a version control system that allows you to manage your code and track changes. GitHub is a web-based hosting service for Git repositories. It provides a graphical interface for managing your code and collaborating with others. In this tutorial, we will cover the basics of Git and GitHub and how to use them together.
- Install Git to use Git, you need to first install it on your computer. You can download the Git installer for your operating system from the official Git website. Once installed and configured, open a command prompt or terminal window and run the following command to check if Git is installed correctly:
- Create a new repository to create a new repository on GitHub, log in to your account and click the “New repository” button. Give your repository a name and description, and choose whether to make it public or private.
- Clone the repository to work on the repository on your local machine, you need to clone it. Go to the repository on GitHub and click the “Code” button. Copy the URL of the repository and run the following command in your terminal:
git clone <repository_url>
This will create a local copy of the repository on your machine.
- Make changes to the code: Now that you have a local copy of the repository, you can make changes to the code. Open the files in your favorite code editor and make any necessary changes.
- Add and commit changes: Once you have made changes to the code, you need to stage and commit the changes. To stage changes, run the following command:
git add .
This will stage all the changes you have made. To commit the changes, run the following command:
git commit -m "commit message"
Replace “commit message” with a short description of the changes you made.
- Push changes to GitHub To push your changes to GitHub, run the following command:
This will upload your changes to the remote repository on GitHub.
Pull changes from GitHub: If someone else has made changes to the code on GitHub, you need to pull the changes to your local machine. To do this, run the following command:
This will download the changes from GitHub and merge them with your local copy of the code.
Create and switch branches Git allows you to work on separate branches of the code. To create a new branch, run the following command:
git branch <branch_name>
Replace “branch_name” with a name for your new branch. To switch to the new branch, run the following command:
git checkout <branch_name>
Now you can make changes to the code on the new branch without affecting the main branch.
Merge branches: Once you have made changes to a separate branch, you can merge them with the main branch. To do this, switch to the main branch and run the following command:
git merge <branch_name>
This will merge the changes from the separate branch with the main branch.
Create a pull request:
If you want to contribute code to someone else’s repository, you can create a pull request. To do this, fork the repository on GitHub and create a new branch for your changes. Make your changes and commit them to the new branch, then create a pull request on GitHub. The repository owner can review your changes and merge them with the main branch if they approve.
There is much more to learn, but this should give you a good starting point. Happy coding!