git — A Compulsory developer Tool
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 branchgit 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.
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.
Synchronize Changes
git remote -v
: Displays all the remotes added to the repository. If you’re forking & cloning the repo, you’llorigin
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.
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 rebase -i HEAD~N
: It opens an interactive rebasing window with last N commits on the current branch.
Leave the first line as it is & change pick
to squash
in rest of the other lines and SAVE+Close the file.
It again opens up a file with commit message, Change the message as you want and SAVE+Close the file.
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.
Check out all the top articles at blog.mindorks.com