DEV Community

Ezhil Arasan
Ezhil Arasan

Posted on

git commands

Git status
Status shows, well, the status of your working tree. Specifically, you'll be able to see your current branch, tracked and untracked files, as well as staged and unstaged changes:

So, in the example above we can see that the current branch is master. The main.rb file is already tracked by Git and it has some changes that are not yet staged for commit (we will see how to achieve that a bit later).

Git add
add is probably one of the most common Git commands. So, what operation does it perform? Basically, it updates the index with the content found in your working tree thus making the content staged for the next commit. An index is like a snapshot of your working tree. When you perform a commit, this snapshot is utilized. Therefore, before doing a commit you should perform git add for any files that were modified or created.

In the simplest case you can say:

git add .
Enter fullscreen mode Exit fullscreen mode

This dot . means "take all the files in the working tree" and add them to the index.

However, you can also add individual files and folders as well as use fileglobs

git add single_file.txt
git add my_folder
git add *.rb
Enter fullscreen mode Exit fullscreen mode

So, to summarize: after you have performed some changes in your project, you will need to run git add before doing a commit.

Git commit
commit is one of the most important Git commands as it records the changes to the repository based on the current index (the snapshot that you've created). To perform a new commit and add a small informational message for it, run:

git commit -m "My first commit"
Enter fullscreen mode Exit fullscreen mode

Now all the staged files will be commited and added to the project history. Also, you can use the -a option to automatically stage and then commit all the files that have been modified and deleted:

git commit -am "My first commit"
Enter fullscreen mode Exit fullscreen mode

Unfortunately, this option will ignore untracked files, therefore you will still have to say git add . for these new files.

Git push/pull
Push Git command allows you to send local changes to the remote repository which is usually hosted on services like GitHub, Bitbucket, GitLab, or Azure DevOps repos.

First of all, you will need to actually add information about this remote repository by running something like:

git remote add origin https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

Usually this command will be supplied to you by the service you're using. What it does? It creates something like a "pointer" to a remote repository that is stored on github.com. This remote repository has an alias origin but you can call it anything you'd like (for example, just github).

Now, when the remote repository is provided, you can push your local changes:
git push origin master

git push origin master
Enter fullscreen mode Exit fullscreen mode

In this example we're taking the contents of the master branch and push it to the remote repository which has an alias origin.

You can also set origin as "upstream" by saying:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Now you don't need to provide origin anymore. Just say

git push
Enter fullscreen mode Exit fullscreen mode

However, please note that it will work only for the master branch which was mentioned when creating an upstream โ€” for all other branches you will still have to say:

git push origin another_branch
Enter fullscreen mode Exit fullscreen mode

To grab changes from the remote repository and apply them locally, use:

git pull origin master
Enter fullscreen mode Exit fullscreen mode

This command works in the same way as git push. If you have specified an upstream, you can simply say:

git pull
Enter fullscreen mode Exit fullscreen mode

Please note that merge conflicts might occur and you will need to resolve them manually .

Git clean
Removes untracked files from the working directory. This is the logical counterpart to git reset, which (typically) only operates on tracked files.

Git clone
Creates a copy of an existing Git repository. Cloning is the most common way for developers to obtain a working copy of a central repository.

Git config
A convenient way to set configuration options for your Git installation. Youโ€™ll typically only need to use this immediately after installing Git on a new development machine.

Git merge
A powerful way to integrate changes from divergent branches. After forking the project history with git branch, git merge lets you put it back together again.

Git checkout
In addition to checking out old commits and old file revisions, git checkout is also the means to navigate existing branches. Combined with the basic Git commands, itโ€™s a way to work on a particular line of development.

Git reset
Undoes changes to files in the working directory. Resetting lets you clean up or completely remove changes that have not been pushed to a public repository.

Refrence
https://lokalise.com/blog/10-git-commands-for-day-to-day-work/
https://www.atlassian.com/git/glossary#terminology

Top comments (0)