DEV Community

Cover image for Git Commands Introduction
Ephraim Duncan
Ephraim Duncan

Posted on • Updated on

Git Commands Introduction

Git is a very powerful, free, open-source and popular version control used worldwide by millions of developers and large companies. It has been and is used to version millions of software to avoid conflicts and make the work of developers really easy.

This is a Git Commands Handbook, more like a cheatsheet to the various commands for easy reference.

Create a Repo

# Creates a local repo
git init [name] 

# Clones a remote repo
git clone [remote_project_url] 
Enter fullscreen mode Exit fullscreen mode

Add a file or directory to the staging area

# Stage a changed file
git add [file_or_directory]

# Stage all changed files
git add .

# Stage some changes but not all changes
git add --patch [file_name]
Enter fullscreen mode Exit fullscreen mode

Commit to a local repo

git commit
git commit -m "[commit_message]"
Enter fullscreen mode Exit fullscreen mode

Status of Working Directory

# Status of the local repository
git status 

# Changes to the file name
git diff [file_name] 
Enter fullscreen mode Exit fullscreen mode

Branches

# Create a new branch
git branch [new_branch_name]

# List all branches 
git branch  
git branch -a
git branch --list

# Switch to branch
git checkout [branch_name] 
git switch [branch_name]

# Creates a new branch and switch to the branch  
git switch -c [new_branch_name]
git checkout -b [new_branch_name] 

# Merges a branch with another
git merge [branch_name]

# Delete a branch 
git branch -d [branch_name] 

# Branch newFeature has all the commits of branch main
git rebase main newFeature 
Enter fullscreen mode Exit fullscreen mode

Working with Remotes Repositories

# Clones a remote repo
git clone [remote_project_url] 

# List all remotes repositories to the local repo
git remote -v

# Add a remote repository with local repository 
git remote add [remote_project_url] 

# Fetches changes from the remote repository
git fetch [remote_project_url] 

# Fetches changes from the remote repository and merge it to local
git pull 
git pull origin [main_branch_name] 

# Publish local changes to remote repository
git push 
Enter fullscreen mode Exit fullscreen mode

Configuration

# List configuration options
git config --list 

# Set your username
git config --global user.name "Ephraim Atta-Duncan"

# Set your email
git config --global user.email 0x10@gmail.com

# Set your global branch names
git config --global init.defaultBranch [new_default_branch_name]
Enter fullscreen mode Exit fullscreen mode

Resetting

# Revert the changes to exactly what you had
# Go back to HEAD
git reset --hard HEAD

# Go back the commit before head
git reset --head HEAD^

# Go back to two commits before head
git reset --head HEAD~2 

# Revert changes to commits only
# Go back to HEAD
git reset --soft HEAD

# Go back the commit before head
git reset --soft HEAD^

# Go back to two commits before head
git reset --soft HEAD~2 
Enter fullscreen mode Exit fullscreen mode

Top comments (15)

Collapse
 
rzeczuchy profile image
rzeczuchy

Looks like a pretty cool cheat sheet for git newbies :) The only thing you could add maybe is git reset? I think that's the only command I use on a daily basis that's not in here.

Collapse
 
ephraimduncan profile image
Ephraim Duncan

Thanks. I will add it.

Collapse
 
theague profile image
Kody James Ague

Q: Why is your commit message in brackets and not quotes?

Collapse
 
ephraimduncan profile image
Ephraim Duncan

As a placeholder not the neccesary commit message

Collapse
 
theague profile image
Kody James Ague

I guess the reason I asked is because if I were uninitiated I would not know any better and may include the [ ] in my message. I definitely wouldn't know to use quotes around the message.

As an introduction to git commands it would be helpful to know the actual syntax of each command.

Thread Thread
 
ephraimduncan profile image
Ephraim Duncan

Any example suggestion would help.

Thread Thread
 
theague profile image
Kody James Ague

Perhaps format it as you did your username for the configuration section? I'm not sure how to change the color in markdown but I'd make the section between quotes that nice orange color you used.
example: git commit -m "Your message goes between the quotes"

Thread Thread
 
ephraimduncan profile image
Ephraim Duncan

Thanks. I got confused

Collapse
 
zdev1official profile image
ZDev1Official

Thank you!
I had a lot of problems when I need to use git! :P

Collapse
 
ephraimduncan profile image
Ephraim Duncan

Welcome😊

Collapse
 
kitarp29 profile image
Pratik Singh

If even one after this awesome article some one gets confussed on git commands and always googles each command before hitting enter
gitexplorer.com/
Go to this Site its really AWESOME

Collapse
 
ephraimduncan profile image
Ephraim Duncan

Thanks, I'll check it out

Collapse
 
titanhero profile image
Lex

Very cool 😁👍✌️

Collapse
 
green600 profile image
Liegeois Jules

really really cool post !! thank <3

Collapse
 
ephraimduncan profile image
Ephraim Duncan

😊😊