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
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.
pwdyou can see your user home directory as projects and if you do
lsyou will see our
Notepad++ hipster.txtand 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.
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
git status, Git tells us that we have changes to be committed and then will be a new file in the staging area.
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.
ls, you can see all the files which are present in the folder.
git status, you can see all the untracked file.
git add .
git status, Git tells us that all the files are staged now.
git commit -m "committing all the files"
git status, you will see your working directory is clean
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>
My First Commit!, and then save and exit.
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.
git statusagain it will tell us that, we need to push changes to publish your local commits.
git pushoperation am going to do
git pulloperation. (refer Pull operation)
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.
git add Fruits.txt
git statusyou will see a new file Fruits.txt in the staging area and hence Git suggests to make a commit.
git commit -m "commit all fruits"
ls, you can see all the files in your project folder including fruits.txt.
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.
git status, you are in a clean working directory.
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 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 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.