Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Last active Sep 17, 2021
Embed
What would you like to do?
Git Basics
#Add single file
git add README.md
git add original.txt
#Add all files
git add .
# Create New Branch
git branch testing
# Switch to new branch (or any other branch)
git checkout testing
# Or use the -b option on the checkout command
# to create the new branch and automatically switch to it
git checkout -b testing
# You may also specify which branch to use as the base branch
git checkout -b <new-branch-name> <base-branch>
# Delete a branch with -d if
# branch is either fully merged in
# upstream branch or in HEAD.
git branch -d testing
# Force Delete a branch
git branch -D testing
# Switch to branch we want to merge changes to
git checkout master
# Merge specified branch changes into current branch
git merge testing
# Check any merge conflicts with this merge tool
git mergetool
# switch to the branch you want to merge in master
git checkout test2
# rebase that branch in master
git rebase master
# continue rebase operation after manually,
# resolving the conflicts
git add <name-of-file-with-conflicts>
git rebase --continue
# skip rebase operation
git rebase --skip
# abort and rollback rebase operation
git rebase --abort
# list all branches
git branch
# list all branches with more details
git branch -v
#Commit all changes to local Git repo with a message
git commit -m "first commit!"
#Create a .gitignore file and add files or folders to exclude
echo exclude.txt >> .gitignore
# Initialize empty git repository
git init
#Check the local git log
git log
# Create a Readme file with some content
echo This is our Git Testing Project! >> README.md
# Create a text file
echo This file will be excluded later >> exclude.txt
# add remote to local git
git remote add origin https://github.com/xnorcode/TestProject.git
# add remote to local git including your github account username
# with this command it will only prompt for a password only
git remote add origin https://username-here@github.com/xnorcode/TestProject.git
# rename a remote
git remote rename origin <new-remote-name>
# you can view a list of all added remote reference in your local git
git remote
# a detailed list of remotes
git remote -v
# show info of a spicific remote
git remote show origin
# set a branch to track a remote branch
# eg. <remote-name/local-branch-name>.
# this is also done automatically when you push
# into a remote or when you clone
git branch -u origin/master
# Or if you are not currently on the
# branch you want to track
git branch -u origin/master master
# deleting a remote
git remote rm origin
# clone from remote git repo entire code from scratch.
# this will create new folder with entire project in it.
git clone https://github.com/xnorcode/TestProject.git
# this will download all changes (if any) to an existing
# project from the remote git repository and merge it in
# the local branch
git pull origin master
# this fetches the remote repo but does not automatically
# merge
git fetch origin master
# push current branch named master
# to remote git repo named origin,
git push origin master
# will reset latest commit and keep the changes staged
git reset --soft HEAD~1
# will reset latest commit and permanently delete the changes
git reset --hard HEAD~1
# will reset latest commit and keep the changes unstaged
git reset HEAD~1
# add changes into the stash
git stash
# get changes from stash
git stash pop
#Check Git status
git status
#Check Git Version
git --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment