DEV Community

Matt Drummond
Matt Drummond

Posted on

Useful Git Bash Commands

With tools like Git Desktop and Git for vscode, having a basic understanding of git bash can still be very beneficial. It's also great to have alternatives in your workflow.

Here are some everyday commands that I find helpful.


Commands that reveal

ls -a                         # List files and folders within directory
git status                    # See what's changed on current directory

git diff                      # Shows un-staged & staged difference
git diff --staged             # Shows staged & most recent commit difference

git log --oneline             # Displays each commit on one line
git log --oneline --parents   # Shows parent-child relationship in log 
Enter fullscreen mode Exit fullscreen mode

Create a new git repository

cd ..                # Go to parent directory (main)
mkdir dir-name       # Make a new directory and name it
git init             # Creates a new repository in the directory ".git"
Enter fullscreen mode Exit fullscreen mode

Modify file structure

touch file-name-01 file-name-02   # Create 1 or more files
rm file-name-01                   # Remove a file
Enter fullscreen mode Exit fullscreen mode

Stage, Commit push to remote repository

git add *                           # Stage all files
git commit -m "your message here"   # Commit and add message
git push                            # Sends commit to GitHub
Enter fullscreen mode Exit fullscreen mode

Override most recent commit with new changes

git add my_file.html   # Stage 1 file
git add *              # Stage all files

git commit --amend --no-edit  # Commit update & keep previous message
git push --force-with-lease   # Push update to remote repository
Enter fullscreen mode Exit fullscreen mode

Branch / Merge

Note: Before merging, ensure you are on the correct branch (usually main) before proceeding.

git branch branch_name      # Creates new "FEATURE" branch
git switch branch_name      # Go to a different branch
git switch -c branch_name   # Creates and switches to new branch

git branch                  # Shows all branches   
git merge branch_name       # Merges branch into main
git branch -d branch_name   # Deletes the merged branch
Enter fullscreen mode Exit fullscreen mode

Top comments (0)