Git for Beginners – Basic Commands, Remote Collaboration, Branch

SatheeshProgrammingLeave a Comment

Git Image

Git is one of the most popular source control systems that is very popular. I am very new to git and I thought, someone like me, who is very new might find this helpful. It is truly for beginners. It doesn’t matter, if you are using Windows, Mac or Linux. The commands are same

Git Init : This command is to initialize git in your local system. You type the below command

Git init

You get info like “Initialized empty Git repository in /Users/xx/xx/.git/ “, if you are on Mac

All the info the changes are tracked in .git hidden folder. If you delete this folder, everything is gone from change tracking side.

Git Clone :

First you create a repository in GitHub or BitBucket and then first thing you would do is to clone that repository using the below command.

Git clone <Github or the repository URL >

Eg: git clone [email protected]:abc/learngit.git

Git Status : This will give you the status of the repository, if something has changed. Like if other users have created anything or anything is new. Below command will tell, if there are any untracked files

Git status

You will get “Your branch is up to date with ‘origin/master’.” if no new files or changes. If there is any new filed added by others or you in the folder, it will say like below and show the file.

Untracked files:

(use “git add <file>…” to include in what will be committed)

index.html

Git Add  : You will use this command to add a new file to the repository after you have created it locally. You type  the below command.

Git add <filename that you want to add to repository”. Now if you do  git status command, you will get below.

On branch master

Your branch is up to date with ‘origin/master’.

Changes to be committed:

(use “git reset HEAD <file>…” to unstage)

new file:   index.html

If you have multiple files in the folder and you want to add all of them then you do the command

git add .  ( basically it is a dot at the end ) or

git add -A

If you want to stage a set of files of same type,  for instance, .html files, then you can do git add *.html. Basically the command is

Git *.<file extension>

Consider the above command of add as like staging command before you commit.

Let’s say you do any changes to the file that you just added after this command, they will not be part of the commit to the repository. You need to add the file again to include any new changes you do after using git add command.

Git .ignore : In some cases like Log files, you do not want to track such types of files in source control, then you can use .gitignore file to put in those files.  You can use the command below to create such file or create manually.

Touch .gitignore

Now, you can type the file names or extensions names that need not be tracked.

E.g: *.log ,if you want git to ignore all the files that start with .log extenion.

.git

But, one thing to do is that you need to add the .gitignore file to the git using  git add .gitignore so that it is staged and then you can use below commit command.

Git commit : This is used to commit the file that you just added above using the Git Add. Basically, it is committing your changes. You use the below command. The text followed by -m is the comment or message for your comment. This has to be in double quotes.

Git commit -m “<added filename or the comment you want to make about commit >”

You will get message like below as show in example

E.g: git commit -m “added new file index.html”

[master f8b4f92] added new file index.html

1 file changed, 2 insertions(+)

create mode 100644 index.html

Now, the changes done are still on your local machine they are not uploaded to the server yet. Next command is that

Also, another variant is that if you do not use -m at the end and say just git commit.

You will get a VIM like command interface to enter message. You need to press i to insert text.  Once you are done entering message, then you press ESC key. At the end you need to press type :wq to exit the screen.

Git Log :  This command will tell the log of the all the commits done to the repository.

Git Push :  You use this command to push the code that is committed to the server repository. You just type below command after commit.

Git push

Below is an example syncing up to the git repository in bitbucket.org, it does not matter, it is same for github or any of the git supported repos.

E.g: git push

Counting objects: 3, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To bitbucket.org:abc/learngit.git

e2dca..f8b4f92  master -> master

Now, you go to the server and check, you can see the new file there.

Git Pull :  This command is to pull the latest files from the repository, basically, all the new updates will be pulled from the repo to your local folder. Command is git pull

Git Pull

If the folder on your desktop is up to date, then you will get message “Already up to date. “

Branching and Merging :

Branching and merging are the most easy and key things to do, when you have a distributed team and you work a specific tasks or features/bugfixes.  Branching allows you to work on a copy of the code in the mainline, without impacting the main codeline.  When you are done you will merge these changes.

Create Branch : You use the below code to create a new branch for yourself by specifying the name

Git branch <Branch Name >

Check out Branch : After you create the branch, then you check out using the below command

Git checkout <Branch Name>

You need to specify the right branch name that will be checked out under your ID.

Going to Main Branch :  The main branch is called Master. You need to be in Main Branch

Git checkout master

To merge changes : Before we merge we need to ensure we are the destination branch, in our case, as we are planning to merge to main, it is our destination branch. You can check the status by using

Git status

This will give you info on the branch you are in like “ On branch master”

To merge the changes, all you need to do is type  below

Git merge <Branch Name>

The Branch Name you entered above should be your source branch or the one we want to merge.

TIp : You can use git commit -a -m  ‘Details of change” to skip a step

Merge with Conflicts :

Git merge

Git remote  :  You use the command git remote to show the remote URL alias that you are in, if it shows blank there is nothing. By default the alias would be origin.

Git remote -v

If you use the above command with -v, it will show the alias of the whole remote URL, by default it is origin. You can use the alias origin to update the repository instead of using the whole URL every time.

Git Fetch : using the below command will get all changes done, but will not merge with the local copy and you need to do it manually.

Git fetch origin

Git Pull  : using the below command will fetch all the changes since the last commit and then merge with the local copy of the code. It will do it from remote branch into your current branch.

Git pull origin.

Git push :  You use the below command to push the local repo to the main master.

Git push origin master

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.