The second post in the series gives a good understanding of the basic commands used in Git.
You can read Part One here.
In this part, we will take an in-depth look at how the basics commands of Git work.
In this story, we will have a nice journey through git config, git init, git add, git commit, git clone, git remote, git push and git status.
If you have a couple of minutes of your expensive time and you want to improve yourself take a seat and start reading and practicing with us, my dear friend.
But before jump into the commands of Git, here we have a little image of how to install Git on Ubuntu 18.
How to install Git.
Git configNow that you have Git on your system, you’ll want to do a few things to customize your Git environment.
You should have to do these things only once on any given computer; they’ll stick around between upgrades.
You can also change them at any time by running through the commands again.
Who you are?The first thing to do is to tell Git who you are, to introduce yourself, to know each other, because it’s a long journey, it’s measured in friends, not in miles.
$ git config –global user.
name "nawter"$ git config –global user.
comWhat is your editor?Now that Git knows who you are, you can configure the default text editor that will be used when Git needs you to type in a message.
If not configured, Git uses your system’s default editor.
If you want to use a different text editor, such as Vim, you can do the following:$ git config –global core.
editor vimBe careful with this step, if you don’t set up your editor like this, you get into a really confusing state when Git attempts to launch it.
An example on a Windows system may include a prematurely terminated Git operation during a Git initiated edit.
What are your settings?If you are not sure about your configuration settings, you can use git config –list a command to list all the settings Git can find at that point:$git config –listuser.
editor=vimGit initSo we hit this section and your first thoughts are what is my next move, I tell you that should be git init.
You’re writing a bit of code to create the new videogame that will change the world, or you’re playing around with some new data to cure cancer and you think git init.
Let’s start with the recipe to create your own project.
Create a new directory to contain the project.
Use of mkdir.
Go into the new directory.
Type git init.
git init sampleCheck the content of the .
git folderThe content of .
git folderWrite something, for example, a README.
md file, either as plain text or with Markdown content, talking about your project, and this is free a template to do it.
Type git add to add the files git add README.
mdType git commit -m “my first commit”Let’s check some data before we move to the next section first if we type git log we can see this information which indicates that head position and our last commit.
commit 7182a646f28f83767810ee751d50b9808bd62bf7 (HEAD -> master)Author: nawter <4772025+Nawter@users.
com>Date: Sun Apr 14 12:46:05 2019 +0100my first commitand last but not least if we run again tree .
gitand inspect the result ourselves we see that there are some changes in the number of directories and files, it changed from 9 directories and 15 files to 15 directories and 23 files.
git folder after changes.
Git remote & Git pushWhat is a remote repository?A remote URL is Git’s fancy way of saying the place where your code is stored.
That URL could be your repository on Github, or another user’s fork, or even on a completely different server.
Git associates a remote URL with a name, and your default remote is usually called origin .
Remote repositories can be on your local machine.
It is entirely possible that you can be working with a remote repository that is, in fact, on the same host you are.
The word remote does not necessarily imply that the repository is somewhere else on the network or Internet, only that it is elsewhere.
Working with such a remote repository would still involve all the standard pushing, pulling and fetching operations as with any other remote.
How to connect to your remote repository?You’ve now got a local Git repository.
You can use Git locally, like that, if you want.
But if you want the thing to have a home on Github, do the following.
Go to Github website.
Log in to your account.
Click the new repository button in the top-right.
You’ll have an option there to initialize the repository with a README.
Click the Create repository button.
Connecting to our remote repository.
Now, follow the second set of instructions, in which you can see …or push an existing repository from the command line.
$ git remote add origin https://github.
git$ git push -u origin masterAnd this is the result after we push our changes to the new repository in our terminal.
Counting objects: 3, done.
Writing objects: 100% (3/3), 227 bytes | 227.
00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)To https://github.
git * [new branch] master -> masterBranch ‘master’ set up to track remote branch ‘master’ from ‘origin’.
And the result in our web browser.
Result in the Github site.
What you can see inside a remote?If you want to see more information about a particular remote, you can use them git remote show <remote> command.
If you run this command with a particular short-name, such as origin, you get something like this:$ git remote show origin* remote origin Fetch URL: https://github.
git Push URL: https://github.
git HEAD branch: master Remote branch: master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up-to-date)Git clone vs Git initThe commandsgit init and git clone can be easily confused.
At a high level, they can both be used to initialize a new git repository.
However, git clone is dependent on git init.
git clone is used to create a copy of an existing repository.
Internally, git clone first calls git init to create a new repository then calls git remote add after that, it then fetches all branches from that URL to your local repository using git fetchand finally checks out a new set of working files with git checkout.
Git statusIn this part of the tutorial, we will explore different real scenarios using the command git statusWrong place$ git statusfatal: not a git repository (or any of the parent directories): .
gitIf we get this error, we are not in a directory that has a Git repository.
Use cd command in our terminal or refer to the Git setup docs regarding setting up your initial repo.
Everything clean$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working directory cleanThis is the ideal Git status message.
Being up-to-date with 'origin/master'.
means there is nothing to push.
working directory clean means all the files in the current directory are being managed by Git and the most recent version of the file has been committed.
Untracked filesIf git statusmentions Untracked files, we may need to add one or more untracked files.
$ git statusOn branch masterNo commits yetUntracked files: (use "git add <file>.
" to include in what will be committed)README.
mdnothing added to commit but untracked files present (use "git add" to track)In this case, we probably want to add README.
A good rule is that if we edit the file directly with our editor, the file belongs in it.
To commit or not to commitIf you see the message Changes to be committed: …, it means we have previously run git add, but we have not run git commit$ git statusOn branch masterNo commits yetChanges to be committed: (use "git rm –cached <file>.
" to unstage)new file: README.
mdFix this by running git commit with a short message saying what we changed in the files.
$ git commit -m "my first commit" [master (root-commit) 7182a64] my first commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 README.
mdAfter the fixing of the commit.
$ git statusOn branch masternothing to commit, working tree cleanThanks for reading.
If you loved this article, feel free to clap for it and hit that follow button so we can stay in touch.