Starting with Git
As a software engineer, we tend to learn lots of technology. We try to learn new coding languages, new frameworks, newer ways to optimize the software as well as automating things. However sometimes, we forget the basics. Learning all these is useful, however one of the main thing to learn is understanding and learning about version control system.
Get howdoicode’s stories in your inbox
Join Medium for free to get updates from this writer.
One of such tool is Git. Today we will cover the absolute basics of using Git.
Git Clone/Remote
The first step for any programmer is to fetch the code from remote repositories to the local system. How?
$ git clone <https://github.com/someone/something.git>$ git remote add <remote_branch_url> {brings in the remote branches to system}$ git remote -v {shows all the remote branches/repositories on system}Git Branches
Once you clone the repository, how can we know which branch are we working on. Before that, what is a Branch? Branching a a basically a strategy where multiple people can collaborate on the same project without disturbing each others work. Each developer can work on their own branch and once the code is reviewed and bug free, this branch can be merged to the main branch.
$ git branch {shows the current and recent branches}$ git branch -a {shows all the branches in your fork}$ git branch -D <branch_name> {deletes the branch from remote and local}$ git checkout <branch_name> {creates new branch}$ git checkout -b <branch_name> remote/branchX {creates a branch with the latest commit on branchX}}Git Changes
Whenever we make changes within the code, this code changes needs to get updated to the repository. However sometimes it is possible, that by mistake, we add wrong code. How to avoid this?
$ git status {shows all the files where changes are done}$ git diff <file_name> {shows all the changes done within this file}$ git add <file_name> {stages the file to be committed}$ git add . {stages all the files to be committed}$ git commit -m <some_msg> {commits all the changes to repository with message}$ git log {shows the history of all the commits}$ git fetch --all {fetches latest changes from all the branches across all repos}$ git push {pushes the committed changes to your remote repository}Git Stash
Sometimes a developer is working on multiple branches. In such cases, developer wants to store (stash) the code temporarily, and change the branch. How?
$ git stash {stores the changes}$ git stash pop {brings back the stored changes after branch change}$ git stash drop {discard all the changes in stash}Git Cherry Pick
One of the most powerful commands in Git is cherry-pick. Lets say as a developer you want to bring in the code changes done by some other developer. But you want to just get a very specific change. Each change in Git has an associated commit id. With this commit-id, a developer can bring that code into their own branch and merge it as a new commit. How?
$ git cherry-pick <commit-id>Let me know in the comments what else do you think can be a part of basic Git learning. Happy Reading!!





