Git Cheat Sheet

Anubhav
Geek Culture
Published in
2 min readJun 5, 2021
Photo by Roman Synkevych on Unsplash

This will be a quick guide to Git’s most common commands which you will be using on day to day basis.

Add Git and Push a Project to Github

cd project/git init                    
# initialises a repository
git add .
# add all the files from working directory to the staging area
git commit -m "COMMIT_MESSAGE"
# commit all changes
git remote add origin https://github.com/GIT_USER_NAME/REPO_NAME.git
# To add a new remote
git push -u origin <new-branch>
# Sync local branch with remote

Clone Repository

git clone <url>
# Clone a repository locally

Branching and Merging

git branch                          
# show list of all branches (* active) (for now we are assuming the current branch is master)
git checkout -b git_demo
# create a new branch with the name git_demo
<make changes>git commit -agit checkout master
# go back to master branch
git merge git_demo
# merge changesets from git_demo (Git >= 1.5)
git pull . git_demo
# merge changesets from git_demo (all Git versions)
git branch -m <oldname> <newname>
# rename branch with oldname to new name
git branch -m <newname>
# rename current branch

Undo unpublished merge

git reset --hard commit_sha
# with git reflog check which commit is one prior the merge (git reflog will be a better option than git log)
another way, git reset --hard HEAD~1
# it will get you back 1 commit.

Delete Repository

git branch -d <branch_name>      
# deletes local branch
git push origin :<branchname>
# deletes remote branch
git remote prune <branchname>
# update local/remote sync

Log

git log
# view information about previous commits, commit hash, author, message, and date

--

--

No responses yet