Skip to content

How to use GIT (for beginners)

Andrea Bistacchi edited this page Jul 21, 2023 · 1 revision

Install a GIT client

On Windows install Git for Windows. On Linux and Mac systems, GIT is generally available as a command-line tool in the terminal. If you do not find the terminal in your Mac, you must install/activate XCode as explained here.

Other GIT clients are listed at git-scm.com.

GitHub Desktop is a GUI GIT client aimed at "focus on what matters instead of fighting with Git". However the classical command-line GIT can be more clear and effective. Maybe a better option is to use the GIT tools in PyCharm.

Instructions below summarize some common usage of the command-line client (valid for all operating systems).

Clone a repository to your PC

$ cd <your repositories location>
$ git clone <URL to the repository>

You will find a new local repository in 'your repositories location'/'name of repository' This is needed just the first time you add a new repository.

Register user name and password

git config --global user.name 'your username'
git config --global user.password 'your password'

Use --global if you want to set user/password for all git projects, or --local if you like to set them just for one repository.

Download new contents FROM the remote repository TO your local repository.

$ cd <your local repository directory>
$ git pull

This is a one-way synchronization FROM remote TO local.

Send updates back to online repository

$ cd <your local repository directory>
$ git add *, git add ., or git add -A (see below)
$ git commit -m "message for this commit" (now the commit is registered locally)
$ git push origin master (this uploads the updated files to the remote online repository in the master branch, use a different branch if needed)

This table explains the different options for '''git add''':

image

If you have removed files locally, use git add -u, and then git commit and git push, to have them removed also on the remote repository.

Update your local repository to the most recent version on the remote online repository

If you have not made changes to the local repository, use:

$ cd <your local repository directory>
$ git pull origin master

If you have made changes to the local repository, and you want to discard them and synchronize everything to the remote repository, use:

$ cd <your local repository directory>
$ git pull --rebase origin master

To see all current branches

$ cd <your local repository directory>
$ git branch

If you like to start a new branch

$ cd <your local repository directory>
$ git branch <new branch name>
$ git push origin <new branch name>

To merge a branch into the active branch

$ cd <your local repository directory>
$ git checkout <active branch>
$ git merge <branch to be merged>
$ git push origin <active branch>

Then you can delete the merged branch locally and on the remote repository:

$ git branch -d <branch to be deleted>
$ git push origin --delete <branch to be deleted>

To switch to a different branch

$ cd <your local repository directory>
$ git checkout <different branch>

To pull from a new/different remote branch

$ cd <your local repository directory>
$ git pull origin <different branch>

To discard all local changes, but save them for possible re-use later

$ cd <your local repository directory>
git stash

To permanently discard local changes on a particular file

$ cd <your local repository directory>
git checkout -- <file>

To permanently discard all local changes and restore your local repository to the remote one (alternatively you can also delete the local folder and clone it again)

$ cd <your local repository directory>
$ git fetch origin
$ git reset --hard origin/master

To restore files deleted at a given stage

git log --diff-filter=D --summary

Returns all the commits which have deleted files and the files deleted, then use:

git checkout <COMMIT NUMBER>~1 <path/to/file.ext>

to restore the deleted file. Here <COMMIT NUMBER> is the id of the commit that can be found at step 1, e.g. e4cf499627...

To add another repository as a submodule

From within the "base" repository:

git submodule add <path_to_submodule_repository>

Now to automatically get updates from the original remote repository to the local copy (submodule):

git submodule update --remote

To send commits and pulls back to the original repository, use the usual add, commit and push commands from within the submodule folder.

To completely remove a submodule from a repository:

git submodule deinit <submodule-name>    
git rm <submodule-name>

Other tutorials on GIT and PyCharm:

Tutorials on branches and checkout:

https://www.jetbrains.com/help/pycharm/manage-branches.html#checkout-Git-branch

https://www.atlassian.com/git/tutorials/using-branches

https://www.atlassian.com/git/tutorials/using-branches/git-checkout

https://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide

https://softwareengineering.stackexchange.com/questions/119782/what-does-stage-mean-in-git