Apart from knowing programming languages and IDE’s, a developer should also have knowledge of VCS to manage his code effectively.
In this article, we will be specifically targeting Git on using it and understand various git commands visually.
Git is Distributed Version Control System(VCS). Now let’s try to break down the term “Distributed Version Control System”.
Version Control System or VCS is the system which stores history(versions) of files over time and
Distributed means that every local git repository of your project can act as the remote repository for others to do pull or push.
Let us understand some git commands which are used to move the files from working area to staging area until committing and pushing the files to the remote repo and at the end of the article there is flow diagram to understand and remember git commands visually.
I have categorized git commands into four sections based on functionality they provide:-
- Initializing repository.
- Getting repository info.
- Git operations.
- Getting operations Info.
Initializing Git repository
initializes current directory as git repo(local) which means that every change in the directory will be versioned by git.
When to use – If you do not have remote repo than use git init otherwise use git clone.
usage – git init
In the screenshot below, directory Geek As Service is initialized as the git repo.
Clones/Downloads remote repository hence local intialized.
usage – git clone https://www.github.com/geek-as-service/git-commands
Getting repository info
git remote -vShows list of remote URLs for your local repository.
Now, If you had used git clone for creating local repo, then remote URL will be present in the output.
but, if you had used git init then no remote URL will be listed. git remote add command can be used to add remote URL to local repo.
usage – git remote -v
In the screenshot below, the output shows remote URLs with name “origin” for pushing and pulling the changes.
git status shows the status of local repo. It lists newly created(untracked files), files with changes(modified files) and changes ready to committed(files in the staging area). It also lists unmerged files if there was conflict.Some terms which will be used more frequentlyUntracked Files – Files which are not tracked by git.
Tracked Files – Files which are being tracked by git for changes. If the change is found in the file then it will show the file as modified file but if there is no change in the file since the last commit then the file will not be listed in the output of git status.
Staged File – Tracked files which have some changes and these changes have been approved using git add and can be committed using git commit.
usage – git status
git status –short –branch
In the screenshot below, git status command was executed with two flags “short” and “branch” which shows the status in a single line for each file. As you can see, “first file” is in staging area while “second file” is modified file and “third file” is untracked file.
Moving on to next set of commands which allows us to do the following operations
- add files.
- commit changes.
- pull new changes from a remote repo to the local repo.
- push local repo to a remote repository.
- add remote URL to local repo.
git add will move the modified file to the staging/indexing area. git add is also used to start tracking untracked files.
usage – git add PATH_OF_FILE
In the screenshot below, git add moves “first file” to the staging area.
Commit’s the indexed/staged changes to local repo.
usage – git commit
git commit -m “SINGLE LINE MESSAGE”
In the screenshot below, “first file” was in staging area but after running git commit it has been committed.
git pullAfter committing changes, you need to synchronize your local repo with remote repo. To do that you pull all the new changes made by others in remote repo using above command.
usage – git pull
git pull origin master
git pull origin master command pulls changes from remote URL “origin” and branch “master”.
git pushPushes your local repo to remote repo, and hence your changes become available for others.
usage – git push
git push origin master
git remote add NAME_OF_REMOTE REMOTE_URLThis command is used to add remote URL to local repo.
usage – git remote add origin URL
In the screenshot below, we are adding remote URL “https://github.com/geek-as-service/git-commands” by name “origin”.
Next set of commands focus on knowing more about what had been done and what is going in local repository, i.e.
- list of commits.
- changes/files which have been committed but not pushed.
- changes in the particular old commit.
Getting Info regarding Git Operations
git logShows the history of commits.
Each commit has commit hash, author, date and commit message. Commits are shown in chronological order(latest commit on top).
usage – git log
In the screenshot below, git log shows two commits. Commit hash is jibberish written as “commit e7f2b69c6daf9d3417f…….”. This hash is useful when using git show.
Shows all the changes done in particular commit. We can either use first five characters of commit hash or use complete commit hash.
usage – git show e55ce
git show REMOTE_NAME/BRANCH_NAME..BRACH_NAMEShows all the changes which you have committed but have not pushed.
usage – git show origin/master..master
In Screenshot below, the complete command is explained. “..” is difference between “origin/master” and “master”
git diffShows all the changes in tracking area which you have not staged or indexed. git status just shows the list of files but git diff shows the exact changes in each file. git diff only shows files which have been modified and have not been staged. It also does not shows untracked files.
usage – git diff
In the screenshot below, git diff shows all the changes which are in tracking with detail that whether these changes have been added or removed.
git diff –cachedShows all the changes in staging area i.e changes which are ready to be committed. It shows the modified changes which have been staged.
usage – git diff –cached
The above image list all commands to move from untracked files to tracking area and staging area and then back to tracking area, untracked area and last committed state of file.