Distributed version control systems like Git are a blessing for projects that require multiple developers to work in parallel. Not only developers are saved from code conflicts, but they can review the changes that have happened within the code as well.
Moreover, since its the time of agile development where the rapidly changing requirements are more than welcome, it is best to have a version control system that gives developers the freedom to revert back to an old version of the code.
In case you are not clear on what exactly a version control system is, let’s backtrack a little to the basics before we dive into the useful commands.
Version Control System: Git allows a team of developers to add their code in parallel without causing any conflicts within the whole project. In addition, Git has features like branches and merges, to support parallel development.
‘Distributed’ Version Control System: Git has two identical repositories, one remote stored in a server and one local that is stored in the computers of the developers.
Basic Git Commands’ List
To set up the author name, email address, username, file format, and more – use the following command
git config --global user.email firstname.lastname@example.org
The ‘-global’ flag represents your email will be used for all local repositories. In case you need to use different emails for several repos then use the following:
git config --local user.email email@example.com
This command will create a new local repository. Moreover, the repository created will be in the current directory.
Alternatively, if you require a repository in a new directory, you have to specify the name like so:
git init [project name]
This is used to copy a repository if it is lying on the remote server.
git clone username@host:/path/to/repository
Similarly, to copy a local repository, use the command:
git clone /path/to/repository
Add files to the staging area using the following git command which will index the temp.txt file:
git add <temp.txt>
This command will create a snapshot of the changes made and save it to the git directory. However, the committed changes won’t reflect in the remote repo as well.
git commit –m “Message to go with the commit here”
Shows the overall status of changed files in a list with the files that are yet to be staged or committed.
This command is used to send local commits to the master branch of the remote repository. in case you need to push your code to another branch and not the master, then simply replace <master> in the following git command:
git push origin <master>
To view all remote repos (short of repositories), run the following command and it will list the connections with the URLs.
git remote –v
To connect the local repository to a remote server or to delete a connection to a specified remote repository – use the following commands, respectively.
git remote add origin <host-or-remoteURL>
git remote rm <name-of-the-repository>
This use of command creates branches and enable you to easily navigate between them.
command git checkout -b <branch-name>
Once the brand is created using the above command, you are automatically switched to it. Now, to switch between branches, you need to run the following
git checkout <branch-name>
A simple ‘git branch’ command will list all the branches present in the repository. Like so:
If you want to delete a branch, then use
git branch –d <branch-name>
This is used to merge a branch into an active branch.
git merge <branch-name>
This merges all the changes present in the remote repository on to your local working directory.
The index and the last working directory reset to the last git commit’s state, using this command.
git reset --hard HEAD
Using this command you can clean unnecessary files and optimize your local repo.
To view conflicts against the base file, run the following command.
git diff --base <file-name>
Similarly, to list down all the conflicts present, run:
git reset -hard
The following git command discards all history and reverts back to the specified commit.
git reset –hard [commit]
To view any git object, run this small command
Fetch all objects from the remote repo that are not yet available in the local directory.
git fetch origin
Browse your local repository in the git-web interface with this simple command
git instaweb –httpd=webrick
This deletes objects that have no incoming pointers. Simply run
Self-explanatory? This allows creating a zip or a tar file that contains the constituents of a single repository tree. For example:
git archive --format=tar master
When you need to apply certain changes from one branch to another, you use:
git rebase master
If you are a beginner, then these would definitely help you get started. Moreover, GitHub announced an amazing discount and perks into their new plan amid COVID-19, which would help you tremendously in learning full hands-on. You will get to experience how custom software development in Houston and around the globe takes place on distributed version control systems, such as GitHub.
We’ll be adding more to the list, but for the meantime, keep on practicing and you will eventually have it on your fingertips!