git — A Compulsory developer Tool

Anamika Tripathi
MindOrks
Published in
5 min readJun 18, 2018

--

I’ve been approached several time with similar question which says “I want to start contributing to open source. Can you help me?” Answering the same thing to everyone gets difficult & that’s why, I’m writing to help the community by my prior experience.

As we all know, Git is a necessity for developers & it’s also a boon for all of us. But How much deep understanding of Git is required before starting the open source contribution? Answers may vary with different person & I’ll share my view on it. If you haven’t installed git yet, check the steps here.

This blog covers: Basics of Git, Reset the files,Review history,Branching,Synchronize Changes,Save fragments,Amending & squashing the commits.

Basics of Git

Clone the repository: git clone [URL] Downloads a project and its entire version history.

Adding files to staging area: git add [filename] or git add . for all files. This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit.

Status of repository: git status This command lists all new or modified files to be committed.

Commit the files: git commit -m [descriptive message] : This command records snapshots permanently in version history.

Reset the files

git reset changes, at minimum, where the current branch (HEAD) is pointing & it can be soft/hard both.

git reset -- soft : uncommit changes, changes is left staged (index).

git reset -- hard : uncommit + unstage + delete changes, nothing left.

Review History

  • git log : Lists version history for the current branch
  • git diff [first-branch] [second-branch] : Shows the content differences between two branches.
  • git diff : This command show the file differences that are not staged yet. git diff --staged : It shows file differences between staging and the last file version.
log details small example
git diff & git diff — — staged

Branching

  • git branch : Lists all local branches in the current repository.
  • git branch [branch-name] : Creates a new branch.
  • git checkout [branch-name] : Switches to the specified branch and updates the working directory.
  • git checkout -b [branch-name] : Creates a new branch with specified name & switches to it.
  • git merge [branch] : Combines the specified branch’s history into the current branch. Be-aware! You must checkout the required branch before merging.
  • git branch -d [branch-name] : Deletes the specified branch.
List, checkout & merge branch
Create a new branch & delete it.

Synchronize Changes

  • git remote -v : Displays all the remotes added to the repository. If you’re forking & cloning the repo, you’ll origin pointing towards your forked project.
  • git remote add upstream [URL] : Adds a remote named upstream which points to the specified Url.
  • git fetch upstream : Downloads all the changes from specified remote.
  • git rebase -i upstream/master : Perform it after fetching the code. It rebases i.e merges all the code to the local branch in a interactive way.
  • git push [remote-name] [branch-name] : Uploads all local branch commits to github.
  • git pull [remote-name] [branch-name] : Downloads all changes from remote specified & incorporates changes.

Save Fragments

  • git stash : Temporarily stores all modified tracked files.
  • git stash list : List all stashed changesets.
  • git stash drop [n] : Deletes the nth changesets.
Example of stash commands

Some difficult commands

Amend the last commit: If you want to change the actual content of your last commit, first make the changes you think you forgot, stage those changes, and the subsequent git commit --amend replaces that last commit with your new, improved commit.

Squashing the commits: it take a series of commits and squash them down into a single commit with the interactive rebasing tool. Follow the order:

git log before rebasing

git rebase -i HEAD~N : It opens an interactive rebasing window with last N commits on the current branch.

1st interactive screen

Leave the first line as it is & change pick to squash in rest of the other lines and SAVE+Close the file.

changed pick to squash

It again opens up a file with commit message, Change the message as you want and SAVE+Close the file.

After squashing the two commits

References: Udacity course , Short interactive course & Git documentation.

Thanks for reading! Be sure to click claps below to recommend this article if you liked it.

You can connect with me on Github, Twitter, Linkedin :)

Check out all the top articles at blog.mindorks.com

--

--

Anamika Tripathi
MindOrks

Android Developer @Zomato | Google Udacity Scholar | GSoC19 Mentor @Mifos | GSoC18 student @systers_org | GCI Mentor | Tech speaker