DEV Community

Shafeeza
Shafeeza

Posted on

Git Commands I Use Regularly

I work with awesome team-mates. In our team, there are three developers, myself included. To collaborate we use Git. When I first started to use Git, I had uncertainties.

Firstly, I did not fully grasp the concepts and flow of Git. Though I still used to make changes, create features, and merge my changes with the master branch on the remote repository, I always had an uncertain and hesitant feeling. I am sort of a visual person; when it comes to processes, I like to have a clear picture in my head. Hence, I decided to learn more about Git.

I followed some of the lessons from two of Kevin Skoglund courses on LinkedIn Learning. I enjoyed watching those lessons and I definitely recommend them. He clearly described the process and gave clear instructions. The two courses are:

After watching those tutorials, I was able to more clearly visualize what was happening when I was pushing changes. Here are Git commands I use on a regular basis.

Git Clone

If you want to create a copy of the remote repository on your system, instead of downloading the remote repository, you can clone it.

  1. Open a terminal and navigate (cd) into a directory of your choice.

  2. Go to your Github repository. Click on the 'Clone or download' button. There you will see the https web URL. Copy it.

  3. Go back to the terminal and enter the following command:

git clone https_url_of_github_repository

https_url_of_github_repository is the https web URL you had copied in step 2.

Status

Checking the status let you see changes that were not yet committed.

git status

Save Changes in the Local Repository

Once you have made changes in your project, you can save and briefly describe the changes that were made.

Add changes

  • Add all of the changes
        git add .
  • Add changes from specific files
        git add file_name

The file_name includes the file extension. If you wish to add changes from more than one file, use a space between the file names to separate them.

Save the Changes

git commit -m "headline
description (optional)"

The description is optional.

Compare Changes with the Origin

git diff master origin/master

If you are unclear about the difference between the origin and the working branch, I highly recommend Kevin Skoglund's tutorials. It is from watching his tutorials that I gained a clear picture of the difference.

Create a New Branch

When I am about to create a new feature, I usually create a new branch. Command for creating a new branch:

git checkout -b name_of_branch

Delete a Branch

After your feature has been created and merged into the master branch on the remote repository on Github, you may want to delete the branch.

git branch -d name_of_branch

View all Branches

To view a list of all of the branches in the local repository, use this command:

git branch

Go to a Specific Branch

If you want to start working in a specific branch, use the git checkout command.

git checkout name_of_branch

Push Changes to the Remote Repository on Github

To push local changes to the remote repository on Github, use the following command.

git push -u origin name_of_branch

On the Remote Repository, Merge a Branch with the Master Branch on Github

On the remote repository, when I have to update the master with the latest changes from a branch on Github, I use the Github merge pull request feature to merge the branch into the master branch.

Delete a Remote Branch from Terminal

git push origin --delete name_of_branch

You can also delete the branch directly on Github.

Keep Branch Up-to-date

Before you start working or attempt to make any push, it is highly recommended to always update your remote branch and local repository. To keep a branch on the remote repository updated with the latest changes on the master branch, I use the pull request feature on Github. To keep the local repository up-to-date, I use either a combination of Git fetch and Git merge or Git pull. I personally prefer to use the Git fetch and the Git merge commands.

Git Fetch and Merge

git fetch
git merge

Git Pull

git pull

Top comments (0)