Git – basic commands to keep track of your project

Install GIT

Git is very useful to keep track of you python (or any other language) project and to collaborate with others. After you install it, you will have to use it through the following commands:

 

git init

With this you, after you choose a position on your drive, you start a local git repository, where your projects will go. A folder will be created, that you do not have to go in. It’s the one that keeps track of the things you did.


git add

This command will put the file in the staging area.
As you can see, I had a file that was not included in the staging area (001_git_init.PNG). I then added this file, while two other files where created, but not added.

git status

With this one you can see the files in the staging area.

You can see the use of the status command in the previous images.

git commit

This will commit all the files in the staging are in the local repository.

git push

You have to use this command to push your commited files to the github remote repository. You will need to have a github account.

To create the repository you have to go in https://github.com/ and create an account. Then make a new repository (in my case the account name is formazione and the repository name is try, you have to substitute these two names with your account name and repository). Then in the cmd, that is in the local folder, write:

echo "# try" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/formazione/try.git
git push -u origin master

 

You can be asket to insert the name and password, before it pushes all the files. At the end you will have the readme.md file uploaded with the PNG image. If you want to push the other two png files (of the example), you must add them to the staging area, 

Now if you check with the git status

if you go in github, you will see these files are not there yet

You have to commit and then push them.

git commit -m “added three png files”

the -m is for message, to write the changes you made.

If you do not put the -m an editor will open (to me it’s Vim… write the message, press ESC and then write :q! to save and exit.

git push

after that, the github repository will be like this:

So, this is what you have to do:

  • create a github account
  • create a repository
  • go in a folder
  • open the cmd from there and write
echo "# try" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/formazione/try.git
git push -u origin master

then create files or change existing one. Add them to the staging area, commit them with a message, push them to github.

Let’s create a text file.

echo “This is a test” >> newfile.txt

let’s add it to stage (you can use git add . to add everything without writing the name of the file to be added)

git add newfile.txt

make the commit with message

git commit -m “added a text file”

push it (you can simply write ‘git push’)

git push -u origin master

you can check the status with git status

The github will be like this (I also added some png to the dir, in the meanwhile)

The newfile.txt is at the end of the list. You can in this way add thing to the project and keep track of the changes, so that you can go back if you want. You can also make branches out of the master to make different projects out of the original one, everyone having their own life. We will come back later to see these other options.

Another example with pull

Let’s see another example and this time, at the end of the video, we will see how to use the command pull, very useful when you change something on github and you want to bring that change into your local folder. It is useful also when someone else makes a change to the code in case of collaboration or when you cloned a repository that has changed. the command is simply git pull.

To update your work

When you initialized your work area, you will update frequently, doing this:

git add .
git commit -m "I made some changes"
git push -u origin master

So, the most of the time you will launch these commands.

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.