You are on page 1of 10
Git Commands Explained Let's get started git pull is git fetch + git merge -> Part 1 Assume the local branch is in-sync with the remote branch at the moment. Both ‘master’ (local copy) and ‘origin/master’ (pointer to remote master) ave pointing to the same commit ‘s4ad... Now, suppose one of your teammate has added/pushed a new file on master remote branch. You local master is not aware of that change. Let's do ‘git fetch’ and see what happens. git fetch [CeCe Oh! t can only see ‘master’ pointing to 'séad..", but where is ‘origin/master' ? in/master got updated to what's in the remote When you did ‘git fetch’ the ovi master. As your teammate added a new change, it must be pointing to it. git pull is git fetch + git merge -> Part 2 ~ Let's find out where is ‘origin/master' _git log origin/master er crest] IS git log origin/master Ce ee eee eee SUE Laser CRs eee) rcs Pe ee eee eer St ec LTe reese) eae eas See, the ovigin/master points to the latest commit which is on remote master, but your local master still points te ‘sad. old covamit. How can we syne them again? git merge we need to doa merge no a forerio) ec Veta CLEC MET eet) aera ger Pieces ad CReL ute Ea aT TGD) eC Awesome, both origin/master and master are now pointing to the same cormit. ee) ced ee eee ese CeCe Be Ser tertile dec ET) es pe eee ae ee STE Beene Ee eae) resend git pull is git fetch + git merge -> Part 3 We can perform the complete ‘git fetch' + ‘get merge’ thing by just doing ‘git pull’. Assume, that your teammate again added a new text file on remote master and you local master is unaware of the change. Current state - ‘git log! arr (master) ea) ce PO eee eee een ener a me pris Thu Apr 28 21 5 2022 +0530 ccs eee eee een (re Cees S Eee cae) aera = Let's do ‘git pull' and see what happens : git pull When we did ‘git pull', et us Tea (master) an aie a ie it first performed Sa ee eee ee ‘git . ed eons ate CL eRe agit fetch’ followed by Cet aa RCD CL ‘git merge’ Total 3 (delta 0), reused 0 (delta 0), p Tiegh el Sian ee ee enn ae urea reed Cert ee ee Tenet ae as p pointir eo raaster’ pointing to Panel samme new commit. ‘origin/master’ & Cee eee Thu Apr 28 21:42:50 2022 +0530 CS ea Creating and Publishing a Local branch to Remote ~ create a local branch ‘feature/demot' and switch te newly ereated branch. git branch featuve/demo1 git checkout -b feature/demor OR git checkout feature/demot ee er Cor koe ais c ona ean GESEL fear) rte branch ‘fea ae (creas) t leg cated er eee een Cee es Dee ee Een eee Third commit cue See et Steeler aes s Sec LE) Second commit ee reseed pr ae eee Perse rZ ae SEs) aeons - viewing last n commits (in this case 2 commits) git log -n2 ian (master) $ git log -n2 cies Oe reece ae (re ee Cea err BE MeL aeSC Es) SR eee ee ee eos Thu Apr 28 20:42:42 2022 +0530 oer mets — one liner info of every commit git log --oneline ey core) er ae ved Third commit Second commit eas (creas) rey cated er eee een Cee es tee ee Third commit See et Ree CECE ar ese s Sec E Second commit eed Sec tar, prea ee ere Pere cae) aeons - skipping last n commits (in this case 2 cormmits) git log --skip=2 et u fcretaD] IS git log --ski Sees, eee a (te ee ee Be reese ec) oats — grep commits by string/pattern git log --grep="Second" ae (creas ea ee ee ese Thu Apr 28 20:42:42 2022 +0530 cures git cherry-pick is cool As the name sounds, it is used to pick changes from existing commit and apply them by creating a new commit. Let's see a simple example. We are on ‘feature/demo' branch currently and we have added a new change with commit id '32.904..'- Created hello file Se at We can see the ‘master’ is behind by 1 commit from the feature. We know one way to updated ‘master’ is by doing merge with the feature branch. But here, we will see how cherry-pick works. We can pick a commit and apply. So let's pick the commit '32.904..' and apply it to master. Let's switch to master and do cherry pick. After cherry -pick the same changes from that commit crc 1 file changed, 1 insertion(+) are applied to re ae ee master branch. s git To = / ? ee But it created a new commit with a different oe Can shat. Let's see the difference - difference between 2 commits git diff commit commit2 al... from commits ‘s4ad...' & b/... is from commit2 ‘a7eLO..". You can see 2 files (hello.txt and pull.txt) were added in commit2. ~ similarly you can see diff of tip between two branches git diff bvanchA branchB - to see difference between your local (unstaged) vs staged(indexed) changes git diff to see difference between staged(indexed) changes vs what's commited your local wepo git diff --staged oR git diff --cached ens eee) In a nutshell Stash Working Staging Local Remote as| Tree, Area, Repo Repo git stash git diff t git diff staged + f git diff --cached! git pop : o> > git apply : git ade + git commit git push git rm : git fetch + git merge git rebase git pull " & mrayank_ahuja

You might also like