Advanced Git And GitHub Commands

๐Ÿ“ Task-1: Set up Your Identity

Before you dive into the world of DevOps and start collaborating on projects, it's essential to set up your identity to be associated with your commits. This ensures that your contributions are appropriately recognized. Follow these steps to configure your username and email address:

Configure Your Username: Open your terminal or command prompt and run the following
Git command, replacing "YourUsername" with the desired name:

git config --global user.name "YourUsername"

Set Your Email Address: Run the next Git command, replacing "youremail@example.com" with your preferred email address:

git config --global user.email "youremail@example.com" Now you're all set to start making contributions with your unique identity!

๐ŸŒ Task-2: Creating a "Devops" Repository on GitHub

A repository is like a project folder where you can store and manage your code. To create one named "Devops" on GitHub, follow these steps:

Login to GitHub: If you don't have an account, sign up on github.com.

Create a New Repository: Click on the "+" sign in the top-right corner of your GitHub dashboard and select "New repository."

Name Your Repository: Enter "Devops" as the repository name. You can also add a brief description if you like.

Set Repository Visibility: You can choose whether you want the repository to be public (visible to everyone) or private (only visible to you and collaborators). For this example, let's make it public.

Initialize with a README: To keep things organized, it's a good practice to initialize the repository with a README file. Check the option that says "Initialize this repository with a README" to create a default README file.

Create the Repository: Finally, click on the "Create repository" button to create the "Devops" repository.

๐Ÿ”— Connecting Your Local Repository to GitHub

Now that you have created the "Devops" repository on GitHub, it's time to connect your local repository to it. Follow these steps:

Copy the Repository URL: On the main page of your newly created repository, click on the green "Code" button and copy the repository URL. It should look something like: https://github.com/YourUsername/Devops.git.

Link the Local Repository: Open your terminal or command prompt, navigate to your local "Devops" repository directory, and run the following command, replacing with the URL you copied in the previous step:

git remote add origin <repository-url>

Verify the Connection: To ensure that the remote repository is linked correctly, run the following command:

git remote -v

You should see the origin URL listed as a remote repository.

๐Ÿ“„ Creating and Pushing Content to "Day-02.txt"

Now that you have successfully connected your local repository to the one on GitHub, let's add some content to a new file and push it:

Create "Day-02.txt": In your local repository, navigate to the "Devops/Git" directory (create it if it doesn't exist). Then, create a new file named "Day-02.txt."

Add Content to the File: Open "Day-02.txt" with a text editor and add some meaningful content related to your DevOps journey or any notes you'd like to share.

Stage and Commit: In your terminal or command prompt, go to the "Devops" repository directory and run the following commands:

git add Git/Day-02.txt

git commit -m "Add Day-02.txt with initial content"

Push to GitHub: Finally, push your local commits to the GitHub repository with the following command:

git push origin main

finally, push our local repository to remote repository

๐ŸŒฟAdvance Git Command๐ŸŒฟ

  1. Branching Commands:

๐ŸŒฟ git branch: List all branches in the repository.

๐ŸŒฑ git branch : Create a new branch with the given name.

๐ŸŒณ git checkout : Switch to the specified branch.

๐ŸŒฒ git checkout -b : Create a new branch and switch to it in one command.

๐Ÿ‚ git merge : Merge the changes from the specified branch into the current branch.

  1. Pull Request Workflow:

๐Ÿ”„ git fetch: Fetch the latest changes from the remote repository without merging them.

๐Ÿข git pull: Fetch and merge the latest changes from the remote repository into the current branch.

๐Ÿš€ git push : Push the committed changes to the specified branch on the remote repository.

๐ŸŒ git remote: List all remote repositories linked to the local repository.

๐Ÿ“ก git remote -v: List remote repositories along with their URLs.

  1. Undoing Changes:

โช git reset: Unstage the changes, preserving them in the working directory.

๐Ÿšง git reset --hard: Discard all local changes and reset the branch to the last committed state.

โฎ๏ธ git revert : Create a new commit that undoes the changes introduced by the specified commit.

๐Ÿ—‘๏ธ git clean: Remove untracked files from the working directory.

  1. Inspecting History:

๐Ÿ“œ git log: View the commit history.

๐Ÿ“ƒ git log --oneline: View a condensed version of the commit history.

๐Ÿ—บ๏ธ git log --graph: Display the commit history as a graph.

๐Ÿ“Œ git show : Display the details of a specific commit.

  1. Git Config:

โš™๏ธ git config --global core.editor : Set the default text editor for commit messages.

๐Ÿ”„ git config --global alias.<alias-name> <git-command> : Create an alias for a Git command to use a shorter version.

  1. GitHub Collaboration:

๐Ÿ“ฅ git clone : Clone a remote repository to your local machine.

๐Ÿ”— git remote add upstream : Add the original repository as the upstream remote for a forked repository.

๐Ÿ”„ git fetch upstream: Fetch changes from the upstream repository after adding it as a remote.

๐Ÿ’ git cherry-pick <commit> : Apply the changes introduced by a specific commit to your branch.

0
Subscribe to my newsletter

Read articles from Saurabh Mathuria directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Mathuria
Saurabh Mathuria