✨Ultimate Git/Github CheatSheet For Devops✨

✨Ultimate Git/Github CheatSheet For Devops✨

Hello ! Hope your'e doing good this blog includes pretty short and precise explanation of the commands used in Git/GitHub s in day to day scenario.

Set Global Username & email for Git

git config --global user.name "<your.name>"

git config --global user.email "<user.email>"

Basic Git commands

1. git init

This command Initialize a new Git repository in the current directory git init

2.git clone

It clones a remote repository to your local machine git clone <repository-url

Local Changes in Git

3.git status

It show the current status of the repository (modified files, staged changes..)

git status

4.git add

Adds a file to the staging area to be included in the next commit

git add <filename>

gitadd .

The git add . command is used to stage all changes in the current directory and its subdirectories for the next commit. The dot (.) represents the current directory.

5.git commit -m

The git commit -m command in Git is used to create a new commit with a commit message. This command allows you to provide a short and descriptive message that summarizes the changes made in the commit.

git commit -m "Your commit message"

6.git diff

The git diff command in Git is used to show the changes between two states (usually between the working directory and the last commit or between two commits). It displays the differences line by line.

Commit History

7. git log

It displays the commit history of the repository git log

Branches and Tags

8.git branch

The git branch command in Git is used to list, create, or delete branches. It allows you to view the existing branches in your repository, create new branches, and switch between branches.

git branch : This command lists all the branches in your repository.

git branch <branch_name>: This command creates a new branch with the specified name.

git checkout <branch_name> : This command switches to the specified branch.

git checkout -b <new_branch_name> : This creates a new branch and switches to it in one step.

git branch -d <branch_name>: This command deletes the specified branch.

git branch -a : This command shows both local and remote branches.

Update and Publish

9. git pull

The command Fetchs and merge changes from a remote repository into the current branch

git pull

10. git push

It pushes the local commits to a remote repository git push

11.git remote add origin

Add a remote repository as the origin git remote add origin <repository-url>

12. git remote -v

View a list of remote repositories and their URLs git remote -v

13. git fetch

It downloads the objects and refs from another repository without merging.

git fetch

14.git push origin

Push local commits to a remote branch on GitHub git push origin <branch>

15. git pull origin

Fetch and merge changes from a remote branch on GitHub into the current branch. git pull origin <branch>

Merge and Rebase

16. git merge

Merge changes from the specified branch into the current branch.

git merge <branch>

17. git rebase

git rebase is a powerful Git command that allows you to modify the commit history of a branch. It is commonly used to integrate changes from one branch into another, as well as to clean up and organize the commit history.

Rebase to Update a Feature Branch:

Copy codegit checkout feature-branch
git pull origin main
git rebase main

This sequence of commands updates the feature-branch with the latest changes from the main branch, avoiding unnecessary merge commits.

Undo

18.git reset

It Unstages the changes for a file, removing them from the staging area.

git reset <file-name>

19.git revert

It creates a new commit that undoes the changes from a specific commit.

git revert <commit>


Stashing

20.git stash

The git stash command in Git is used to temporarily save changes that are not ready to be committed. This is useful when you need to switch to a different branch, but you don't want to commit your current changes.

git stash list : This command lists all stashes you've created.

git stash apply : This command reapplies the changes.

git stash clear : This command removes all stashed changes, and the stash list becomes empty.

git stash pop : This command pops the most recent stash off the stash stack, applies the changes to your working directory, and removes the stash entry.

21. git cherrypick

The git cherrypick command is used to apply the changes introduced by a specific commit onto your current branch. This is helpful when you want to selectively pick one or more commits from another branch and apply them to your working branch.

git cherrypick <commit-hash> : This command applies the changes from the specified commit to your current branch.

💡Fix Conflicts when Merging

The procedures required to resolve merge conflicts in Git might be shortened by taking a few specific actions.

  • Opening the conflicting file and making the appropriate adjustments is the simplest approach to remedy the issue.

  • After making changes to the file, we may stage the newly merged material using the git add command.

  • The git commit command is used to generate a new commit as the last step.

  • To complete the merging, Git will generate a new merge commit.

Thanks for sticking till the end of this article. Feel freee to share your views and comments about this Blog !

🚀 Let's connect on !

Linkedin

Twitter.