Advance Git & GitHub for DevOps Engineers

Advance Git & GitHub for DevOps Engineers

Day 9

If you are reading this then I hope you already go through the blog - Basic Git & GitHub for DevOps Engineers.

◉ What is Git and why is it important?

Git is a tool for version management. So in the IT world developers, DevOps and other teams who are managing and developing the code they are using the Git tool for the code version management for their project and that is very important as there may be different environments such as Test, UAT, PROD in the real world and all the platform has a different version of code. So basically that is the importance of the source code versioning tool Git.

◉ What is the difference Between Main Branch and Master Branch?

There is no basic difference between main and master branches,

  1. "Master" Branch: In many version control systems, including Git, the default branch name historically used to be "master." This term dates back to the early days of version control systems when it was commonly used to denote the primary branch or the default development branch. However, in recent years, there has been a movement to transition away from the term "master" due to its association with slavery and to promote more inclusive terminology.

  2. "Main" Branch: In response to the movement mentioned above, many projects and organizations have started adopting the term "main" as an alternative to "master." The "main" branch serves the same purpose as the "master" branch: it represents the default development branch and is typically where the most up-to-date and stable version of the codebase resides. By using "main," these projects aim to promote inclusive language and create a more inclusive and welcoming environment.

◉ Can you explain the difference between Git and GitHub?

Git is the underlying version control system that allows tracking and managing code changes, while GitHub is a platform built on top of Git that provides hosting, collaboration, and project management features. Git can be used independently on local machines, while GitHub provides a centralized platform for hosting and managing Git repositories, facilitating collaboration and community interaction.

◉ How do you create a new repository on GitHub?

  1. In the upper-right corner of any page, use the drop-down menu, and select New repository.

    Screenshot of a GitHub dropdown menu showing options to create new items. The menu item "New repository" is outlined in dark orange.

  2. Type a short, memorable name for your repository. For example, "hello-world".

    Screenshot of the first step in creating a GitHub repository. The "Repository name" field contains the text "hello-world" and is outlined in dark orange.

  3. Optionally, add a description of your repository. For example, "My first repository on GitHub AE."

  4. Choose repository visibility. For more information, see "About repositories."

  5. Select Initialize this repository with a README.

  6. Click Create Repository.

Congratulations! You've successfully created your first repository and initialized it with a README file.

◉ What is the difference between local & remote repositories?

Local and remote repositories are two distinct copies of a project's repository that serve different purposes. Here's an overview of their differences:

Local Repository:

  • A local repository resides on your local machine, typically on your computer's hard drive.

  • It contains the complete history of the project, including all the files, commits, branches, and tags.

  • Developers work directly with the local repository, making changes, creating branches, and committing code.

  • Local repositories provide the ability to work offline, experiment with new features, and test changes before sharing them with others.

Remote Repository:

  • A remote repository is hosted on a server, typically on a platform like GitHub, GitLab, or Bitbucket.

  • It serves as a central location where developers can push their local changes and collaborate with others.

  • Remote repositories facilitate team collaboration, code sharing, and version control across multiple developers or machines.

  • Remote repositories provide a backup of the project's code, allow for easy sharing and synchronization of changes, and enable access from different locations or devices.

Connecting a Local Repository to a Remote Repository: To connect your local repository to a remote repository, follow these general steps:

  1. Create a Remote Repository: First, create a remote repository on the platform of your choice (e.g., GitHub). This is typically done through the platform's interface or by using their API.

  2. Initialize Git in your Local Repository: If you haven't already, navigate to your local repository directory using the command line or a Git GUI tool. Run the command git init to initialize Git within that directory.

  3. Link the Local and Remote Repositories: In your local repository, add the URL of the remote repository as a remote. For example, if your remote repository is on GitHub, you can use the command git remote add origin <remote repository URL> to add it as the "origin" remote.

  4. Push Local Changes to the Remote Repository: After linking the repositories, you can push your local commits to the remote repository using the command git push origin <branch name>. This will upload your local commits to the corresponding branch in the remote repository.

  5. Pull Remote Changes to Local Repository: If there are changes in the remote repository that you want to incorporate into your local repository, you can use the command git pull origin <branch name> to pull and merge those changes into your local branch.

These steps provide a general outline of connecting a local repository to a remote repository. The specific commands and procedures may vary depending on the hosting platform and your specific project setup. It's recommended to refer to the documentation or guides provided by the platform you are using (e.g., GitHub, GitLab) for more detailed instructions on setting up and configuring your remote repository.

◉ Operations using Git and GitHub

◎ Set your user name and email address, which will be associated with your commits.

$ git config --global user.name "Nildip" 
$ git config --global user.email "test@gmail.com"

Why do we need to set the user name and user email? Because when we commit the code that time it needs the user who did commit. so everybody's commit will be registered in their name and we can get to know who did this commit and other details.

  • Create a repository named "git-test-repo" on GitHub

  • Connect your local repository to the repository on GitHub.

  • Create a new file in git-test-repo/README.md & add some content to it

  • Push your local commits to the repository on GitHub

$ echo "# git-test-repo" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/patelnildip/git-test-repo.git
$ git push -u origin main

GIT Commands | Basic to Advanced GIT Commands List That You Should Know –  Junos Notes

◎ Git Commands

○ Creating Snapshot

Initializing a repository

$ git init

Staging files

git add file1.js # Stages a single file

git add file1.js file2.js # Stages multiple files

git add *.js# Stages with a pattern

git add .# Stages the current directory and all its content

Viewing the status

git status #full status

git status -s #Short status

Committing the staged files

