Working with Git
Scenario
John is a budding software developer excited about building usable products, he develops and hosts applications using cheap hosting services. After a few months of diving into software development, he felt it was time to get a job to show what he has learnt and also work with teams.
During an interview with one of the well-known firms he applied to, he was given a task to work on. One of the requirements for being successful was to have an understanding of working with git. Poor John who has been working alone was not aware of the usefulness of version control in software development. He has been hearing about the concept but does not fully understand how it works.
Guess what! He lost the opportunity because he was actually expected to push the code to the repository when he is done with coding but was not told, so he just finished and left. A few minutes later, the interviewer called him and asked — Hi John, hope you pushed your code to git? He felt very bad losing that opportunity as a result of not understanding that version control is an integral part of software development.
If you are like John, this article is for you. Let’s dive right in …
What is version control?
A system that records changes to a file or set of files over time so that you can recall specific versions later
We have Centralized Version Control Systems where we have a single server that contains all the versioned files, and a number of clients that check out files from that central place and Distributed Version Control Systems in which every clone is really a full backup of all the data.
The second option is widely used. The popular GitHub and Bitbucket use this method.
Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files.
To work with git, you need to be familiar with using the command line to run the commands below. There are GUI's that abstract these for you but as a software developer, it is advisable to use commands.
Join the train let’s move…
Open your terminal
Set a Git username globally:
$ git config --global user.name “your_user_name”
Set a Git email globally:
$ git config --global user.email “your_email”
Confirm that you have set the Git details correctly:
$ git config --global user.name$ git config --global user.email
To view your git configurations
$ git config --list --show-origin
Initialize Git in the repository:
$ git init
To start with a remote repository
$ git clone repository_URL
Create a new branch for the aspect of the project you want to work on and change the current working directory to your new branch:
$ git checkout -b name_of_your_new_branch
or Switch to an existing branch
$ git checkout name_of_the_branch
Add specific file
$ git add filename.extension
or Add all changes
$ git add .
Add README file
$ echo "# repository_name" >> README.md
Commit changes
$ git commit -m “commit_message”
Check the current state of your branch
$ git status
Delete a directory from Git AND local
$ git rm -r directory_name
Delete a directory from Git BUT NOT LOCAL
$ git rm -r --cached directory_name
Delete a file from Git AND local
$ git rm file_name.extension
Delete a file from Git BUT NOT LOCAL
$ git rm --cached file_name.extension
Forcefully Delete a staged file
$ git rm -f filename.extension
Unstage the file
$ git reset HEAD filename.extension
View git logs
$ git log --summary
View all your branches:
$ git branch
Add a new remote for your branch:
$ git remote add name_of_your_remote remote_URLe.g $ git remote add origin https://github.com/username/project_name.git
View your current remote repository
$ git remote show origin
Push changes from your commit into your branch :
$ git push name_of_your_remote name_of_your_branche.g $ git push origin master
To push the current branch and set the remote as upstream:
$ git push --set-upstream origin name_of_your_new_remotee.g $ git push --set-upstream origin master
Update your branch when the original branch from the official repository has been updated :
$ git fetch name_of_your_remote
or
$ git pull
Merge changes from master to your branch:
$ git checkout your_branch$ git merge master
Merge changes from your branch to the master branch:
$ git checkout master$ git merge your_branch
View differences in branches
$ git diff name_of_the_source_branch name_of_the_target_branch
Fetch updates from a forked repository
$ cd the_forked_repo$ git remote add upstream forked_repo_URL$ git pull upstream master
Change your remote URL
$ git remote set-url origin remote_URL
To rename a local branch
- If you are on the branch:
$ git branch -m new_name
- If you are on another branch:
$ git branch -m current_name new_name
To rename a remote branch, delete the current branch and push the new one:
$ git push origin :name_of_your_branch$ git push origin -u new_name_for_the_branch
Delete a branch on your local file system :
$ git branch -d name_of_your_branch
Delete the branch on Github :
$ git push origin :name_of_your_branch
Version Control Tools
Some tools make it possible for us to avoid the hassle of writing these commands by abstracting its complexities.
GitLens extension for VS Code
Source tree (for Mac and Windows)
VCS menu option on IntelliJ IDEs (PHP Storm, Android Studio, etc.)
Bonus
Learn how to configure your terminal with ZSH for maximum output
Practice while doing on https://try.github.io/
Thank you for reading!
If you like my article, please hit this icon 👏 below as many times as possible, bookmark, share it with your friends and follow me for more stories. You can add any version control tool you find useful in the comment section.