DEV Community

Cover image for Git basics

Posted on • Updated on

Git basics


Download git.
Use: git <command> [<args>]


Executing git init inside the project folder will create a new repository.


A copy of any repository can be created using git clone /path/to/repo for local and git clone username@host:/path/to/repo for remote.

How (g)it works

The working directoryproject/— holds all the files; to avoid complications, the staging areaproject/.git/index— could be seen as where the modified files are stored; after changes are commited, the repository or HEADproject/.git/objects— will be updated and ready to push.


Add & Commit

When a file is modified git add <filename> will place a file into the staging area, git add . can be used to add all files. This gets the modified files ready to commit.
To actually commit the changes git commit -m "Message" is to be used, now the file is commited to the HEAD but not to the remote repository yet.


git push origin <branch> will send the changes from the HEAD to the remote repository.
In order to push, the repository must be connected to a remote server, git remote add origin <server> will do the trick.


The master branch is the "default" branch when a repository is created. Other branches are use to develop features isolated from each other; Create-Develop-Merge.


To create and move to a branch named develop: git checkout -b develop; to switch back to master: git checkout master; delete the branch: git branch -d develop; push the branch to remote repository: git push origin <branch>.

Update & Merge

To update the local repository, git pull will fetch and merge remote changes; To merge a branch into the active branch: git merge <branch>.
When pulling/merging git tries to auto-merge changes, sometimes possible conflicts arise, in such cases a manual editing of the files in conflict need to be done; after changes are made, index the files using git add <filename>.


A visual git reference

Discussion (0)