How to Hard Sync local Master with Original Upstream
Follow these steps in your main branch (upstream" is the original repository and "origin" is the fork one) :
-
git checkout main git pull upstream main git reset --hard upstream/main git push origin main --force
Why the previous commits of another branch are showing in the new branch
Let's say you have this repository with branch 'prev_branch'
---o---o---A prev_branch
Let's say you commit something A in prev_branch and created new branch 'new_branch'1, it won't differ until you start commiting to new branch
---o---o---A prev_branch + new_branch
Then you commit B in newbranch
---o---o---A prev_branch
\
B new_branch
The new commit B has A as its parent commit. Now prev_branch and new_branch differ in some way.
---o---o---A---X---Y prev_branch
\
B---Z new_branch
When you create a new branch from a particular branch, you'll start from the point where that branch currently is. So all commit history will be there in the new branch as well.
A good rule of thumb is to always create a new branch from the branch that you intend to eventually merge the new branch into (main branch). So if D is intended to be merged into main at some future point in time, create it from the current tip of main.
For more details refer blog / blog
Follow this while creating new branch and commiting changes:
- git checkout main/master
- git branch -b new_branch
- git add .
- git commit -m "message"
Edit a commit
-
Convenient way to modify the most recent commit
# Edit src.js and main.js git add src.js git commit # Realize you forgot to add the changes from main.js git add main.js git commit --amend --no-edit
-
Using git reset
git add . git commit -m "This commit is a mistake" git reset HEAD~ git add main.js git commit -m "This commit corrects the mistake"
-
To undo the last two commits, use the commands:
git add git commit -m "This commit is a mistake" # make changes to files git add . git commit -m "This commit is another mistake" # want to go back to 2nd last commit to make changes git reset HEAD~2 # make changes git add . git commit -m "this commit corrects both mistakes"
for more info refer this blog
-
Change the last commit message
# it will change the last commit's message git commit --amend -m "New commit message"
for more info, watch this video
To Check the commit Tree:
#Run below to visualize the commit tree.
gitk
git log show commit logs.
Rebase
#Fetch the latest changes
git fetch main
#Rebase onto the upstream branch
git rebase upstream/main`
Good Practice
-
Avoid committing unnecessary files:
#Avoid using which commits everything. git commit -a
-
Use targeted commit commands:
git add specific file