How to use GIT in Ubuntu ? (Part -2)

In the previous article, we learnt how to use basic GIT. In this article we would try to learn some more basic concepts like branching and merging.

 SOME MORE BASIC COMMANDS:

Some of the very helpful commands while using git are:



  • git

Type this in the terminal and you’ll see a bunch of commonly used GIT commands with their usage. To know about a command in detail, you can type in ‘git help <command>’ .har_5_1

  • git status

This is one of the most useful commands of GIT. If, at any point of time, you wish to know your current branch or changes in the files which aren’t staged for commit or untracked files, type in this command.

har_5_2

Here, the branch is master and everything is up-to-data. Let’s make some changes in the file and then use this command again.har_5_3har_5_4

There are changes which are not staged for commit in the file helloworld.c and we have the option of adding these changes in the final commit or discarding the changes in the final commit. We can add them with the git add . command (as discussed previously) and discard the changes in the file by the command git checkout — helloworld.c (NOTE: There is a space in between ‘–‘ and ‘helloworld.c’)

  • git log

With this command one can see the commit logs with the Date, Time, Name of the Author and commit notes. This command is very useful in a group of developers working on the same project.har_5_5

  • git add

As discussed previously, we must add the file(s) to the list while we would commit later. We can add the files with the help of git add . or git add helloworld.c But, let’s say we want to add only a specific group of files with similar extensions. Let’s say all the files ending with .txt . Then we can do it by git add *.txt command. This would add only the files which ends with .txt .

Now, if we want to ignore a specific group of files with similar extensions ( .txt, .cpp, etc) then we can also do it with git. For example, Let us consider we want to ignore the .c~ files that we can see (helloworld.c~). I have made another file named newfile.c so that there is another .c~ file which would be ignored. We need to follow two simple steps:

  1. We need to make a .gitignore file where we would store all the extensions to be ignored. We can make a such a file by typing touch .gitignore in the terminal and then open it with a text editor.
  2. Type in all the extensions of files which you want to ignore. Here, I want to ignore all the .c~ files and the.gitignore~ file.

har_5_6

We can see the file newfile.c~ by typing the command ls but when we check the status ( git status), we can see just two untracked files (.gitignore and newfile.c) . Note that there is no newfile.c~ file which means that it has been ignored successfully. We can now add the untracked files and then commit as we have been doing earlier.


har_5_7

 

BRANCHING & MERGING

Branching allows you to work on a copy of code in the mainline without actually effecting the main line directory. For example, Let’s say if you want to work on a new module of the project, then you can choose a new branch on which you’ll work. Everyone else can work on the same project by working on their branches without being affected by your work. When you’re done, then you can merge all your work back into the main branch. A few important commands used for branching and merging are :

  • git branch

This lists all the local branches of your account. If you have not created a branch, then it’s by default the master branch.

har_5_8

  • git branch NewBranchName This creates a new branch starting at the some point in history as the current branch. Note that it doesn’t make it the current working branch so we need to change it manually with the command git checkout NewBranchName

har_5_9

 

Now if we execute the command git status then it would also show the current branch to be newmodule. 

If on this branch we made some changes to the file aboutme.txt and then we want to merge the two files aboutme.txt (on branch: newmodule) and aboutme.txt (on branch: master). For merging, you must ensure that the current branch is your desination branch. Here, the desination branch would be the master branch. So, we would switch our current branch from newmodule to master by git checkout master and then merge them.


  •  git merge SourceBranchName

This would Merge the specified branch ( SourceBranchName) into the current branch and auto-commit the results.

Here, we made some changes in the aboutme.txt file in the branch: newmodule

har_5_10

 

 

Now, after staging and committing the changes in the newmodule branch, we changed out current branch to master by git checkout master and open aboutme.txt.

har_5_11

Note that this aboutme.txt file is not updated with the second line as in branch: newmodule

Now , we would use the command git merge newmodule to merge both the branches. Now, opening aboutme.txt , we see that the file is updated with the second line as well.

har_5_12

This would run pretty smooth but mostly we’re going to run into Merge Conflicts even in the simplest cases. You can imagine how complex this can get while working in real-life projects. But whenever we run into a merge conflict, we can launch a mergetool which can help fixing a conflict in a far more easier way. Meld is a mergetool available on the Ubuntu software center. You can also install it using command line on Ubuntu, Windows and Mac.

Article By Harshit Gupta: harshit
Kolkata based Harshit Gupta is an active blogger having keen interest in writing about current affairs, technical Blogs, stories, and personal life experiences. Besides passionate about writing, he also loves coding and dancing. Currently studying at IIEST, he is an active blog contributor at geeksforgeeks. You can reach him at https://in.linkedin.com/pub/harshit-gupta/102/b71/605

If you also wish to showcase your blog here,please see GBlog for guest blog writing on GeeksforGeeks.

 



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.