What Is Git ( Part-3 )

git basic command - what is git

What Is Git ( Part-3 )

this blog is the third part of What is Git. you can read the first part here: 

git remote

πŸ”— In Git, a “remote” is a reference to a repository hosted on a different location, typically on a remote server or another directory. You can use the “git remote” command to manage these references. Here are some common use cases:

View Remotes: To see the remote repositories associated with your current repository.

$ git remote

Add Remote: To add a new remote repository (e.g., on GitHub).

$ git remote add remote_name remote_url

Update Remote URL: To update an existing remote url.

$ git remote set-url origin new_remote_url

Rename Remote: To rename an existing remote.

$ git remote rename old_name new_name

Remove Remote: To remove a remote reference.

$ git remote remove remote_name

Show Remote Details: To view detailed information about a specific remote.

$ git remote show remote_name

Fetch from Remote: To fetch changes from a remote repository.

$ git fetch remote_name

Remember, remotes allow you to collaborate with others and synchronize changes between local and remote repositories. πŸŒπŸ“‘x

git cherry-pick

πŸ’ The “git cherry-pick” command allows you to apply specific commits from one branch onto another branch. This is useful when you want to pick individual changes from one branch and apply them to another branch without merging the entire branch.

Here’s the basic syntax:

$ git cherry-pick commit_hash

For example, if you want to apply the changes from a specific commit with the hash “abcdef123” onto your current branch:

$ git cherry-pick abcdef123

This will apply the changes introduced by the specified commit to your current branch, creating a new commit with those changes.

Keep in mind that conflicts can occur during cherry-picking, just like with merging. Make sure to review and resolve any conflicts that might arise. πŸ’πŸ”€

git clean

🧹 The “git clean” command is used to remove untracked files and directories from your working directory. These are files that Git is not currently tracking. Be cautious when using this command, as it can permanently delete untracked files.

Here’s how you can use it:

$ git clean [options]

Some common options:

  • -n or --dry-run: Shows what would be deleted without actually deleting anything.
  • -f or --force: Performs the clean operation forcefully, actually deleting untracked files.
  • -d: Removes untracked directories as well.
  • -i or --interactive: Interactively choose which files to delete.

For example, to perform a dry run and see what would be deleted:

$ git clean -n

And to actually delete untracked files and directories:

$ git clean -f

Use “git clean” with caution, as it can lead to permanent data loss if used improperly. It’s a good practice to use the -n flag first to preview what will be deleted. 🧹🚫

git status - what is git - MAK Writing House
git status – what is git – MAK Writing House

git gc

πŸ—‘οΈ The “git gc” command, short for “git garbage collection,” is used to optimize and clean up the internal Git repository storage. It helps to reduce the size of your repository and improve performance by removing unnecessary files and objects.

Here’s how you can use it:

$ git gc

Running “git gc” without any options triggers the garbage collection process. Git will identify and remove objects that are no longer needed, consolidate pack files, and generally clean up the repository.

You can also use the “–aggressive” flag for more thorough optimization:

Running “git gc –aggressive” triggers a more comprehensive optimization process for your Git repository. This command attempts to aggressively optimize and compress the repository to save space and improve performance. It can take longer to complete compared to a regular “git gc” operation.

$ git gc --aggressive

Keep in mind that while “git gc –aggressive” can further reduce the repository’s size, it might also require more computational resources and time to complete. It’s generally a good practice to run this command when you have a solid chunk of time available and when your system is not under a heavy load. The benefits of running an aggressive optimization may vary depending on the size and nature of your repository. πŸ—‘οΈπŸ”§πŸš€

Keep in mind that “git gc” is usually automatically performed by Git itself when certain conditions are met, so you might not need to run it manually very often. However, it can be helpful if you notice your repository’s size growing significantly over time. πŸ—‘οΈπŸ”§

git config

πŸ”§ The “git config” command is used to set or get configuration options for your Git repository. These options can be related to user information, repository settings, aliases, and more. Here are some common use cases:

Set User Information:

You can configure your name and email that will be associated with your commits:

$ git config --global user.name "Your Name"
$ git config --global user.email "your@email.com"

View Configuration:

To view your Git configuration settings:

$ git config --list

Set Repository-Specific Configuration

You can set configuration options specific to a repository:

$ git config user.name "Your Name"

Set Aliases:

You can create shortcuts (aliases) for frequently used Git commands:

$ git config --global alias.co checkout

Edit Configuration:

You can directly edit the Git configuration file:

$ git config --global --edit

Remember that you can specify –global for user-level configuration and omit it for repository-specific settings. Git configurations help you customize your Git experience and work more efficiently. πŸ”§πŸ“

this series is continued and you can learn more about this on Git’s official site

Leave a Reply