Skip to main content

GIT Commands: Pull, Add, Commit, Push.....and sometimes Revert


Pull, Add, Commit, Push ...... then Repeat

We talked about GIT, GIT Hub, and the three different areas GIT works in.  Now it is time to talk about some of the most used GIT commands.  

I am currently working with a team on a project called Stedman.  We are all using the GIT Hub server to track our work as we make changes to the files within the Stedman project.  

Many changes can be made by other members of the team, from the time I sign off to the time I sign back on.  How do I get the changes they made to a file I need to work on, onto my computer?


ENTER GIT COMMANDS:

1.  The first thing I do is goto my command window on my computer and navigate to the directory where the file I want to work in is located (this will be a directory that is being tracked by GIT).   *If you need a quick refresher:  cd file_name  will take you through each of your directories until you reach your desired location.  Issuing an  ls  command will list the files (sub-directories) in each of your directories. 

2.  I then need to navigate into MY Git branch that has been assigned to me to work on (There will be a future post on what this is and how it works). 

3.  git pull - Once I am in the correct branch, I need to "PULL" (download) the most current file from the GIT Hub repository that my team is working in.  To execute the pull command, you just type 'git pull' from the directory you are wanting to work in.  See the picture below to see my 'git pull' command.  

Look at my 'git pull' command on the first line.  Notice that before the command, it says Stedman. This is the directory I am working in.
Remember to PULL first

Now that I executed a 'git pull', my Git Repository is up-to-date and synched with the Git Hub Repository.


4.  git status - This command lets the user know which of the three areas GIT is in.  Here is an example of what this command looks like after issuing a 'git status' after doing a 'git pull'.

NOTICE the 'git status' command on the first line.
It also shows that my branch is up-to-date with the GIT Hub repository (on the third line)



5.  git add <file_name> -  Once I am done modifying my file on my local computer and saving it,  I need to put it into the staging area of GIT.  I do this by going back to my command window.  I need to first make sure that I am in the correct directory that I am working in, as well as in the correct git branch for that directory.  I usually do a 'git status' command right before I am about to do a 'git add your_file-name' command.
Issuing this command will allow me to then decide whether to "commit" it to my local repository, or go back and make more modifications.  You will see the command I issued, 
git add StedmanLesson14.html  in the picture below (*don't forget to put the filename you want to add- in my case it is StedmanLesson14.html):



I immediately issue a 'git status' command to make sure the 'git add' went smoothly.  The picture below will show what that looks like:

NOTICE on the first line, you see the 'git add' command.  The second line the 'git status' command.  

You see that the "modified:    StedmanLesson14.html" is now highlighted in green.  This is telling me that my file was successfully added to the staging area and I can commit it when I am ready (or go back and work on my file some more).
Remember to ADD to the staging area


6.  git commit -m "place your comment here" - Once I am ready to make that commitment to saving it to my GIT repository, I issue a "git command".  As you can see, there is a "-m" after the word "commit".  That is so that I can leave a message about what I am committing.  

NOTICE the comment "fixed spelling errors in line 399" after the '-m'.  This lets myself or my team members know what this particular commit was for. 
It is good practice to always write a descriptive message about why you are issuing a 'git commit'.
GIT has now tracked my file with the new changes I made, and it is now saved in my local GIT Repository.

Remember:  COMMIT saves it to your local GIT repo and gets it ready for a push


I usually do another 'git status' to make sure my 'commit' went smoothly.  Here is what that looks like:

NOTICE is recommends a "git push" to send it to the GIT Hub Repository

7. git push -  NOW I am ready to PUSH (upload) my file with the new changes/modifications to the GIT Hub repository that my team is working from.  This way, everybody will have the current changes to the files.  The command to issue a PUSH is simply: git push .  Please see the picture below:  

The TOP line shows my PUSH command.  It also shows on the eighth line the GIT Hub repo where this is going.







Now that the git push has been made, my local GIT repository is synched and up to date with the GIT Hub repository.  

BUT WAIT:  what happens if a mistake is caught AFTER a 'git push' has been made?

8. git log - Issuing this command from your command window will show you the history of all of the 'git pushes' you have made in the 'git branch' you are working in.


9.  git revert <unwanted commit hash> - Once you have found the push you want to reverse from your 'git log', you need to find the unwanted commit hash.  In the picture below, you will see that the commit hash numbers are highlighted in gold.


     * I blocked out my Author information and the email that is associated with my GIT Hub account.

Once I found the commit hash number, I was able to issue the 'git revert' command.



This command took me back to my previous git push. I was able to work from there and the file was not ruined. This is the beauty of using GIT Hub, if you make a mistake in your branch, you can revert to the previous PUSH and start again.  


Remember this order when using GIT:

                                         git pull                                                                           git add <file_name>   
            git commit -m "your comment here"                                                  git push




Follow me as I learn to build my website bit by bit!    IronTreeDev.com

Photo by Bekir Dönmez on Unsplash
Photo by Alora Griffiths on Unsplash








Comments

Popular posts from this blog

XPATH and XPATH Expressions In XMLLINT

XPATH And XPATH Expressions Earlier, I told you about xmllint and xmllint for html files .  Let's say you just want to parse the <span> tags within your html file or just your <span lang="el"> tags? Enter:  Xpath. Xpath is yet another option available within the xmllint language. Remember, an Xpath is used to navigate through elements and attributes in xml and html documents.  Xpath uses Xpath Expressions to select nodes or node sets within a document. Example 1 .  Looking for all of the <span> tags within an html document. xmllint -- html -- xpath " // span" StedmanLesson10.html xmllint = This tells the command line that we are going to be using the xmllint language. space = because we always have space in between commands -- = Remember, these are the two hyphen-minus characters that we need to tell the command line that we are going to use an xmllint option. html = This is the xmllint option we want to

Back To The Basics

Click photo above to see photo clearly  It's Been A LOOONG Minute since I have coded!!   Even so, I decided to give JavaScript another try.  I always seem to do good and then when I get to this one area, I always seem to hit a brick wall.  Well....nothing like getting back up and trying again right?   I decided to try Rithm School's online JavaScript Fundamentals FREE course .  I have never joined their school but their free courses tend to explain things in a way my brain understands it.  💁 To let you know how much I have forgotten in the coding world, I couldn't even remember how to write the basic commands for an HTML document! Yesterday I started the fundamentals free course and I am so glad that I did.  Because of time, I only made it through the JavaScript History and Setup chapter, but that chapter was enough to get my memory flowing again!   It started to feel familiar and I quickly remembered why I enjoyed coding so much.  Yesterday and this morning, I just erased