DEV Community

Jessica Iwu
Jessica Iwu

Posted on

Getting started with Git? 10 commands that would save you…

Pushing, Pulling, fixing Merge issues among many others, used to be insurmountable problems for me because I was not familiar with git. I was always scared that I would ruin something along the way, and cause my code to break or just disappear completely. It took a while getting to understand how these processes work. After getting a grasp of some of it, I thought I should share the knowledge.

If you do not already know why Git is important, know this:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
~ https://git-scm.com

I’d talk about 10 Git commands that would help you work effectively.

1. git init

This is basically the starting point for you. You would need to choose a location on your system which would be used as your local Git repository. The 'init' command would create a new git repository for you. If you already have an existing project, 'init' would create a git repository in your working directory. On your terminal, cd into the directory path where your project is, and type git init.

  $ cd var/www/devProj
  $ git init

Enter fullscreen mode Exit fullscreen mode

2. git clone

If you are working in a team, and you need to work on an already existing repository, you might need to clone it so as to have a version on your local. Git clone simply replicates the content of an existing repository, and reproduces it in a new directory. On your terminal, cd into the the directory path where you would want the project to be, and clone it there.

  $ cd var/www/
  $ git clone path/to/the/repo

Enter fullscreen mode Exit fullscreen mode

3. git add

If you have been working on a branch, you would need to add the files you have been working on to your index before you can stage for a commit. You can add a specific file using git add or you can add all your new and modified files at once using git add . You can git add several times before committing, as long as new changes are being made.

    $ git add /name/of/file
    $ git add . //new and modified files

Enter fullscreen mode Exit fullscreen mode

4. git commit

After you have added all the changes, you can stage all of them for a commit. This updates the Head but not the remote repository. Every time you use git commit,what you are doing is to collate all the file changes you have earlier recorded using git add .,and making it into an object.

    $ git commit -m "write-a-commit-message"

      //You can actually add and commit at once using -

    $ git commit -am "write-a-commit-message"

Enter fullscreen mode Exit fullscreen mode

5. git branch

Git branch shows a list of all the branches available in a repository. It also shows what branch you are presently on. A branch is a portion of a project that is independent of all others.Your first commit is usually made to Git’s default branch,master. If there is a need to create a new branch, you can use git branch new-branch. This new branch would be independent of all other existing branches until it is merged into another branch.

      $ git branch branch-name
      $ git branch -D branch-name //This deletes the created branch

Enter fullscreen mode Exit fullscreen mode

6. git checkout

Git checkout is necessary when you want to leave a branch. Recall that git branch only creates a new branch, however, git checkout moves you to an already existing branch.

      $ git checkout branch-name //This switches you to another branch
      $ git checkout -b new-branch-name //This creates a branch and checks out to 
         it also.
Enter fullscreen mode Exit fullscreen mode

7. git status

This command tells you what branch you are currently on. It identifies all the changes that have been made, and all the changes that have not yet been added and committed to that branch.

      $ git status

Enter fullscreen mode Exit fullscreen mode

8. git log

It lists all the previous commits made on that branch, the author, the time and the date of commit. This helps you keep track your activities.

      $ git log

Enter fullscreen mode Exit fullscreen mode

9. git push

If there are several people working on a project,the several others would definitely need to work with the new changes you have just made on a branch. For them to access your changes, you would need to push your commits to the remote repository.This way, the remote version would be updated, and can be easily accessed by others.

       $ git push origin master 

Enter fullscreen mode Exit fullscreen mode

10. git pull

As a developer, you might need to pull from a couple of repositories.Pulling takes files from a remote source and merges them to your local files. That way, you would have access to the same file contents as the remote repository.

       $ git pull origin master

Enter fullscreen mode Exit fullscreen mode

There are a lot of other git commands, and usually, you would find them out when you exactly need them. Nevertheless, the above listed git commands would definitely be of help to you especially if you are just getting acquainted with git.

No one knows it all, so, if there are other commands you would like to share, please don’t hesitate to drop them as comments.
If you like the article, please like and share.

Top comments (9)

Collapse
 
katzy687 profile image
Natti Katz

these are good base commands to learn

you'll want to learn about git reset or git revert when something goes wrong.

also "git checkout ." is one I use occasionally when something new I'm trying just goes to shit

Collapse
 
belhassen07 profile image
Belhassen Chelbi

I added this post page to my favorites

Collapse
 
devansvd profile image
Devan

Well, git fetch, git stash ??

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber

Agreed, git stash is an absolute must have when you have irrelevant changes that force you to merge having no cvs integrated in IDE.

Collapse
 
vinopravin profile image
Pravinraj Mohan

git merge???

Collapse
 
jessicaiwu profile image
Jessica Iwu

git merge is really cool too..

Collapse
 
avallete profile image
avallete

git rebase -i HEAD HEAD~X

For rewriting some history before pushing is a must have for me.

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber

I personally never use git init. Is it wrong? I always initialise a repo from github with a readme then clone

Collapse
 
devansvd profile image
Devan

In some cases especially when you work offline.