DEV Community

Nesha Zoric
Nesha Zoric

Posted on

Git Commands Ultimate Tutorial [Part 2]

In the last Git Commands article, you have learned how to use Git options and what they can do. Today, we will extend our reach and discover everything there is to know about commonly used Git Commands.

Git Commands

Git commands are used to access the Git working directory and connect it to our remote repository, by making changes, viewing different files, and many other possibilities! Once your project is set, be sure to connect it with Git.

With the following commands and short descriptions, we will make it sure that you are ready to use Git whenever you need it. For a detailed description of all, the GIT commands please check git's official description.

$git config
You can use it to configure the author's name, email address, file formats and many more to be used with your commits.

git config --global user.name "Kolosek
git config --global user.email "kolosek@example.com"
Enter fullscreen mode Exit fullscreen mode

$git init
Using this command you make it sure that your git repository is initialized and creates the initial .git directory in a new or in an existing project. The output will be the following:

Initialized empty Git repository in /path/.git/
Enter fullscreen mode Exit fullscreen mode

You can undo a $git init with rm -rf .git.

$git clone <path>
This creates a working copy of a Git repository from a remote source to your local repository. This is the first command you want to use when you are cloning a Git repository.

git clone /path/repository
Enter fullscreen mode Exit fullscreen mode

Also, you can add the original location as a remote so you can easily fetch from it again and push it if you have permissions. Once the project has been cloned you can start working on it. Write your RSpec tests!

git clone git@github:user/repository.git
Enter fullscreen mode Exit fullscreen mode

You can clone one specific branch at a time: git clone -b <branch_name><repository_url>:

git clone -b branch_name git@github:user/repository.git
Enter fullscreen mode Exit fullscreen mode

$git add <file_name>
Add one or more files in your working directory to your index.

$git commit
Take all your changes written in the index to the HEAD branch with a -m message.

git commit -m "Commit these changes."
Enter fullscreen mode Exit fullscreen mode

You can also commit any files you've added to git add, and also commit any files you've changed since then:

git commit -a
Enter fullscreen mode Exit fullscreen mode

Always commit all your changes even if its only a bunch of Capybara tests!

$git status
It shows you the status difference between an index and working directory files. Lists the files you've changed, untracked because they are only in your working directory and staged since they are ready to be committed.

On branch master
Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    File_name

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

$git remote
Shows all the remote versions of your repository.

$git checkout <branch_name>: You can switch from an existing branch to another one or create a new branch and switch to it git checkout -b <branch_name>.

$git branch
With this, you can simply list all existing branches, including remote branches by using -a or create a new branch if a branch name is provided.

$git push
Pushes all changes to the remote repository.

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

You can also delete a branch from your remote repository:

git push origin :<branch_name>
Enter fullscreen mode Exit fullscreen mode

$git pull
Fetch and merge your changes in the remote repository to your working directory.

$git merge <branch_name>
Merges one or more branches into your active branch and if there are no conflicts it will automatically create a new commit.

In Kolosek, we commit all our changes to Git and make it sure to notify our team when they are merged with the rest of the application! Try to create your own application using Rails Associations.

$git diff
Show changes between your working tree and the index, between two branches, or changes between two files on disk. Example between branches:

git diff <source_branch> <target_branch>
Enter fullscreen mode Exit fullscreen mode

$git reset
Reset your index and working directory to the state of your last commit.

git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

git reset --hard will also undo the changes you made so far! Use git reset --soft if you want to keep your changes.

$git revert
Revert works in a very similar way to $git reset, but instead of resetting it will create a new commit that reverses everything introduced by the accidental commit.

$git tag
You can use tagging to mark a significant change you made, such as a release.

git tag 1.0.0 <commit_id>
Enter fullscreen mode Exit fullscreen mode

Always tag your new production releases. Try it out by implementing CarrierWave and adding a release tag to it!

$git log
It shows a listing of commits on a branch with corresponding details.

commit 134808af7c596be8d92c619f9efb94542874e1e3
Author: Kolosek <kolosek@example.com>
Date:   Fri Mar 23 14:24:54 2018 +0100

    [#1] First Commit
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this, we have covered everything there is to know about Git commands. Don't forget, these are only the very basic and most commonly used commands. Hope this helped you to get started with Git!

This post is originally published on Kolosek Blog.

Top comments (5)

Collapse
 
pixelbrackets profile image
Dan • Edited

A good command, which I should have learned when starting with Git, is the partial commit using git add -p <file>. This lets you decide which part of a file you want to add to the next commit. The following dialog lets you decide what to do with the given chunks: y yes - add it, n no - dont add it, s split the current part into smaller parts.
Very handy at the beginning when you change a lot of stuff, and need to create proper commit messages afterwards.

PS: I published a Git Cheat Sheet with the most common Git commands → pixelbrackets.github.io/git_cheat_...

Collapse
 
philsinsight profile image
Mr. Boateng

yep literally only learnt this the other day, so useful 👌🏾

Collapse
 
havarem profile image
André Jacques

You could talk about the git commit -S that uses GPG key to sign a commit. I just set it up this years and it looks great on GitHub !

Collapse
 
shreyasminocha profile image
Shreyas Minocha • Edited

You could also do git config --global commit.gpgsign 'true' to make this persistent. You'll need to set user.signingkey too, though.

Collapse
 
neshaz profile image
Nesha Zoric

That's really interesting! I will definitely check it out.