[{"content":"Pre-requisite First, generate a SSH key on your local machine if you don\u0026rsquo;t have one. Follow instructions at GitHub docs on how to do this.\nMethod 1: using ssh-copy-id Step 1: Transfer the Public Key\nRun this command on your local machine, replacing user with the remote username and remote_host with the IP or hostname:\nssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host This command appends your public key to the ~/.ssh/authorized_keys file on the remote host. You\u0026rsquo;ll be prompted for the remote user\u0026rsquo;s password once.\nStep 2: Verify SSH Key authentication works\nTest the connection:\nssh user@remote_host You should be able to log in without being prompted for a password. If it still asks for a password, the key wasn\u0026rsquo;t added properly. So, follow Method 2, or retry Method 1.\nMethod 2: Manually copy your SSH public key If ssh-copy-id is unavailable, follow these steps:\nStep 1: Copy your Public Key\nOn your local machine, display the contents of your public key:\ncat ~/.ssh/id_ed25519.pub Copy the entire output.\nStep 2: SSH into the Remote Host with password authentication\nssh user@remote_host Enter your password when prompted.\nStep 3: Create the .ssh directory (if it doesn\u0026rsquo;t exist)\nOn the remote host, run:\nmkdir -p ~/.ssh Step 4: Add Your Public Key to authorized_keys\nOn the remote host, open the authorized_keys file:\nnano ~/.ssh/authorized_keys Paste your public key on a new line, then save the file.\nStep 5: Set Correct Permissions\nOn the remote host, set the proper permissions:\nchmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh Step 6: Exit the Remote Host\nexit Step 7: Test Key-Based Authentication\nBack on your local machine, test the connection:\nssh user@remote_host You should be able to log in without being prompted for a password.\nAdd a config entry for convenient login Step 1: Create or edit your .ssh/config file\nOn your local machine, open (or create) the config file:\nnano ~/.ssh/config Step 2: Add a Host Entry\nAdd an entry like this (replace with your actual values):\nHost myserver HostName remote_host User user IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes Host myserver — this is the custom name you\u0026rsquo;ll use to log in HostName — the actual IP address or hostname User — the username on the remote machine IdentityFile — path to your private key IdentitiesOnly - only use the identity file(s) you explicitly specify in the config file. Step 3: Save and Set Correct Permissions\nSave the file, then set permissions:\nchmod 600 ~/.ssh/config Step 4: Login using your custom name\nNow you can simply run:\nssh myserver You can add multiple host entries to your config file using the same format for other servers.\n","permalink":"https://gourabsarkar.pages.dev/notes/ssh-key-based-authentication/","summary":"Passwordless and convenient","title":"SSH key-based authentication for Linux"},{"content":"Autowiring by type is a fundamental concept in Spring Boot that simplifies dependency injection. Instead of manually configuring every dependency in your application context, Spring can automatically wire them together based on their data type. This makes your code cleaner, more maintainable, and less prone to configuration errors.\nWhat is Dependency Injection? Before diving into autowiring, it\u0026rsquo;s essential to understand Dependency Injection (DI). DI is a design pattern where an object receives its dependencies from an external source rather than creating them itself. This promotes loose coupling and makes your code easier to test and reuse.\nIn Spring, the \u0026ldquo;external source\u0026rdquo; is typically the Spring IoC (Inversion of Control) container, which manages the lifecycle of your application\u0026rsquo;s beans (objects).\nAutowiring by Type: The Basics When you use autowiring by type, Spring looks for a bean in its application context that matches the data type of the dependency you\u0026rsquo;re trying to inject. If it finds exactly one such bean, it injects it.\nThe primary annotation for autowiring in Spring is @Autowired.\nExample Scenario Let\u0026rsquo;s imagine we\u0026rsquo;re building a simple application where we have a NotificationService that depends on a MessageSender.\nFirst, let\u0026rsquo;s define our interfaces and implementations:\n// Interface for sending messages public interface MessageSender { void sendMessage(String message); } // Implementation for sending email @Service // Spring will recognize this as a component/bean public class EmailSender implements MessageSender { @Override public void sendMessage(String message) { System.out.println(\u0026#34;Sending email: \u0026#34; + message); } } // Implementation for sending SMS (another option) @Service public class SmsSender implements MessageSender { @Override public void sendMessage(String message) { System.out.println(\u0026#34;Sending SMS: \u0026#34; + message); } } Now, let\u0026rsquo;s create our NotificationService that needs a MessageSender:\nimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class NotificationService { private MessageSender messageSender; // The dependency // Constructor Injection (Recommended for autowiring) @Autowired public NotificationService(MessageSender messageSender) { this.messageSender = messageSender; } public void sendNotification(String message) { messageSender.sendMessage(\u0026#34;Notification: \u0026#34; + message); } } And a main application class to run it:\nimport org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class AutowiringByTypeApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AutowiringByTypeApplication.class, args); NotificationService notificationService = context.getBean(NotificationService.class); notificationService.sendNotification(\u0026#34;Hello from Spring Boot!\u0026#34;); context.close(); } } How Autowiring by Type Works Here: @Service Annotation: By annotating EmailSender, SmsSender, and NotificationService with @Service, we\u0026rsquo;re telling Spring to treat them as Spring-managed components (beans). Spring will create instances of these classes and manage their lifecycle.\n@Autowired on Constructor: In NotificationService, we\u0026rsquo;ve placed @Autowired on the constructor. This is the recommended approach for autowiring because it ensures that all required dependencies are provided when the NotificationService object is created.\nType Matching: When Spring encounters the NotificationService bean, it sees that its constructor requires a MessageSender. It then scans its application context for a bean that implements the MessageSender interface.\nUnique Match: In our initial setup, if only EmailSender (or SmsSender) is annotated with @Service, Spring will find exactly one MessageSender implementation and automatically inject an instance of EmailSender (or SmsSender) into NotificationService.\nOutput (if only EmailSender is a @Service):\nSending email: Notification: Hello from Spring Boot! Output (if only SmsSender is a @Service):\nSending SMS: Notification: Hello from Spring Boot! Types of Autowiring (by Type) 1. Constructor Injection (Recommended) @Service public class NotificationService { private MessageSender messageSender; @Autowired public NotificationService(MessageSender messageSender) { this.messageSender = messageSender; } // ... } Pros:\nEnsures that dependencies are available at object creation time, making the object fully initialized. Helps identify missing dependencies at compile time (if using a good IDE). Promotes immutability if the fields are final. Easier to test as you can easily mock dependencies in unit tests. 2. Setter Injection @Service public class NotificationService { private MessageSender messageSender; @Autowired public void setMessageSender(MessageSender messageSender) { this.messageSender = messageSender; } // ... } Pros:\nAllows optional dependencies. Useful for circular dependencies (though often a sign of bad design). Cons:\nObject might be in an incomplete state immediately after construction. Dependencies can be changed after creation, potentially leading to unexpected behavior. 3. Field Injection (Not Recommended) @Service public class NotificationService { @Autowired private MessageSender messageSender; // ... } Pros:\nMost concise. Cons:\nDiscouraged: Hides dependencies, making the class harder to test and understand its requirements. Makes it difficult to create an instance of the class outside the Spring context (e.g., in unit tests) without using reflection or specific Spring testing utilities. Breaks the principle of \u0026ldquo;explicit dependencies.\u0026rdquo; Handling Ambiguity: Multiple Beans of the Same Type What happens if you have multiple beans of the same type in your application context? For example, if both EmailSender and SmsSender are marked with @Service:\n@Service public class EmailSender implements MessageSender { /* ... */ } @Service public class SmsSender implements MessageSender { /* ... */ } If Spring tries to autowire a MessageSender into NotificationService, it will encounter an NoUniqueBeanDefinitionException because it doesn\u0026rsquo;t know which one to choose.\nSpring offers several ways to resolve this ambiguity:\n1. @Qualifier The @Qualifier annotation allows you to specify exactly which bean you want to inject by its name. By default, the bean name is the lowercase version of the class name (e.g., emailSender, smsSender).\nimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class NotificationService { private MessageSender messageSender; @Autowired public NotificationService(@Qualifier(\u0026#34;emailSender\u0026#34;) MessageSender messageSender) { this.messageSender = messageSender; } public void sendNotification(String message) { messageSender.sendMessage(\u0026#34;Notification: \u0026#34; + message); } } Now, NotificationService will specifically receive the emailSender bean.\n2. @Primary If you have a preferred bean among multiple candidates of the same type, you can mark it with @Primary. Spring will then prioritize this bean when autowiring by type.\n@Service @Primary // This will be the default MessageSender public class EmailSender implements MessageSender { @Override public void sendMessage(String message) { System.out.println(\u0026#34;Sending email: \u0026#34; + message); } } @Service public class SmsSender implements MessageSender { @Override public void sendMessage(String message) { System.out.println(\u0026#34;Sending SMS: \u0026#34; + message); } } Now, even without @Qualifier in NotificationService, Spring will inject EmailSender by default. You can still use @Qualifier if you explicitly want the non-primary one.\n3. Renaming Beans (Less Common for Autowiring by Type) You can explicitly name your beans using the @Component, @Service, @Repository, or @Controller annotations:\n@Service(\u0026#34;myEmailSender\u0026#34;) public class EmailSender implements MessageSender { /* ... */ } Then you would use @Qualifier(\u0026quot;myEmailSender\u0026quot;) to inject it. While possible, @Qualifier with the default class-name-based bean name is more common.\nAutowiring Lists and Maps Spring Boot can also autowire collections of beans of a specific type.\nAutowiring a List If you want all implementations of MessageSender:\nimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class MultiNotificationService { private List\u0026lt;MessageSender\u0026gt; messageSenders; @Autowired public MultiNotificationService(List\u0026lt;MessageSender\u0026gt; messageSenders) { this.messageSenders = messageSenders; } public void sendNotificationsToAll(String message) { for (MessageSender sender : messageSenders) { sender.sendMessage(\u0026#34;Multi-Notification: \u0026#34; + message); } } } When MultiNotificationService is created, Spring will inject a list containing all beans that implement MessageSender (e.g., EmailSender and SmsSender).\nAutowiring a Map You can also autowire a Map where the keys are the bean names and the values are the bean instances:\nimport org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Map; @Service public class NamedNotificationService { private Map\u0026lt;String, MessageSender\u0026gt; messageSendersMap; @Autowired public NamedNotificationService(Map\u0026lt;String, MessageSender\u0026gt; messageSendersMap) { this.messageSendersMap = messageSendersMap; } public void sendSpecificNotification(String type, String message) { MessageSender sender = messageSendersMap.get(type); if (sender != null) { sender.sendMessage(\u0026#34;Specific Notification (\u0026#34; + type + \u0026#34;): \u0026#34; + message); } else { System.out.println(\u0026#34;No sender found for type: \u0026#34; + type); } } } In your main application:\n// ... in main method NamedNotificationService namedService = context.getBean(NamedNotificationService.class); namedService.sendSpecificNotification(\u0026#34;emailSender\u0026#34;, \u0026#34;Important email!\u0026#34;); namedService.sendSpecificNotification(\u0026#34;smsSender\u0026#34;, \u0026#34;Urgent SMS!\u0026#34;); // ... @Autowired and required Property By default, @Autowired dependencies are required. If Spring cannot find a matching bean, it will throw an exception (NoSuchBeanDefinitionException). You can make a dependency optional by setting the required attribute to false:\n@Autowired(required = false) private Optional\u0026lt;MessageSender\u0026gt; messageSender; // Use Optional for clarity If required = false and no bean is found, the field will remain null (for field injection) or the setter method won\u0026rsquo;t be called. For constructor injection, you\u0026rsquo;d typically use Optional\u0026lt;T\u0026gt; to indicate an optional dependency.\nWhen to Use Autowiring vs. Manual Configuration Autowiring (by type): Ideal for most typical dependency injection scenarios, especially within a Spring Boot application. It reduces boilerplate and makes your code more concise. Manual Configuration (@Bean methods in @Configuration classes): Useful when: You need to instantiate a third-party class that you don\u0026rsquo;t control (cannot annotate with @Service, etc.). You need custom logic to create a bean (e.g., based on properties, conditional logic). You\u0026rsquo;re defining multiple beans of the same type that require different configurations. Autowiring by type using @Autowired is a cornerstone of Spring Boot\u0026rsquo;s dependency injection capabilities. By understanding how Spring matches dependencies by type and how to handle ambiguities with @Qualifier and @Primary, you can build clean, maintainable, and scalable applications with ease. Always prefer constructor injection for clarity and testability.\n","permalink":"https://gourabsarkar.pages.dev/notes/spring-boot-dependency-injection/","summary":"Let\u0026rsquo;s break down how autowiring by type works in Spring Boot, along with examples.","title":"Dependency injection and autowiring by type in Spring Boot"},{"content":"SOLID principles are a set of design guidelines that helps us write better code and follow best practices while programming.\nS → Single responsibility O → Open / Closed L → Liskov substitution I → Interface segregation D → Dependency inversion S → Single Responsibility Each class should have only one sole responsibility and not be filled with excessive functionality.\nFor example, let\u0026rsquo;s assume there\u0026rsquo;s a Square class, a Circle class with their own private member variables. The Square class contains the length of a side of the square. In case of Circle class, it\u0026rsquo;s the radius of the circle. There\u0026rsquo;s also an AreaCalculator class which has a sum method which checks to see if the instance of the shape passed is a Circle or Square and then consequently calculates the area of the shape. The main method has an instance of AreaCalculator class and it adds together the sum of all shapes.\nNow, if we modify the AreaCalculator class to also contain various methods to print the sum of the shapes in various formats, that would violate the Single Responsibility principle.\nThe correct way to comply with Single Responsibility would be to create a separate class called AreaPrinter which will take care of printing the area in all formats.\nO → Open / Closed Classes should be open for extension, but closed for modification. In other words, existing classes should not have to be modified or rewritten in order to implement new functionalities.\nIf a new shape (say Rectangle) was to be added, then the AreaCalculator class would need to be modified and the functionality to check and calculate area for a rectangle would be added. But this would be a violation of Open / Closed principle.\nThe correct way would be to add an interface called Shape with the a method declaration to calculateArea(). All other shapes, Rectangle, Square, and Circle would implement Shape and have their own implementation for area calculation. Finally the new class, Rectangle will also implement Shape interface and have its own implementation for area calculation.\nThe AreaCalculator class would simply accept a (generic) shape and call it\u0026rsquo;s area calculation method as specified by the Shape interface.\nL → Liskov Substitution Every subclass or derived class should be substitutable with their base or parent class.\nContinuing with the last scenario, there\u0026rsquo;s a new class called NoShape which implements the Shape interface. The NoShape class however throws an IllegalStateException in its area calculation method.\nSo in the main method if we try to instantiate an object of NoShape class like:\nShape noShape = new NoShape(); and run the program, then we get an IllegalStateException in the output. So the NoShape violates the Liskov substitution principle.\nHowever, with other shapes, such as Rectangle, Circle and Square, Liskov substitution principle remains valid.\nShape square = new Square(); Shape circle = new Circle(); Shape rectangle = new Rectangle(); I → Interface Segregation Interfaces should not force classes to implement what they can\u0026rsquo;t do. Large interfaces should be divided into small ones.\nA cube is a 3D shape. Unlike 2D shapes like squares, rectangles and circles, cubes also have a volume.\nLet\u0026rsquo;s add a new calculateVolume() method declaration to the Shape interface. Then we can calculate volume for the Cube class too. But all other existing shapes such as Rectangle, Square and Circle will now have errors because they can\u0026rsquo;t implement the calculateVolume() method. And that would violate the interface segregation principle.\nTo comply with interface segregation principle, a new interface called ThreeDimensionalShape should be created and that interface should have the calculateVolume() method. Since Cube has both area and volume, it will need to implement both Shape and ThreeDimensionalShape interfaces containing both calculateArea() and calculateVolume() method implementations. All other shapes - Circle, Rectangle and Square will implement only the Shape interface containing only the calculateArea() method implementation.\nD → Dependency Inversion Components should depend on abstractions, not on concretions. In other words, dependent components should depend on interfaces, not on concrete implementations.\nThe AreaCalculator class has a sum() method which accepts shapes and calculates the sum of their areas. The AreaPrinter class depends on a concrete implementation of AreaCalculator class because it uses its sum() method to get the sum of areas, which it then prints to console. This is a violation of the Dependency Inversion principle.\nThe correct way should be:\nAreaPrinter class should depend on an area calculator interface that declares a sum() method. The AreaCalculator class will implement the mentioned interface and implement its sum() method. The AreaPrinter class\u0026rsquo;s constructor will set the private interface member variable to the constructor argument variable which is the interface itself. The main class can then use AreaPrinter class by passing an implementation of the area calculator interface. If there are multiple implementations of the area calculator interface, all of them can be used simultaneously in different AreaPrinter instances as the AreaPrinter class constructor is compatible with any implementation of the area calculator interface. References\nhttps://youtu.be/_jDNAf3CzeY ","permalink":"https://gourabsarkar.pages.dev/notes/solid-principles/","summary":"Apt name.","title":"SOLID principles"},{"content":"Check version git -v Initial Configurations On a console window:\ngit config --global user.name \u0026#34;Gourab Sarkar\u0026#34; git config --global user.email \u0026#34;gourab.sarkar01@outlook.com\u0026#34; git config --list git config --list to see all your configurations There are a lot of configuration items available.\ngit config --global for User level configurations (recommended). git config --system for System level configurations across Users. git config for Project level configurations. So, if git configuration needs to be created / changed for a particular project on disk, use the following commands.\ngit config user.name \u0026#34;Gourab Sarkar\u0026#34; git config user.email \u0026#34;gourab.sarkar01@outlook.com\u0026#34; git config --list Git Help Getting help from Git is easy. Type:\ngit help for all help commands at once. git help log for help on git logging. Page opens in a browser. git help init for help on initializing a repository. Page opens in a browser. The .git folder Inside a directory which has been already initialized as a Git repository, a hidden directory called .git can be found. This is where git stores all configs and tracking for that repository (or project).\nDeleting this directory will completely remove Git and any of its tracking from that repository (or project).\nList statistics for all files changed in a commit To see statistics for the list of files which changed in the latest (HEAD) commit, use:\ngit show --stat --oneline HEAD Remove the --oneline switch to see detailed changes, similar to how git log and git log --oneline works.\nTo see the list of changed files in a specific commit, use:\ngit show --stat --oneline 5321a8d where 5321a8d is the commit ID of the specific commit for which you want to see the list of changed files.\nDescribing a commit To describe a commit, i.e. to see the diff of all files changed in a commit, use:\ngit show --oneline HEAD OR\ngit show --oneline \u0026lt;COMMIT_SHA\u0026gt; Multi-line commit messages To write out a multi-line commit message, just use: git commit and press Enter. Because a commit must have its message, Git will open the default text editor registered with git, where multiple lines can be typed out. To finalize, save and close the file.\nOnce the file is closed, the commit is automatically made.\nThe Commit Log Displays the commit history with the topmost one being the most recent.\ngit log shows all commits. git log -n 5 shows the 5 most recent commits. git log --since=2020-01-01 shows all commits made on or after Jan 1, 2020. git log --until=2020-06-16 shows all commits made until June 16, 2020. git log --author=\u0026quot;Gourab\u0026quot; shows all commits made by any user whose namestrings have the substring or string \u0026ldquo;Gourab\u0026rdquo;. git log --grep=\u0026quot;Init\u0026quot; shows all commits that have the substring \u0026ldquo;Init\u0026rdquo; in their commit messages. grep stands for Globally search for Regular ExPressions. git log --oneline shows a condensed, single-line version of the commit log. Branches There can be multiple branches in a repo, but the default branch is master.\nListing branches git branch This command lists all branches and highlights the currently selected branch.\nSwitch to an existing branch git checkout my-existing-branch This command will switch the currently selected branch (default: master) to my-existing-branch.\nRenaming current branch git branch -m new-branch-name Renaming a different branch git branch -m old-branch-name new-branch-name To delete the older pushed branch, use:\ngit push origin --delete old-branch-name Create a new branch based off of an existing branch git checkout -b FEATURE This command creates a new branch called FEATURE which is based off of the currently selected branch (usually master by default), then does checkout on it, thus switching your current working branch to FEATURE.\nTo create a new branch based off of another branch (let\u0026rsquo;s say anotherBranch):\ngit checkout -b newFeatureBranch anotherBranch Deleting an existing local branch git branch -d branch_name The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.\nDangerous:\ngit branch -D branch_name The -D option is an alias for --delete --force, which deletes the branch irrespective of its merged status.\nMerge branches To merge branch MySecondFeature into another branch MyFirstFeature:\ngit checkout MyFirstFeature git pull git merge MySecondFeature git merge MySecondFeature merges the branch MySecondFeature into the current working directory branch MyFirstFeature.\nTo abort a merge: git merge --abort.\nUpdate upstream branch and merge into current branch The branch feature is created and checked out from main. To pull updates done on upstream branch main and merge changes into downstream feature branch:\ngit pull origin main Status of a repository git status Removing tracked files To stop tracking a file you need to remove it from the index. This can be achieved with this command.\ngit rm --cached \u0026lt;file\u0026gt; If you want to remove a whole folder, you need to remove all files in it recursively.\ngit rm -r --cached \u0026lt;folder\u0026gt; The removal of the file from the head revision will happen on the next commit.\nNote: While this will not remove the physical file from your local, it will remove the files from other developers machines on next git pull.\nThe Three Trees in Git Git has a three tree architecture:\nHEAD -\u0026gt; Last commit snapshot, next commit\u0026rsquo;s parent Staging Index -\u0026gt; Proposed next commit snapshot Working Directory -\u0026gt; Sandbox Tracking changes to files in the repository with git diff git diff shows changes between the staging tree and the working directory. git diff --staged shows changes between the repository and the staging tree. Compare multiple commits Multiple commits can be compared in Git. The syntax is:\ngit diff old_commit_SHA..new_commit_SHA --color-words git diff 34d1bbc..f55ba2b --color-words git diff 34d1bbc..HEAD --color-words This shows all changes in the 2 commits merged into a single diff.\nUndo changes in the Working Directory To undo changes in the working directory, use git restore: git restore explorers.html To undo all changes at once from the working directory and replace them with copies of files in the repository, use: git restore . The . indicates all changes to all files should be undone.\nUndo Staged changes To undo changes that have already been staged, use:\ngit restore --staged tours.html git restore --staged directory/ To undo all staged changes in current working directory, use:\ngit restore --staged . Undo committed changes To undo commits as well as discard changes from the working directory,\ngit reset --hard HEAD~1 Here HEAD~1 means that the working directory will be restored to the state of 1 commit before the current HEAD. Similarly, HEAD~2 will undo the last 2 commits and discard their changes.\nTo undo commits but keep changes in the working directory so a better commit can be made,\ngit reset HEAD~1 Undo pushed commits (Revert commits) To revert a commit and therefore undo all changes in that commit, use: git revert f70006a where f70006a is the partial SHA ID of the commit that is to be reverted. The commit message still needs to be supplied to this new commit which opens in the default text editor registered with Git.\nTo revert a range of commits and create a new commit with the reverted changes, git revert \u0026lt;oldest_commit_hash\u0026gt;..\u0026lt;latest_commit_hash\u0026gt; Note that the latest_commit_hash is not included in revert.\nYou might also need to do a git revert --continue in case of conflicts after resolving them. Amending commits The latest commit (HEAD commit) can be \u0026ldquo;amended\u0026rdquo;. This means that changes in that commit can be changed, and so can be commit metadata like the commit message. Here\u0026rsquo;s how to do it:\ngit commit --amend -m \u0026#34;Amended commit\u0026#34; The SHA of the HEAD commit will change after amending a commit.\nTechnically, what amend does is that it takes whatever was in the HEAD commit, bring it back down to staging, add whatever changes staging has and then recommit the commit again. This generates a new SHA for the commit as the metadata (or actual data content) for the commit has changed.\nCherry picking commits To cherry pick a commit, use:\ngit cherry-pick commitSHA Cherry pick multiple specific commits git cherry-pick commitsha1 commitsha2 commitsha3 commitsha4 commitsha5 In the above case, note that you can cherry-pick any number of commit hashes at once, and in any order you want. They will simply be applied one-at-a-time, and in the order you specify. If any conflicts arise, you will have to resolve them one-at-a-time then use git add my_file then git cherry-pick --continue when done to continue the cherry-pick process.\nCherry pick a range of commits Notice that to cherry-pick a range of commits, you must specify a starting and ending commit hash, with .. between them. However, in a range of commits, the beginning commit is NOT included. Therefore, to include it, you must specify the commit before the beginning commit. The syntax to specify the preceding commit is to put ~, ~1, or ^ right after your commit, as in: beginning_commit~, which means: \u0026ldquo;the commit right before beginning_commit\u0026rdquo;.\n# A. INCLUDING the beginning_commit git cherry-pick beginning_commit~..ending_commit # OR (same as above) git cherry-pick beginning_commit~1..ending_commit # OR (same as above) git cherry-pick beginning_commit^..ending_commit # B. NOT including the beginning_commit git cherry-pick beginning_commit..ending_commit To specify three commits prior to beginning_commit, you can do this:\nbeginning_commit~3 Specialized stash and pop Add only inventory-core/src/main/java/com/dell/sae/inventory/core/handlers/DeviceDiscoveryHandler.java to the stash.\ngit stash push inventory-core/src/main/java/com/dell/sae/inventory/core/handlers/DeviceDiscoveryHandler.java Perform your pull or merge\ngit pull List all stashes\ngit stash list Show the diff of the most recent stash\ngit stash show -p stash@{0} Apply the most recent stash without removing it\ngit stash apply stash@{0} (Optional) Drop the stash after applying it\ngit stash drop stash@{0} (Optional) Clear all stashes (use with caution!)\ngit stash clear Remove untracked files from the Working Directory To remove untracked files from the working directory, use:\ngit clean -n which would show a list of actions Git would perform on untracked files. But it would not actually remove those files. This is what\u0026rsquo;s called a dry run. To actually remove files, and this action cannot be undone, use:\ngit clean -f Cloning from a local repository on disk Use this command to clone a repo from another folder on disk. Note that this command can be only used with .git contents - such as contents exported by GitHub export.\ngit clone C:\\Users\\gourab\\Downloads\\github_export\\repositories\\GourabIX\\lab.git Cloning a specific branch of a remote repository With this, you fetch all the branches in the repository, checkout to the one you specified, and the specific branch becomes the configured local branch for git push and git pull. But you still fetched all files from each branch.\ngit clone -b \u0026lt;branchname\u0026gt; \u0026lt;remote-repo-url\u0026gt; View remote URL for repository To see the remote URL for a repository, use the following command.\ngit remote -v Change remote URL for repository To change the remote URL for a repository, use the following command. new.git.url/repo must be a valid Git URL.\ngit remote set-url origin new.git.url/repo ","permalink":"https://gourabsarkar.pages.dev/notes/git-quick-guide/","summary":"A quick guide","title":"Working with Git"}]