GIT – The Introductory Overview
GIT is tool which plays a very vital role in Software Development Life Cycle. A software developer use to change their file many times. So if more developer will work then more number of times code will have the updates. GIT helps to handle the modification of code by providing a revision number on each commit. That revision number we name as HEAD revision. It means that is the updated file version. GIT has its own repository system. It stores all types of file used during the software development life cycle. GIT uses branching and merging strategy for handling frequent modifications at same time. Let’s checkout GIT frequently used commands.
Below are the basic steps a developer do while moving the updated file to GIT repository.
a) Developer update the file in the working directory or branch.
b) Now modified files will be in the staging area queue and are ready to commit.
c) Developer will commit and push the files. GIT repository will have the latest changes now.
d) Take the latest file from GIT repository.
pictorial representation of work flow in git
Frequent Use GIT Commands:
1) $ git config –global user.name “[name]”
It sets the name what you want to put into your commit transactions.
Example: git config –global user.name “yourname”
2) $ git config –global user.email “[email_address]”
It sets the email what you want to put into your commit transactions.
Example: git config –global user.email “yourname@company.com”
3) $ git branch
It lists all the local branches present in your current repository. It’s easy to identify the current branch by seeing the asterisk symbol. It means that is your current branch.
4) $ git branch [branch_name]
It creates a new branch.
Example: git branch newbranch
5) $ git branch –delete [branch_name]
or
$ git branch -d [branch_name] # Alias version
It deletes the specified branch in your local. Be careful while performing this task.
Example: git branch –delete develop
Example: git branch -d develop
6) $ git branch –delete –force [branch_name]
or
$ git branch -D [branch_name] # Alias version
It deletes the specified branch in your local, irrespective of merge conflicts. Its a force action to delete without any check.
Example: git branch –delete –force develop
Example: git branch -D develop
7) $ git checkout [branch_name]
It checkout a different branch in your local. This branch will have latest index, working tree and HEAD.
Example: git checkout develop
8) $ git status
It shows you the status of files in the index versus the working directory. It will list out files that are untracked, modified and staged.
untracked : File is present only in your working directory.
modified : File is tracked but not yet updated in your index.
staged : File is added to your index and ready for committing.
9) $ git diff
It checks the file differences which is not yet staged.
10) $ git diff [first_branch_name]..[second_branch_name]
It shows the content differences between two respective branches.
11) $ git merge
It merges one or more branches into your current branch. After that it will create a new commit if there are no conflicts.
Example: git merge newbranchversion
12) $ git stash
It’s very often when you’ve been working on some part of your project. Things are in a messy state and you want to switch branch for a bit to work. The problem is, you don’t want to do a commit of incomplete work. Then we use git stash command in this condition. It will save changes that you don’t want to commit immediately. It is temporary saved condition.
13) $ git stash apply
It put back the unsaved changes to your local, which you had stashed earlier.
14) $ git stash clear
It removes all the stashed file states. You can’t able to get it back again. Be careful while performing this task.
15) $ git stash list
It lists all the stashed files or changes.
16) $ git stash pop
It restores the most recently stashed files or changes.
17) $ git stash drop
It discards the most recently stashed files or changes.
18) $ git reset –hard [branch_name]
It resets your index and working directory to the state of your latest commit. You will get latest head revision of your repository. Changes will effect only in your local branch.
Example: git reset –hard origin/develop
19) $ git pull
It fetches the files from remote branch and merges to your local branch. This command is equal to the git fetch and the git merge sequence.
20) $ git pull –all
It fetches the files from all the remote repository and merges it with your local branch files.
21) $ git fetch
It fetches all the objects from the remote repository and branches that are not present in the local.
22) $ git fetch –prune
It fetches all the objects from the remote repository and branches that are not present in the local. After that it will remove all references that no longer exist on the remote. It is the best utility for cleaning outdated branches on remote. It is not applicable for local branch.
23) $ git add [filename]
It adds a single file to staged and make ready to commit.
24) $ git add –all
or
$ git add -A # Alias version
It adds all the modified files to staged and make ready to commit.
25) $ git add .
It adds all the modified file present at current location.
26) $ git commit -m “[GIT Commit Message]”
It commits the file to repository with specified message.
27) $ git push
It pushes all the committed file to repository. The branch will be set to new head revision. It is the mandatory task performed after git commit.
28) $ git push –prune
It pushes all the committed file to repository. After that it will remove all references that no longer exist on the remote.
29) $ git push [remote_branch_name] –delete [branch_to_be_deleted]
or
$ git push [remote_branch_name] :[branch_to_be_deleted]
It deletes the specified remote branch. Be careful while performing this task. The branch will get deleted from repository.
Example: git push origin –delete develop
Example: git push origin :develop
30) $ git prune
It removes the objects that are no longer pointing to any reachable branch.
31) $ git gc
It works like garbage collector for your repository. It Optimizes your repository by flushing the unused objects.
32) $ git blame [file_name]
It is use to examine the contents of a file line by line. It checks when that line was last modified and who was the author of that modifications.
Example: git blame ABC.java
33) $ git log
It lists the version history for the current branch. You can able to see all the commits happens to this branch.
34) $ git log –follow [file_name]
It lists the version history for the current branch and the specified file. It shows result for single file at a time.
35) $ git show [commit_id]
It gives the commit information about the specific commit id.
Example: git show 2a2c71434f81784dgfgfgd76jhjh87h
36) $ git rm [file_name]
It deletes the file from the working directory and from the index. It stages the deletion also and you see by using command git status.
Example: git rm ABC.java
37) $ git rm –cached [file_name]
It deletes the file from the index. Though, it will be available in the local working branch. It is like reverse of git add command.
Example: git rm ABC.java
38) $ git mv [old_file_name] [new_file_name]
It renames the file name.
Example: git mv ABC.java XYZ.java
39) $ git mv [old_file_directory] [new_file_directory]
It changes the file location.
Example: git mv ABC.java test/XYZ.java
40) $ git clone [url]
It downloads a project and its entire version history. Open Git Bash and change the current working directory to the location where you want to create the local copy of the repository. Then after use this command.
Example: git clone https://gitlab.abc.com/excom/ecommerce_test.git
Shanice
Hey There. I found your blog using msn. This is a really well written article. I’ll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will definitely return.
Dorolice Sherm Dorkus
Somebody necessarily lend a hand to make significantly articles I would state. This is the first time I frequented your web page and so far? I surprised with the analysis you made to make this particular post amazing. Magnificent task!
Olivia Kristo Flss
Hi there. I found your website by way of Google even as looking for a similar topic, your site came up. It appears to be good. I have bookmarked it in my google bookmarks to visit then.
Shiv Priya
I think the admin of this web site is truly working hard in favor of his web page, for the reason that here every stuff is quality based material.
Lucretia Carleton Kristel
Way cool! Some very valid points! I appreciate you penning this write-up and also the rest of the site is also really good.
Cassondra Elisha Abelard
Excellent points altogether, you simply won a new reader.
Othilia Jere Timi
This is my first time go to see at here and I am really happy to read all at single place.