Git Commands for Open-Source Contribution
Quick reference of git commands during contribution

👋 Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!
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-URLCreate a new branch:
Change to the repository directory using:
cd your-repo-nameCreate a branch using
git switchorgit checkoutcommand:git switch -c your-new-branch-nameor
git checkout -b your-new-branch-nameMake 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 addcommand.To add all changed files use:
git add .Add specific changed file use:
git add changed-file-nameCommit those changes using
git commitcommand:git commit -m "Add your message here"Push your changes to GitHub using
git pushcommand:git push -u origin your-branch-nameGo to your repository on your GitHub, you'll click
Compare & pull requestbutton to add your PR.Finally, add your PR title and comment, then you'll click
Create pull requestbutton 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-nameorgit switch own-branch-nameFetch the latest updates from the origin:
git fetch originMerge 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 checkoutcommand 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 resetThen 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 resetcommand with the--hardoption:bashCopy codegit reset --hardBe careful with this command, as it will permanently discard all your local changes and cannot be undone.




