Git Basic Commands


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Git is one of the most popular version control systems out there. Below you’ll find the basic commands you need to get started with it.

Note: One important aspect to understand about Git is that it thinks about data as snapshots of files over time. Every commit will generate a new snapshot, that is independent of previous ones. This is very different from the approach used by most other version control systems, where data is thought as files and changes to those files over time.

1. Installing Git on Linux

sudo apt-get install git-all

2. Creating a Git Repository

There are two ways to create a new Git repository. You can either use an existing folder/project, or clone one from a remote server. If you are using an existing folder, go inside it and type

git init

If you want to clone a repository:

git clone https://github.com/project/

3. Getting information about the repository

git status

4. Tracking a file

If you create a new file, it won’t be tracked by Git immediately. You need to use the ‘add’ command to do so.

git add file.c

5. Staging files

Once you change files, Git will know. But that doesn’t mean it will automatically use those modified versions on the next commit. If you want to do so, you need to ‘stage’ the modified file. You do so using the same ‘add’ command.

git add file.c

6. Committing Changes

git commit

or if you want to add a message to the commit:

git commit -m "bug fix 1"

or if you want to include even files that weren’t staged with the ‘add’ command:

git commit -a

7. Removing files

git rm file.c

8. View changes over time

git log

9. Getting a remote project

git fetch [remote-project]

or if you want to get and merge:

git pull [remote-project]

10. Pushing your project to remove server

git push [remote-project] [branch]

usually this will look like

git push origin master

Because when you clone a project Git automatically calls the remote project ‘origin’.

Small Teams Working on the Same Git Project
– No one pushes to master.
– For every feature (or update on a feature), create new branch.
– Send pull request for reviewing (one of us is the reviewer).
– Merge pull request into master.

Leave a Reply

Your email address will not be published. Required fields are marked *