Git is a powerful version control system that helps developers collaborate, track changes, and manage their codebase effectively. In this quick cheat sheet, we'll cover the essential Git commands used in development, along with a bonus section on creating pull requests and draft pull requests on GitHub.
Basic Commands
-
Initialize a new Git repository:
git init
-
Check the status of your repository:
git status
-
Add files to the staging area:
git add <file> git add .
-
Commit changes with a message:
git commit -m "Your commit message"
-
View commit history:
git log git log --oneline
Branching and Merging
-
List all branches:
git branch
-
Create a new branch:
git branch <branch-name>
-
Switch to a branch:
git checkout <branch-name>
-
Create a new branch and switch to it:
git checkout -b <branch-name>
-
Merge a branch into the current branch:
git merge <branch-name>
-
Rebase the current branch onto another branch:
git rebase <branch-name>
Undoing Changes
-
Discard changes in the working directory:
git restore <file>
-
Unstage changes from the staging area:
git restore --staged <file>
-
Revert a commit by creating a new commit:
git revert <commit-hash>
Remote Repositories
-
Clone a remote repository:
git clone <repository-url>
-
Add a remote repository:
git remote add <remote-name> <repository-url>
-
Fetch changes from a remote repository:
git fetch <remote-name>
-
Pull changes from a remote repository:
git pull <remote-name> <branch-name>
-
Push changes to a remote repository:
git push <remote-name> <branch-name>
Bonus: Pull Requests on GitHub
Pull requests are a way to propose changes to a repository on GitHub. Here's how to create a pull request:
Fork the repository you want to contribute to.
Clone the forked repository to your local machine.
-
Create a new branch for your changes:
git checkout -b <branch-name>
Make your changes and commit them.
-
Push the branch to your forked repository:
git push origin <branch-name>
Go to the original repository on GitHub and click on the "Pull requests" tab.
Click on "New pull request" and select your branch as the compare branch.
Provide a title and description for your pull request.
Click on "Create pull request" to submit your changes for review.
Draft Pull Requests
Draft pull requests allow you to propose changes without requesting a review immediately. They are useful when you want to share your work in progress or get early feedback. To create a draft pull request, follow the same steps as above, but click on "Create draft pull request" instead of "Create pull request."
Base Branch
When creating a pull request, you need to specify the base branch, which is the branch you want to merge your changes into. Typically, the base branch is the main branch of the repository, such as master
or main
.
That's it! You now have a handy cheat sheet for the most commonly used Git commands and a guide on creating pull requests on GitHub. Happy coding!
Top comments (0)