Git Commands for Open-Source Contribution
Quick reference of git commands during contribution
The step-by-step guide to contributing to open-source projects:
Fork Repository: Top-right corner second button is a Fork.
Clone Repository: Copy the URL of the repo to clone in your local directory. Open Git Bash, select the directory where you want to clone your repo, right-click and open Git Bash.
git clone paste-your-URL
Create a new branch:
Change to the repository directory using:
cd your-repo-name
Create a branch using
git switch
orgit checkout
command:git switch -c your-new-branch-name
or
git checkout -b your-new-branch-name
Make your changes (here you can find issues or improve code or add your name to the contribution list).
Add those changes to the branch using the
git add
command.To add all changed files use:
git add .
Add specific changed file use:
git add changed-file-name
Commit those changes using
git commit
command:git commit -m "Add your message here"
Push your changes to GitHub using
git push
command:git push -u origin your-branch-name
Go to your repository on your GitHub, you'll click
Compare & pull request
button to add your PR.Finally, add your PR title and comment, then you'll click
Create pull request
button to complete your first pull request (PR).
Congratulations ๐ you have completed your first contribution.
Note: If I get code from the main branch to my own branch, I will run the following commands in my branch terminal:
Switch to my own branch:
git checkout own-branch-name
orgit switch own-branch-name
Fetch the latest updates from the origin:
git fetch origin
Merge with the main branch:
git merge origin/main
You can revert individual commits using git revert <commit_hash>
, which creates a new commit that undoes the specified commit's changes. To revert a range of commits, use git revert <oldest_commit_hash>..<latest_commit_hash>
.
To discard changes in your working directory and revert to the last committed state, you can use the following Git commands depending on your situation:
If you have uncommitted changes (files not staged for commit):
Use the
git checkout
command to discard changes in specific files:git checkout -- <file>
Or, to discard changes in all files:
git checkout -- .
If you have staged changes (files added to the staging area):
First, unstage the files:
git reset
Then discard the changes:
git checkout -- .
If you want to discard both staged and unstaged changes and revert to the last commit:
You can use the
git reset
command with the--hard
option:bashCopy codegit reset --hard
Be careful with this command, as it will permanently discard all your local changes and cannot be undone.