Git basics in a FLASH!

Photo by Yancy Min on Unsplash

Hello World! Apart from adding, committing and pushing files to the repository, here are some common git operations, in a flash!

1. Branching

Branching is necessary when you want to work on a new feature (or for other reasons) and separate your code from the master branch.

Create a branch and checkout:

git checkout -b nameOfYourNewBranch

Delete a local branch :

git branch -d branchName

Delete a remote branch:

git push origin -d branchName

2. Merging

Merging a branch is necessary because you might want to merge a feature branch to the master branch.

# Start a new feature
git checkout -b new-feature master

# Edit some files
git add <file>git commit -m “Start a feature”

# Edit some files
git add <file>git commit -m “Finish a feature”

# Merge in the new-feature branch
git checkout mastergit merge new-feature

# Delete the new-feature branch
git branch -d new-feature

3. Stashing

Stashing is used when you temporarily want to put away your change and have a clean working tree. Don’t worry, you will be able to get your stashed changes back.

Stashing current work:

git stash

Stash with a message:

git stash save “Your Work In Progress message here”

Listing stashes

git stash list

Importing the latest stash:

git stash pop

Importing a particular stash if more than one:

git stash pop stash@{n}

Dropping stash:

git stash drop stash@{n}

Here, n refers to a number, 0, 1, 2… which represents which stash to delete.

4. Clearing Cache

Clearing cache is required when you want to remove previously added files that you don’t want to track anymore.

Clear all git cache:

git rm -r --cached .

Clear cache of only required file:

git rm fileName --cached