Reverting & Reset

git checkout commit_ID = to just check the commit id entered , see it in read only ... changes will not be saved

git checkout master = to come back to original commit (As checkout removes us from master branch)

Revert commit :

git revert commit_ID = to remove the changes of the provided commit (will add a new revert commit and remove the changes of the specific commit)

Reset Commit :

git reset commit_ID = will remove all the commits after the provided id , but the files in local directory will not be touched (therefore you can still commit to original state after doing changes as needed) ... might take you to vim editor (type ":wq" then "Enter" to exit)

git reset commit_ID --hard = will remove all the commits after the provided id and even delete all the files and lines from local directory too

Branches

Used to test a new feature or code , by creating a branch .. then merging it to master only if needed

can be used for multiple developers working on same project .. create different branch for each developer adding their own feature then merging at the end

git branch branch_name = to create a new branch

git branch -a = to list all the branches

git checkout branch_name = to shift to the other branch

git branch -d branch_name = to delete the branch only when it has been merged

git branch -D branch_name = to delete the branch (even if not merged to master)

git checkout -b branch_name = to create and shift to a new branch at once

Merging branches

after completing changes in a branch and commiting them

come back to master and run

git merge branch_name = this will merge the branch to master (all commits show in master) = automatic

git merge --squash branch_name = this will merge the branch to master (only the commit after merge is shown in master) = manual

Conflicts

If Branch's Base (First Commit) is Master's Head (Last Commit) = No Conflict

If Master had commits after creating Branch = Conflicts Might Come

to solve this , edit the files manually , Solve The Conflicts then ..

run git add . and then git commit -m "Message" and the changes will be made

Git Rebase & Git Merge

Using Git Merge Shows that the Branches Were Added to master , i.e the tree is not inline for all commits

whereas Git Rebase keeps changing the base, and makes the commit inline , feels like the branch was never there

RUN git rebase master on your branch

Takes the base of master , matches it with every commit of your branch

If The Master is already your base , no need of step 3, 4, 5

solve the conflicts , then git add .

run git rebase --continue

Repeat 2, 3 steps for every commit - conflict

Now The Master's Head is Branch's Base

Move to Master

run git rebase branch_name

Now All the commits of Branch are added above your Master commits

NOTE! : It is specified in the git docs that rebase should not be used in public repos (collaboration) as it can cause major errors and conflicts, it can be used in private repos.

You can try out git stash on some sample files and you will get the idea of what it is.

git rebase --continue works when you started rebasing (git rebase master) a branch with other branch (or master) that was ahead of it.
but yeah, if the branch that you are merging isnt ahead, git rebase might work in one shot.

git merge merges the branches (their commits) into one marking that it was merged from a branch.
Whereas git rebase takes the head of other branch, puts it in base of the branch that you are in, like the other branch was never there.