DEV Community

Cover image for GIT-Bash Commonly Used Commands.
AxlBlaze
AxlBlaze

Posted on • Updated on

Git Bash Commands GIT-Bash Commonly Used Commands.

Git-bash :-

Git Bash is an application for Microsoft Windows environments which provides an emulation layer for a Git command line experience. Bash is an acronym for Bourne Again Shell. A shell is a terminal application used to interface with an operating system through written commands.

Installing Git Bash :-

To download Git Bash:-

  * 1.Go to [link](https://git-scm.com/downloads)

  * 2.download the software for Windows

  * 3.install Git choosing all the default options
Enter fullscreen mode Exit fullscreen mode

First Time Git Configuration:-

  • Sets up Git with your name:
   >> git config --global user.name "<Your-Full_Name>"
Enter fullscreen mode Exit fullscreen mode
  • Sets up Git with your email:
   >> git config --global user.email "<your-email-address">
Enter fullscreen mode Exit fullscreen mode
  • Makes sure that Git output is colored:
   >> git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode
  • Displays the original state in a conflict:
   >> git config --global merge.conflictstyle diff3

   >> git config --list
Enter fullscreen mode Exit fullscreen mode

Git & Code Editor:-

The last stop of configuration is to get Git working with your code editor. Below are three of the most popular code editors. If you use different editor.

  • Atom Editor Setup
  >> git config --global core.editor "atom --wait"
Enter fullscreen mode Exit fullscreen mode
  • Sublime Text Setup
  >> git config --global core.editor "C:Program Files/ SublimeText2 /sublime_text.exe' -n -w"
Enter fullscreen mode Exit fullscreen mode
  • VSCode Setup
  >> git config --global core.editor "code --wait"
Enter fullscreen mode Exit fullscreen mode

Repository Commands:-

  • Create brand new repositorie(reps) on your computer.
  >> git init
Enter fullscreen mode Exit fullscreen mode
  • Copy existing repos from somewhere else to your local computer.
  >> git clone <url>
Enter fullscreen mode Exit fullscreen mode
  • Check the status of a repo.
  >> git status
Enter fullscreen mode Exit fullscreen mode

Useful Commands

1. ls - Used to list files and directories.

2. mkdir - Used to create a new directory.

3. cd - Used to change directories.

4. rm- Used to remove files and directories.
Enter fullscreen mode Exit fullscreen mode

Review Repo History

Displays information about the existing commits.

  • It shows the SHA, the author, the date, the commit message.
  >> git log 
Enter fullscreen mode Exit fullscreen mode
  • The git log --online show first 6 letter of SHA and the commit message.
  >> git log --oneline
Enter fullscreen mode Exit fullscreen mode
  • Displays information about the given commit. The output of the git show command is exactly the same as the git log -p command.
  >> git show
Enter fullscreen mode Exit fullscreen mode

View Files Changes

  • This command is used to display the files that have been changed in the commmit as well as the number of lines that have been added or deleted.
  >> git log --stat
Enter fullscreen mode Exit fullscreen mode
  • The git log command has a flag that can be used to display the actual changes made to a file
  >> git log -p
Enter fullscreen mode Exit fullscreen mode
  • The git show command will show only one commit.
  >>git show <sha id>
Enter fullscreen mode Exit fullscreen mode

Add Commits to a repo:-

1.Add files from the workng direcotry to the staging index.
  • Add all files
  >> git add .
Enter fullscreen mode Exit fullscreen mode

*Add specific files

  >> git add <filename>,<filename>
Enter fullscreen mode Exit fullscreen mode
2.This command will not distroy the work but it just removes it from the Staging index.
  >> git rm --cached <filename>
Enter fullscreen mode Exit fullscreen mode
3.Take files from the staging index and save them in the repository.
  • Commit files
  >> git commit
Enter fullscreen mode Exit fullscreen mode
  • Commit the repo with message.
  >> git commit -m "Initial commit"  
Enter fullscreen mode Exit fullscreen mode
  • Displays the difference between two version of commits.
  >> git diff
Enter fullscreen mode Exit fullscreen mode

Tagging, Branching and Merging

Tagging

Git allow to put tag on various commit.
  • Add tags to specific commits
  >> git tag -a v1.0
Enter fullscreen mode Exit fullscreen mode
  • A Git tag can be deleted with the -d flag (for delete!)
  >> git tag -d v1.0
Enter fullscreen mode Exit fullscreen mode
  • To show all existing tag
  >> git tag 
Enter fullscreen mode Exit fullscreen mode

Branching

Git branch Allow multiple lines of development
It can be use to
  • List all branch name in the repository
  >> git branch 
Enter fullscreen mode Exit fullscreen mode
  • Create new branch
  >> git branch <branchname>
Enter fullscreen mode Exit fullscreen mode
  • Create new branch from existing commit>
  >> git branch <branchname> <sha-id of existing branch>
Enter fullscreen mode Exit fullscreen mode

*Delete branch

  >> git branch -d <branchname>
Enter fullscreen mode Exit fullscreen mode

Change Working Branch:

  • Checkout is used to change the branch from one to another.
  >> git checkout <branchname>
Enter fullscreen mode Exit fullscreen mode

Merging

  • Combine the differnce of different branches.
  >> git merge  <name-of-branch-to-merge-in>
Enter fullscreen mode Exit fullscreen mode

Merge Conflict

The editor has the following merge conflict indicators:

  <<<<<<< HEAD everything below this line (until the next indicator) shows you what's on the current branch.

  ||||||| merged common ancestors everything below this line (until the next indicator) shows you what the original lines were.

  ======= is the end of the original lines, everything that follows (until the next indicator) is what's on the branch that's being merged in.

  >>>>>>> heading-update is the ending indicator of what's on the branch that's being merged in (in this case, the heading-update branch)
  Resolving A Merge Conflict.
Git is using the merge conflict indicators to show you what lines caused the merge conflict on the two different branches as well as what the original line used to have. So to resolve a merge conflict, you need to:

  1. Choose which line(s) to keep.

  2. Remove all lines with indicators.
Enter fullscreen mode Exit fullscreen mode

Undoing changes

  • Changing the most recent commit-
  >> git commit --amend 
Enter fullscreen mode Exit fullscreen mode
  • Reverse given commint given with sha-
  >> git revert <sha of commit to revert>
Enter fullscreen mode Exit fullscreen mode
  • Erases(reset) commits
  >> git reset <reference-to-commit>

  >> git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode
Git Reset's Flags
The way that Git determines if it erases, stages previously committed changes, or unstages previously committed changes is by the flag that's used. The flags are:

--mixed take the changes made in commit and move them to the working directory

--soft the changes moved to the Staging Index

--hard The changes are complete erased
Enter fullscreen mode Exit fullscreen mode

Backup Branch

  >> git branch backup
Enter fullscreen mode Exit fullscreen mode

Push Date to Github Repo:-

 >>git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Top comments (0)