Version Control Simplified with Git & GitHub
Sharpen Skills, Ace Interviews
Git/Github : Frontend Development Interview Questions
Git is a distributed version control system that allows multiple developers to work on a project simultaneously. It tracks changes to files and coordinates work between multiple people.
Use the git init command to create a new Git repository.
Code in bash:
git init
Use git clone to copy a repository from a remote server to your local machine.
Code in bash:
git clone https://github.com/username/repository_name.git
Use git status to display the state of the working directory and staging area.
code in bash:
git status
Use git add to stage files for the next commit.
Code in bash:
git add file_name
git add .
del and the view:
- Interpolation: {{ expression }} - One-way binding from the component to the view.
- Property Binding: [property]="expression" - One-way binding from the component to the element's property.
- Event Binding: (event)="handler" - One-way binding from the element's event to the component's method.
- Two-way Binding: [(ngModel)]="property" - Two-way binding that combines property and event binding to keep the view and model in sync.
Example:
html:
<input [(ngModel)]="name"> <!-- Two-way binding -->
<p>Hello, {{ name }}!</p> <!-- Interpolation -->
Use git commit to save changes to the repository with a descriptive message.
Code in bash:
git commit -m "Commit message"
Use git branch to create a new branch, and git checkout to switch to it.
Code in bash:
git branch branch_name
git checkout branch_name
# or
git checkout -b branch_name
Use git merge to combine changes from one branch into another.
Code in bash:
git checkout main
git merge branch_name
Use git push to upload your local commits to a remote repository.
Code in bash:
git push origin branch_name
Use git pull to fetch and merge changes from a remote repository to your local branch.
Code in bash:
git pull origin branch_name
Some common lifecycle hooks:
- ngOnInit: Called once the component is initialized.
- ngOnChanges: Called when input properties change.
- ngOnDestroy: Called just before the component is destroyed.
Example:
typescript:
import { Component, OnInit, OnDestroy } from '@angular/core';
@Component({
selector: 'app-lifecycle',
template: '<p>Lifecycle Demo</p>',
})
export class LifecycleComponent implements OnInit, OnDestroy {
ngOnInit() {
console.log('Component Initialized');
}
ngOnDestroy() {
console.log('Component Destroyed');
}
}
Use git log to display a list of commits made in the repository.
Code:
git log
Use git revert to create a new commit that undoes the changes made by a specific commit.
Code in bash:
git revert commit_id
Use git reset to move the HEAD pointer to a previous commit and optionally discard changes.
Code in bash:
git reset commit_id # --soft or --hard can be used depending on whether you want to keep the changes
Use git tag to mark a specific commit with a label, often used for versioning.
Code in bash:
git tag v1.0.0 commit_id
git push origin v1.0.0
Use git stash to temporarily save changes that are not yet ready to be committed.
Code in bash:
git stash
git stash pop # To apply the stashed changes
Use git cherry-pick to apply the changes from a specific commit to the current branch.
Code in bash:
git cherry-pick commit_id
Use git diff to compare changes between two commits or branches.
Code in bash:
git diff commit_id1 commit_id2
Git marks the conflicting areas in the affected files. You need to manually resolve the conflicts by editing the files and then committing the changes.
Code in bash:
git merge branch_name
# If conflicts arise:
# Edit the conflicting files
git add resolved_file
git commit -m "Resolved merge conflicts"
Example with Reactive Forms:
typescript:
import { FormGroup, FormControl, Validators } from '@angular/forms';
const form = new FormGroup({
name: new FormControl('', [Validators.required, Validators.minLength(3)]),
});
Example with Template-driven Forms:
html:
<form #form="ngForm">
<input name="name" ngModel required minlength="3">
</form>
Use git rebase to apply your branch’s changes on top of another branch.
Code in bash:
git checkout feature_branch
git rebase main
Use git branch -d to delete a local branch, and git push origin --delete to remove a remote branch.
Code in bash:
git branch -d branch_name
git push origin --delete branch_name
git stash allows you to temporarily save changes without committing them, so you can work on something else and then return to those changes later.
Code in bash:
git stash
git stash pop
Use git log --graph to visualize the commit history as a graph.
Code in bash:
git log --graph --oneline --all
Git hooks are scripts that run automatically at specific points in the Git workflow, such as before a commit or after a push.
Code in bash:
# Example: Pre-commit hook
echo "#!/bin/sh" > .git/hooks/pre-commit
echo "echo 'Running pre-commit hook'" >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Submodules allow you to keep a Git repository as a subdirectory of another Git repository.
Code in bash:
git submodule add https://github.com/username/repo_name.git path/to/submodule
git submodule update --init --recursive
Git Large File Storage (LFS) is an extension that allows you to manage large files in a Git repository by storing them outside the repository.
Code in bash:
git lfs track "*.psd"
git add .gitattributes
git add large_file.psd
git commit -m "Add large file"
git push origin branch_name
Use git bisect to perform a binary search to find the commit that introduced a bug.
Code in bash:
git bisect start
git bisect bad # Mark the current commit as bad
git bisect good commit_id # Mark a known good commit
Use git reset --soft to move the HEAD pointer to the previous commit, but keep the changes in the working directory.
Code in bash:
git reset --soft HEAD~1
Squashing commits means combining multiple commits into a single commit. This is usually done during a rebase.
Code in bash:
git rebase -i HEAD~n # where n is the number of commits to squash
Use git show to display the details of the most recent commit.
Code in bash:
git show
Use git reset to unstage a file.
Code in bash:
git reset file_name
Get in touch
We are here to help you & your business
We provide expert guidance, personalized support, and resources to help you excel in your digital marketing career.
Timing
9:00 am - 5:00 pm
Book Your FREE Digital Marketing Consultation
+91 -8005836769
info@webounstraininghub.in