git statusgit logWhenever you are working on feature or bug, always work on another branch & not on the main branch.
git branch <new-branch-name>git checkout <branch-name>Head is where the current pointer is, all the commits happen at Head pointer, so inorder to commit to other branches, you must switch branches.
Step 1: Switch to your local main
git checkout mainStep 2: Pull the latest changes from the original repo (upstream)
git pull upstream mainStep 3: (Optional) Push changes to your fork
git push origin mainStep 4: Now create the new branch from the updated main
git checkout -b new-featureNote:
- Always have separate branch for each feature/bug you're working on.
- If a PR is opened on one branch, then all the commits on that same branch will be grouped under the same PR, which means 1 branch = 1 pull request only.
- While creating a new branch always make sure it is up to date with the main branch of original repo
git add .git add <file_name>git restore --staged .git restore --staged <file_name>git commit -m "<description_of_commit>"git reset <hash_value_of_commit_whose_state_is_to_be_restored>Say you have 3 commits, and you want to remove the last 2 commit, for that you need to mention the hash value of the 1st commit, and the codebase will restore to 1st commit's state.
git add .
git stash git stash popThis will bring the saved state back to the current state
git stash clear git rebase -i <hash-of-commit-from-which-commits-are-to-be-merged>now for every commit you want to merge, say a interactive dashboard is shown with these commits:
pick 1
pick 2
pick 3
pick 4 now if you want to merge 2 & 3 into 1 then do this:
pick 1
s 2
s 3
pick 4Basically wherever you mention s, the previous commit is taken as the commit & the commit with "s" get merged into the previous one. exit with "x"
Then you will be prompted to write the commit message. write. exit with "x".
Then, git push origin temp
git remote add origin <github-url-with-git-suffix>where, remote = declares working with URL add = adding new URL origin = name of URL to be added (all repo that are in personal account are by default named as origin )
git remote -vgit push origin mainwhere, origin = url to which commits are to pushed main = branch to push at
- Go to the repo, fork it.
- On local machine, clone your own forked repo.
- Create a new branch for each feature/bug you're working on.
- Change the head to the branch you're working on.
- Make changes, commit the changes.
- Open a PR from Github for repo owner/maintainers to review.
- If any changes recommended, make the changes in the same branch, commit & push the changes.
- If your contribution is good enough, without any conflicts, if would be merged.
git clone <github-repo-link-with-git-suffix>You can add the original URL in git for the forked repo. The original URL is the URL of the organisation's repository from where you have forked the repo.
git remote add upstream <url>-
Change the branch to main
-
git pull upstream main (gets all the changes, commits locally)
-
git push origin main (push to github)
OR -
- Change the branch to main
-
fetch all commits on all branch, prune enables the ones that are already deleted git fetch --all --prune
-
Reset main branch of origin with the main branch of upstream: git reset --hard upstream/main
-
git push origin main (push to github)
-
git log, you can see the updated commits
You "raghav" made a change in line 3, someone else "rohan" also changed line 3.
now rohan's PR is merged.
maintainer will see a merge conflict, since both of you tried to change the same line of code. it needs to be manually specified that whose changes are to be over-written & then the pull request is merged.