Git & GitHub Cheatsheet
Git is a popular version control system that allows developers to track changes to their codebase and collaborate with others. GitHub is a web-based platform that provides hosting for Git repositories, as well as tools for collaboration, code review, and project management. This cheat sheet covers the most commonly used Git and GitHub commands and their usage.
Getting Started
To get started with Git and GitHub, you need to first create a GitHub account and set up Git on your local machine. You can do this by following the instructions on the GitHub Website
Once you have set up Git and created a GitHub account, you can start using Git to manage your project files. To initialize a Git repository, navigate to the folder where you want to store your project files and run the following command:
$ git init
This will create a new Git repository in the current directory.
Staging and Committing Changes
Once you have initialized your Git repository, you can start making changes to your project files. To stage these changes, you can use the git add
command. This command takes the path to the file you want to stage as an argument. For example, to stage a file called script.js
, you can run the following command:
$ git add script.js
To stage all changes in the current directory, you can use the .
wildcard character as the argument to git add
, like this:
git add .
Once you have staged your changes, you can commit them to the repository using the git commit
command. This command takes a commit message as an argument, which is a short message that describes the changes you are committing. For example, to commit your changes with the message "Add script.js file"
, you can run the following command:
Pushing Changes to GitHub
Once you have committed your changes to your local Git repository, you can push them to your GitHub repository using the git push
command. This command takes the name of the remote repository and the branch you want to push to as arguments. For example, to push your changes to the master
branch of a remote repository called origin
, you can run the following command:
$ git push origin master
You can also set a remote repository as the default destination for your push and pull operations, using the git remote
command. For example, to set the origin
repository as the default remote, you can run the following command:
$ git remote add origin https://github.com/username/repository.git
After setting the default remote, you can simply use the git push
command without specifying the remote and branch, like this:
$ git push
Pulling Changes from GitHub
If other people are collaborating on your project, they may make changes to the code in the GitHub repository that you don't have in your local repository. To get these changes, you can use the git pull
command. This command will fetch the latest changes from the remote repository and merge them into your local repository. For example, to pull changes from the master
branch of the origin
repository, you can run the following command:
$ git pull origin master
Collaborating on GitHub
GitHub provides a number of tools for collaborating on projects with others. For example, you can use the git clone
command to create a local copy of a remote repository, which you can then work on and push changes back to the remote repository.
$ git clone https://github.com/username/repository.git
You can also use the GitHub website to create pull requests, which are requests for someone else to review and merge your changes into their branch. To create a pull request on GitHub, follow these steps:
Push your changes to your GitHub repository.
On the GitHub website, navigate to the repository where you want to create the pull request.
Click the "Pull requests" tab, then click the "New pull request" button.
On the next page, select the branch you want to merge your changes into and the branch you want to merge from.
Click the "Create pull request" button.
On the next page, enter a title and description for your pull request, then click the "Create pull request" button.
The owner of the repository will receive a notification about your pull request and can then review your changes and decide whether to merge them into their branch.
Conclusion
This cheat sheet covers the most commonly used Git and GitHub commands and their usage. By mastering these commands, you can use Git and GitHub to efficiently manage and collaborate on your projects.