Skip to content

Instantly share code, notes, and snippets.

@gajjardarshithasmukhbhai
Last active March 24, 2023 19:48
Show Gist options
  • Save gajjardarshithasmukhbhai/e84e114e0a6a26bbc478e35c58229750 to your computer and use it in GitHub Desktop.
Save gajjardarshithasmukhbhai/e84e114e0a6a26bbc478e35c58229750 to your computer and use it in GitHub Desktop.
Advanced Git concepts are super important when you worked in Extensively worked on the Dev-Ops Side Because withOut You will struggling

Advanced Git and Github Series & CI/CD

Git and GitHub Intro
Git Imporant command
  • git checkout -b : It create the new branch and checkout on that branch
  • git switch -c : It create the new branch and checkout on that branch. It work same like checkout. but above commad confused begginer so git introduce this one
  • git ls-files : It Display all stageing files not Un-stages Files.
  • git clean -d : It will send warning and out to unstaged the file that you have been added (Note: not Removed the Unstaged File)
  • git clean -df : After above command you have used this command so this command force to remove the file.
  • git reset intial-commit.txt : This is used to unstaged the file and after you had to do checkout(git checkout <File_Name>). It will remove the File!

upe4t9.png

Some Of Are Important Command that are used in Daily Life

uvMjWJ.png

  • If We want to one step back so we will use this command: cd D:\USERS\MANUAL_GIT
  • we want to change the drive so used D:>C:
  • we want to go on root dir >cd /
commitId (HEAD -> main, virtusa)

uv7cBq.png

  • Head is basically Refer the Latest commit of the branch
  • we have multiple branch so that time through head we will identify which is Latest commit Over there.
  • Otherwise it so much difficult to identify that
The Detached Head

uv7BHx.png

  • The Detached Head is not branch, but It will refer the particular branch that is not associated with any branch but only associated through commit-Id.
The Head Vs Detached Head
  • Head refer the latest commit of particular branch.
  • when detached head refer only based on the particular commitId.
  • ex. If we want to checkout on particular branch through commitId. so that time Detached Head is display as per above code.
  • If we want to back the root project so that time we use git checkout command
Soft Reset not Remove the file but it work in your workspace

Soft Reset Video

After Merging with Master branch we Forgot to Add some functionality to add that Branch
  • First of We go git log
  • Then After go git checkout <commit_id>
  • Then After Modify Your Changes and Everything do whatever you are missed in past then stage and commit
  • but still It Display detached when you hit this command git branch
  • so solution is git branch <New_Branch_Name> <Detached_ID>
  • Then After Merge into the master branch

uvARCX.png

Git Stash Command

commands:

  • git stash list -> It display all stashes

  • git stash push -m "add message" -> this is best practice that followed to creating the messagble stash

  • git stash apply -> use latest stash displaying

  • git stash pop <stash-id> -> If you have multiple stash specific stash you want to use so this command is used for that and this pop(display) in code and removed in stash list

  • git stash drop <dropId> -> this command is useful if you want to remove particular stash in the list.

  • git stash clear -> this command is used for clearning the stash

  • Git stash is used when you not want to loose your unstaged changes while you want to merge with main branch after checkout to main.

❓Scenario ❓
  • you are working on the feature branch suddenly you're manager will come Darshit merge main branch. our USA developer Team doing lost more changes. so what you will do ? you working on your local branch 1 week you not get single pull from main branch.
🙋Solution 🙋
  • you working on your local branch. so you use this command **git stash push -m "feature.file changed" **
  • so you see your all local code will out. Don't panic it is there so simply use this command git stash list
    • so you see 'stash@{0}: On stash-learn: dummy.txt file changed'
    • so If you want to see your code simply use this command git stash apply. this command will display latest stash you have been done.
  • go to your main branch. In main branch use this command to get latest code git pull
  • so your main branch updated [Great Job]
  • go to your Feature branch and simply use this command for merge with main git merge main
  • then you want to back your latest code so, use this command git stash apply
  • so you will see merge conflict. solve it you're code and your team code wil display
where to use Checkout and Where to use reset commands
  • Use reset if you want to undo staging of a modified file.
  • Use checkout if you want to discard changes to unstaged file/s and attached the Detached
Git Reflog Command
  • Git Reflog command is useful when suddenly you lost the commited code and Delete the branch. so that time it will so much useful. git Reflog Display All Info about Specific branch.
❓Scenario ❓
  • your Team member working on the one branch like ex.Feature and Suddenly Darshit's TeamMember. Delete the branch and lost their code what will do in this scenario ?
🙋Solution 🙋

-> 'कभी नहीं डरना', Simply go to CMD use below commnads for that.

  • git reflog command use. so it will display everything whatever you are play with cmd
  • then after go to your commit and find the message where display commit and use that commit-ID git checkout
  • you will get the [deatached commitId] and that display when you use this command git branch
  • simply make another branch like git checkout -b <branch_name> and that detached Id Based branch removed and you will see the fresh New branch. so your code still perfectly worked.
How to Handle multiple commit to one

-> option:1 - we will use the git merge with the squash flag

  • so basically when you're merging to anothe branch so it's all commit will come as default so it will create the oevrhead for developers.
  • so beautiful solution is to attach the git merge --squash <branch_name>. soit will work like charm
  • Before use squash flag: Flh7tp.png
  • After use squash flag: FlhDzP.png

-> option:2 - we will use the git rebase command

Flh0or.png

  • git not recommended to developer because it create the problem what ? Git Rebase commnad changed the commit-ID when the you will doing the Rebase so it is dangerous Issue When you share git history to others
  • when to use the git rebase I make graphical representation for that FlhKZh.png
Differebce Between Git Rebase vs Git merge vs Git Cherry Pick

FlhB1P.png

FliUpK.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment