Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

GIT-Professinal-Rewind

git commit -m "Always give a valid description while commiting the code"

Git merge:

Fast forward: while merging two branches git itself it will create a new commit that commit we called as the merge commit and after that all commits history in single line

go to the target branch where you want merge into lets say my scenario it is master

git checkout master

git merge feature-branch(it will merge the feature branch into the master)

**to stop the merge

git merge --abort

Git Rebase:

Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. 

or

In Git, the term rebase is referred to as the process of moving or combining a sequence of commits to a new base commit. Rebasing is very beneficial and visualized the process in the environment of a feature branching workflow.

go to the target branch where you want merge into lets say my scenario it is master

git checkout master

git rebase feature-branch

In this process git will remove all the commit from master then new merge commit will create in master by using that both branches looks same

It means it rewrites the commit history after the rebase the parent also will change

**but the problem in rebase if the code is already pushed and those we have some important commit history then will do the rebase means it will delete the all old commit history will rewrite new parent commit

***Do not use the Rebase on commits that you have already pushed/shared to remote repositary.

instead you can use for local commit history 


git rebase how far you want to go your commit back to go

git rebase -i HEAD~3 (i means interactive rebase)

when you use squash it will combine two commits it will give a new commit.

=============real time git scenarios=============
To initialize the git we can use git init
------------git diff

to check diff b/w branches

git diff main..feature

Git Diff Between staging area and last commit

git diff –staged

find diff b/w files 

git diff


Git Diff Between two commits

git log --pretty=oneline

gid diff sha1 sha2

ex:git diff 21d752987e7f507494439a599a02a105039b4125 60b1649d99710436fb56991b1120736d5e33c63e

Compare Using Head (Last two commits)

If you want to find the differences between the two most recent commits, you will use the following command:

Syntax: git diff HEAD HEAD~1

==============git log

to view the list of history we can use the

git log --oneline

to get the logs of branch

git log branch-name

======git remote
Check the configuration of the remote server:

$ git remote -v

Add a remote for the repository:

$ git remote add Fetch the data from the remote server:

$ git fetch

Remove a remote connection from the repository:

$ git remote rm

Rename remote server:

$ git remote rename

Show additional information about a particular remote:

$ git remote show

Change remote:

$ git remote set-url

Git origin master

Push data to the remote server:

$ git push origin master Pull data from remote server:

$ git pull origin maste


======mering=====

to merging the branches from one branch to other we can use either

git merge

git rebase

----git cheery-pick useful to merge particular commit into the branch

go to the target branch

git checkout master

git cherry-pick commit-id(from feature branch get commitid git log you will get) 

Now if you add git log master the commit will add from feature to master

**cherry pic will useful for 1 or 2 commits if its more or thousand of commits we need to use

git merge or git rebase

---------------diff b/w git merge & rebase--------------

While playing with merging if you modify in same files conflcts will while merging you need resolve those issue if you want to skip conflcts you can skip the files which already there in source branch & try to create a new files then it will merge into the source branch witout fails

create a branch git branch branch-name ot git checkout -b mergebranchname

git add .

git commit -m "test commit"

again go to master branch by git checkout  master

Now create a one more branch one rebase

git checkout -b rebasebranchname

git add .

git commit -m "test commit"

Now switch back to master
git checkout master
make some changes

git add .
git commit -m "test commit"

git merge mergebranchname

Note: if we modified in same file merge conflicts will occurs that time solve the merge conflict then again git add , and git commit -m "message of branch 

it will merge after that if you see logs of master branch you can see all commits from mergebranch as well,

git reabase:

while rebasing if you face merge conflicts to resolve auto

git rebase branchname
if conflict
git rebase --continue(auto way)
git rebase --skip

****** When we merged the commits will always sits in the top or head of git log

where as git rebase commits of branches will sit below the merging commits

Git merge will not follow the linear order of commits which already there in master commits always merge commits will show on top

Where as git rebase the commits in linear in git logs i.e line by line on the top orderwise.

"If commit history should be in linear way then we tarck the commits in order then rebase will use"

======================================================================

Git reflog 

the log is a public record of the repository's commit history, whereas the reflog is a private, workspace-specific record of the repo's local commits

----how we can undo a rebase

  • git reflog
  • git reset hashcode-- ( till above commits u want to undo)
----How we can change the commit message and combine 2 commits or squash
git rebase -i  HEAD~3 (last 3 commits)

at start mention s or squash to combine 2 commits 

after if you want change the commit messages if you want

at the last save the file :wq!

after git log
we can see we combined 3 commits into 1 commit.



git fetch & pull:

Pull the remote files from remote into your local directory were as git fetch will get the files from remote but it can't put in working directory or stagging are it will hold only on first phase 

after git fetch you need to run git merge as well

git pull = (git fetch + git merge)

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn’t make any changes to your local files.





Post a Comment

0 Comments

Ad Code

Responsive Advertisement