GIT COMMANDS INTRO


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] 

ADD A FILE TO THE STAGING AREA

# Stage a changed file
git add [file_or_directory]

# Stage all changed files
git add .

COMMIT TO A LOCAL REPO

git commit<br>git commit -m [commit_message]

STATUS OF A WORKING DIRECTORY

# Status of the local repository
git status 

# Changes to the file name
git diff [file_name] 

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 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 

WORKING WITH REMOTE 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 

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]

RESETTIING

# 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 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s