- git init → Initializes a new Git repository in the current directory.
- git add . → Stages all changes (new, modified, or deleted files) for the next commit.
- git branch -m main → Renames the current branch to “main.”
- git remote add origin [https://github.com/user/user-repo.git] → Links the local repository to a remote repository at the specified URL.
- git remote -v → Displays the remote repositories linked to the local repo with their fetch and push URLs.
- git push -u origin main → Pushes the main branch to the remote “origin” and sets it as the default upstream branch for future pushes.
git branching
- git branch [name] → Creates a new branch with the specified name.
- git branch → Lists all branches in the repository, highlighting the current branch.
- git checkout [branch_name] → Switches to the specified branch.
- git branch -d [branch_name] → Deletes the specified branch (if fully merged).
- git checkout -b [branch_name] → Creates a new branch with the specified name and switches to it.
Steps for Open Source Collaboration
1. Fork the Repository
- Go to the GitHub repository.
- Fork the repository.
- Create your forked repository.
- Copy the main branch only.
- Hit the Create Fork button.
- Done~
2. Clone the Forked Repo
- Go to the repository you forked.
- Copy the git_link.
- Clone the repository to your local folder using
git clone <git_link>
.
3. Create a New Branch
git branch
→ See all branches in your current repo.
git branch [branch_name]
→ Create a new branch in your repo.
git checkout [branch_name]
→ Switch to another branch.
git checkout -b [branch_name]
→ Create a new branch and switch to it.
git branch -d [branch_name]
→ Delete a branch.
4. Make a Pull Request
- Make changes in your branch.
- Stage and commit changes:
git add .
git commit -m "Your changes description"
- Push your branch to your forked repo:
git push origin [branch_name]
- Go to the original repository on GitHub.
- Click Compare & Pull Request.
- Write a brief description of your changes and submit the pull request.
- Done! 🎉
Random Commands
- git log → Displays the commit history with details like author, date, and commit message.
- git log —oneline → Shows the commit history in a condensed, single-line format per commit.
- git reset [commit_id] —hard → Resets the repository to the specified commit, discarding all changes after it.
- git push -f → Force-pushes changes to the remote repository, overwriting the existing history.
- git revert [commit_id] → Creates a new commit that undoes the changes introduced by the specified commit.