git commit -m “Message” # Commits with a one-line message

git commit # Opens the default editor to type a long message

Skipping the staging area

git commit -am “Message”

Removing files

git rm file1.js # Removes from working directory and staging area

git rm --cached file1.js # Removes from staging area only

Renaming or moving files

git mv file1.js file1.txt

Viewing the staged/unstaged changes

git diff# Shows unstaged changes

git diff --staged# Shows staged changes

git diff --cached# Same as the above

Viewing the history

git log # Full history

git log --oneline # Summary

git log --reverse # Lists the commits from the oldest to the newest

Viewing a commit

git show 921a2ff # Shows the given commit

git show HEAD# Shows the last commit

git show HEAD~2# Two steps before the last commit

git show HEAD:file.js# Shows the version of file.js stored in the last commit

Unstaging files (undoing git add)

git restore --staged file.js # Copies the last version of file.js from repo to the index

Discarding local changes

git restore file.js# Copies file.js from index to working directory

git restore file1.js file2.js# Restores multiple files in working directory

git restore . # Discards all local changes (except untracked files)

git clean -fd# Removes all untracked files

Restoring an earlier version of a file

git restore --source=HEAD~2 file.js

○ Browsing History

Viewing the history

git log --stat # Shows the list of modified files

git log --patch # Shows the actual changes (patches)

Filtering the history

git log -3# Shows the last 3 entries

git log --author=“Mosh”

git log --before=“2020-08-17”

git log --after=“one week ago”

git log --grep=“GUI” # Commits with “GUI” in their message

git log -S“GUI” # Commits with “GUI” in their patches

git log hash1..hash2 # Range of commits

git log file.txt # Commits that touched file.txt

Formatting the log output

git log --pretty=format:”%an committed %H”

Creating an alias

git config --global alias.lg “log --oneline"

Viewing a commit

git show HEAD~2

git show HEAD~2:file1.txt # Shows the version of the file stored in this commit

Comparing commits

git diff HEAD~2 HEAD # Shows the changes between two commits

git diff HEAD~2 HEAD file.txt # Changes to file.txt only

Checking out a commit

git checkout dad47ed # Checks out the given commit

git checkout master # Checks out the master branch

Finding a bad commit

git bisect start

git bisect bad# Marks the current commit as a bad commit

git bisect good ca49180# Marks the given commit as a good commit

git bisect reset# Terminates the bisect session

Finding contributors

git shortlog

Viewing the history of a file

git log file.txt# Shows the commits that touched file.txt

git log --stat file.txt # Shows statistics (the number of changes) for file.txt

git log --patch file.txt# Shows the patches (changes) applied to file.txt

Finding the author of lines

git blame file.txt # Shows the author of each line in file.txt

Tagging

git tag v1.0# Tags the last commit as v1.0

git tag v1.0 5e7a828# Tags an earlier commit

git tag# Lists all the tags

git tag -d v1.0# Deletes the given tag

○ Branching & Merging

Managing branches

git branch bugfix # Creates a new branch called bugfix

git checkout bugfix # Switches to the bugfix branch

git switch bugfix# Same as the above

git switch -C bugfix# Creates and switches

git branch -d bugfix# Deletes the bugfix branch

Comparing branches

git log master..bugfix # Lists the commits in the bugfix branch not in master

git diff master..bugfix # Shows the summary of changes

Stashing

git stash push -m “New tax rules”# Creates a new stash

git stash list# Lists all the stashes

git stash show stash@{1} # Shows the given stash

git stash show 1# shortcut for stash@{1}

git stash apply 1 # Applies the given stash to the working dir

git stash drop 1 # Deletes the given stash

git stash clear# Deletes all the stashes

Merging

git merge bugfix# Merges the bugfix branch into the current branch

git merge --no-ff bugfix# Creates a merge commit even if FF is possible

git merge --squash bugfix # Performs a squash merge

git merge --abort # Aborts the merge

Viewing the merged branches

git branch --merged# Shows the merged branches

git branch --no-merged# Shows the unmerged branches

Rebasing

git rebase master # Changes the base of the current branch

Cherry picking

git cherry-pick dad47ed # Applies the given commit on the current branch

○ Collaboration

Cloning a repository

git clone <url>

Syncing with remotes

git fetch origin master # Fetches master from origin

git fetch origin# Fetches all objects from origin

git fetch# Shortcut for “git fetch origin”

git pull# Fetch + merge

git push origin master # Pushes master to origin

git push# Shortcut for “git push origin master”

Sharing tags

git push origin v1.0# Pushes tag v1.0 to origin

git push origin —delete v1.0

Sharing branches

git branch -r# Shows remote tracking branches

git branch -vv# Shows local & remote tracking branches

git push -u origin bugfix# Pushes bugfix to origin

git push -d origin bugfix# Removes bugfix from origin

Managing remotes

git remote# Shows remote repos

git remote add upstream <url># Adds a new remote called upstream

git remote rm upstream# Remotes upstream

○ Rewriting History

Undoing commits

git reset --soft HEAD^ # Removes the last commit, keeps changed staged

git reset --mixed HEAD^ # Unstages the changes as well

git reset --hard HEAD^# Discards local changes

Reverting commits

git revert 72856ea # Reverts the given commit

git revert HEAD~3..# Reverts the last three commits

git revert --no-commit HEAD~3..

Recovering lost commits

git reflog# Shows the history of HEAD

git reflog show bugfix# Shows the history of bugfix pointer.

Amending the last commit

git commit --amend

Interactive rebasing

git rebase -i HEAD~5

Thanks for reading the blog!