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 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.
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
- 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:
- 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.
- 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.
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.
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.
- 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
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
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.
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.
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:
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.