Every Git command a DevOps Engineer must know(Basic, Advanced)
Tags: git, github, devops, githubactions
Published: 2023-02-13 05:08:07

**Basic Git:**

- **git init:** Initializes an empty git repository 
- **git add :** Adds the files into staging area 
- **git commit :** Selects the changes which are staged for the next commit. 
- git commit -a -m "<commit message>"- commits the changes without the file being undergoing the staging area 
- git remote add origin <URL>- adds the pathway from our local git to the remote repo. 
- git remote set-url origin<URL>-To change the remote repo to another remote.  
- git remote -v- Displays the pathway of remote repo which the user has set. 
- git config --global user.name "<username>" - configures the username 
- git config --global user.passsword "<password>" - configures the password 
- git config --global --edit - To edit the configured details 
- git status - Shows the status of the files and displays whether the respective files are already staged,committed or yet to stage, commit 
- git log- displays the log of the previous commits 
- git log --oneline - displays the previous commits in one line with the starting 8 hashcode characters 
- git diff <fileName> - shows the modifications and updations in the file between the commits 
- git stash - stores our commit in a hidden place and we can make changes to the file prior that commit  
- git stash pop <{head-path}> - returns the previously stashed commit 
- git log --merge- returns the list of commits which are causing merge conflicts 
- git log --after<YY/MM/DD>:returns the commits done after the mentioned date 
- git log --before<YY/MM/DD>:returns the commits done before the mentioned date 
- git branch <branchName>: Creates a new branch with the specified branch name 
- git checkout <branchName>: navigates and makes the head pointer points towards the specific branch 
- git checkout -b <branchName>: creates and navigates to the created branch 
- git merge <branchName>: Merges one branch with another branch 
- fork: creates a copy of an existing repo in the user's remote repo 
- git clone<URL> creates a copy of files of existing repo in the local repo of the user 
- git push origin<branchName>: lets you move local branch into the remote repository 
- git pull origin<branchName>: lets you move your remote branch into the local repo 
- git fetch: downloads the copy of a branch from another repo but doesn't integrate it with your local repo 
- ls : gives the list of files in the current working branch 

**Advanced Git:**

- git reset <commitHashCode>: undo's the changes made in the commit and puts it into staging area 
- git reset --soft<commitCode>: undo's the changes made in the commit and puts them in non committed area 
- git reset --hard<commitCode>:undo's the changes made in the respective commit and deletes the changes. Once used they are deleted permanantly.  
- git reset --mixed: It works the same as 'git reset' 
- git merge --abort : aborts the merging process 
- git merge --reset: undo's the merging process and puts back the commits in the state as they were before merging process 
- git rebase -i HEAD~<no. for commit >: deletes a specifc commit  
- git rebase --abort: aborts the deletion of a branch 
- Git  rebase <branch> : acts same as git merge but moves the commits of entire feature branch on top of the master branch 
- Git rebase –i HEAD~<no of commits> : interactive rebase helps in manipulating the commits. Ex: squash is used to combine two commits, reword is used to edit the commit message. 
 
- Git revert: undo commits in a branch and creates a new commit  
  
- Find .git/objects -type f : displays the staged files present in git in the form of objects 

- Git cat-file –t <commit code>: displays the type of object 
  
- Git cat-file –p <commit code>: displays the content present in the object 
  
- Git gc –aggressive : performs compression on stored git objects freeing up disk space. It identifies a group of similar objects and compress them into a ‘pack’.  
 
- Git verify-pack –v .git/objects/pack/pack<hashcode>: displays the contents of a pack  
 
- Git reflog : gives the history of everything done in local repo 
  
- Git reflog expire –-expire=now –-expire-unreachable=now –all: expire all entries to the reflog that are older than now 
  
- Git show <commit code>: inspects a commit