DEV Community

Cover image for Git Basics: The Final Guide
ZigRazor
ZigRazor

Posted on • Originally published at dev.to

Git Basics: The Final Guide

Hot to Create a new repository

create a new directory, open it and perform a

git init
Enter fullscreen mode Exit fullscreen mode

to create a new git repository.

How to checkout a repository

create a working copy of a local repository by running the command

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

if you want create a working copy of a remote repository

git clone username@host:/path/to/repository
Enter fullscreen mode Exit fullscreen mode

Workflow

your local repository consists of three "trees" maintained by git. the first one is your Working Directory which holds the actual files. the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you've made.

Add & Commit

You can propose changes (add it to the Index) using

git add <filename>
Enter fullscreen mode Exit fullscreen mode

or

git add *
Enter fullscreen mode Exit fullscreen mode

This is the first step in the basic git workflow. To actually commit these changes use

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

Now the file is committed to the HEAD, but not in your remote repository yet.

Pushing changes

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute

git push origin master
Enter fullscreen mode Exit fullscreen mode

Change master to whatever branch you want to push your changes to.

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with

git remote add origin <server>
Enter fullscreen mode Exit fullscreen mode

Now you are able to push your changes to the selected remote server

Branching

Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

create a new branch named "branch_x" and switch to it using

git checkout -b branch_x
Enter fullscreen mode Exit fullscreen mode

switch back to master

git checkout master
Enter fullscreen mode Exit fullscreen mode

and delete the branch again

git branch -d branch_x
Enter fullscreen mode Exit fullscreen mode

a branch is not available to others unless you push the branch to your remote repository

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

Update & Merge

to update your local repository to the newest commit, execute

git pull
Enter fullscreen mode Exit fullscreen mode

in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with

git add <filename>
Enter fullscreen mode Exit fullscreen mode

before merging changes, you can also preview them by using

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

Tagging

it's recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 0.0.1 by executing

git tag 0.0.1 1b2e1d63ff
Enter fullscreen mode Exit fullscreen mode

the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id by looking at the...

Log

in its simplest form, you can study repository history using..

git log
Enter fullscreen mode Exit fullscreen mode

You can add a lot of parameters to make the log look like what you want. To see only the commits of a certain author:

git log --author=bob
Enter fullscreen mode Exit fullscreen mode

To see a very compressed log where each commit is one line:

git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode

Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches:

git log --graph --oneline --decorate --all
Enter fullscreen mode Exit fullscreen mode

See only which files have changed:

git log --name-status
Enter fullscreen mode Exit fullscreen mode

These are just a few of the possible parameters you can use. For more, see

git log --help
Enter fullscreen mode Exit fullscreen mode

Replace local changes

In case you did something wrong, which for sure never happens ;), you can replace local changes using the command

git checkout -- <filename>
Enter fullscreen mode Exit fullscreen mode

this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.

If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this

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

Others Useful Command

built-in git GUI

gitk
Enter fullscreen mode Exit fullscreen mode

use colorful git output

git config color.ui true
Enter fullscreen mode Exit fullscreen mode

show log on just one line per commit

git config format.pretty oneline
Enter fullscreen mode Exit fullscreen mode

use interactive adding

git add -i
Enter fullscreen mode Exit fullscreen mode

For More "The Final Guide" see the Index Page

Discussion (3)

Collapse
simpleadam profile image
simpleAdam • Edited

"Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. "

Except it won't be on GitHub, as they don't like the word master. It'll be the Main branch. just to add to the confusion

Collapse
zigrazor profile image
ZigRazor Author

True, in new repository the "Master" branch has become "Main".

Collapse
wambuimethu profile image
wambuimethu

Thank God I can now git comfortably even during the wee hours of the night 😐