Git Basic Workflow Operations

To understand how Git works with its local and remote repositories, this article explains you about Git basic workflow, which involves some basic operations such as

  • Add Operation
  • Commit Operation
  • Pull Operation
  • Push Operation

Git Add Operation

Git Add operation involves adding a new file to the staging area before doing any commits, so here am using my default text editor to create a text file.

So before adding a file to the staging area, we should create a .txt file.

  • Before doing remember some URL which is going to use in this demo, first one is bitly URL to the git repository on Github we are using it here if you have not forked before then go to this URL and fork it into your personal repository.
  • If you do not know how to fork just refer our article on Forking and Cloning
  • Next, are and
  • Go to your terminal and give pwd you can see your user home directory as projects and if you do ls you will see our starter-web-1/ repository.
  • Enter Notepad++ hipster.txt and press enter this command will invoke notepad and it is ready for some text.

As I have already explained about hipster website in the Text Editor for Git article, which provides us with some dummy text. Now open hipster Ipsum website and click on Beer me to get some dummy text and select as you required and copy the text.

  • Go to your editor page and paste it in and save by using command Ctrl+s and press without quitting Ctrl+w and exit by using Alt+F4.
  • Now return to your terminal and dols.
  • Here you can see a hipster.txt file in your folder.
  • If you do git status, Git tells you that there is one untracked file and it also tells you how to add that file to the staging area, so that Git can track that file.
    git add hipster.txt
  • Do git status, Git tells us that we have changes to be committed and then will be a new file in the staging area.
  • So in order to proceed further, we have to commit a message. (refer commit operation)

Git Add Multiple Files

We Learnt how to add the file to the staging area before doing commit. Now let us see how to add multiple files at once by using the following command.

  • Before adding, am going to create some .txt files by using the default editor Notepad++ like file1.txt,file2.txt,file3.txt and file4.txt respectively.
  • Creating file1.txt
  • Creating file2.txt
  • Creating a file3.txt
  • Creating file4.txt
  • If you do an ls, you can see all the files which are present in the folder.
  • Now check for git status, you can see all the untracked file.
  • And then I will add all the above-mentioned files at once by using the command git add .
  • Now you can check for git status , Git tells us that all the files are staged now.
  • You can also commit all the files at once by using the command git commit -m "committing all the files"
  • Again you can check for git status, you will see your working directory is clean

Cloning in Git

Git Commit Operation

Git Commit operation involves saving changes to the local repository, the important thing you have to note that, you have to explicitly tell Git which changes you wanted to include in a commit message before running a commit command.
commit command is git commit -m <values for m>

  • Here we have a default editor so we can commit without specifying the parameters.
  • Hence the default editor will show up for the commit message.
  • I am committing my message in the default editor page as My First Commit!, and then save and exit.
  • Git will use same commit message whatever we entered in the editor as our commit message.
  • Now, it will be returned with the commit message as it is on the master branch and a unique identifier for the commit, followed by commit message MY First Commit and we have 1 file changes with 3 insertions.then the last line tells us which file actually was changed.
  • Let us do another git status, Git tells us, we are ahead of origin master by 1 commit and nothing to commit hence working directory is clean, which means there is nothing left in the staging area.
  • So now go back to your Github browser and refresh the page, there is no hipster.txt file here because the commit command was actually a local operation and hence it does not affect the remote repository.
  • Do a git status again it will tell us that, we need to push changes to publish your local commits.
  • Before doing git push operation am going to do git pull operation. (refer Pull operation)

Changing the Commit Message

If the commit message contains the unclear or any missing information, you can make it correct by adding the missing information and you can change only the most recent commit message which is not been pushed to the repository.

  • Let's start by creating a .txt file by using the default text editor notepad++.
  • Here am going to create a Fruits.txt file and adding some dummy text into it and save then exit.
  • Now add Fruits.txt file to the staging area by using git add Fruits.txt
  • If you check git status you will see a new file Fruits.txt in the staging area and hence Git suggests to make a commit.
  • Now let's commit by using git commit -m "commit all fruits"
  • If you do an ls, you can see all the files in your project folder including fruits.txt.
  • So, Now if you wanted to make any changes to the commit, you can use --amend command like git commit --amend, press enter, then you can see editor page so there you can edit the commit message as commit all mango save then exit.CO-amend
  • you can see the updated commit message as shown below.
  • Now if you do a git status, you are in a clean working directory.

Git's History

Git Pull Operation

Git Pull Operation involves updating the local version of a repository from a remote repository. git pull includes two commands such as git fetch and git merge.

Git pull first runs fetch command and downloads the content from the specified remote repository and then git merge command executed to merge the remote content references and heads into a local merge commit git pull origin master

The above command will update our repository with any changes that may have happened on the remote repository down to our local repository. So just to make sure that we are up-to-date, do pull operation before doing any pushes.

Once you pass pull command, git pull origin master , Git will return that, it is already up to date.

This is the best practice to do pull operation before doing push operation because, if you are working with the team then you had multiple developers or contributors who are putting their code in the same repository then you might get out of synchronization.

So now, you can do git push operation.

Git Push Operation

Git Push Operation stores the data permanently to the git remote repository after a successful git push operation, every contributors or developer can see your changes.

Fetching imports commit to local branches whereas pushing exports commits to remote branches.

push command is git push origin master

Git push command will push any commits from the local repository to the remote repository by reference name with the branch you wanted to push.

In push, the command is going to specify both the Branch and Remote reference. Here master is the only default branch we have, and origin is a remote reference.

Once you pass push command sometimes you may be prompted to enter username and password to get back to Github, login with your credentials and press enter.

Git Pushes all the changes you have on your local system to the remote repository. So Git pushes the changes to the starter-web-1 git repository on Github.

Basically, Git pushed the commit from 4be7f0 up to 170822d and it is also master to master branch means, local master to the remote master branch.

Now go back to Github and refresh the browser, you can see hipster.txt with the commit message as well.

Click on the hipster.txt file, you can see the content of that file also.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions