Git basic commands

I have been using git for a while but not to often so as to remember all the commands. The goal the following list is to have a summary of the most frequently used by me Git commands.

Upload to remote repository

The process of updating a remote repository involves the following commands

git status

will return a list of untracked changes

git add .

(Dont forget a space between add and the dot)

will add all untracked files, while

git add <file>

will add only the given file

In case there are files to delete from the repo the following command does the trick

git add -u

while

git commit -m "Description of the commit"

will submit the files that have been added so far for upload but it will not upload anything yet.

git push -u origin master

This finally will update the remote repository.


 Download from remote repository

It is always a good idea before start working on a project to check if there are updates on the remote repository

git remote update

and then

git status -uno

This will tell whether the local repository is behind.

In that case

git pull

will update the local repository


 Exclude files

Typically files which are created as part of compile process and depend on the operating or local system as well as autosaved files should not be uploaded to the remote repository.

To avoid this one can specify rules locally in the file

.git/info/exclude

For example to ignore mex object files we can add *.mex.

There is also the .gitignore option. This is good option when only few files need to be uploaded to the remote repo. One can exclude all files and the include the sources only. See this at the end of the page for example

However the gitignore can be a bit strange in case there are multiple folders in a project and selected files from each folder need to be tracked. Lets say there is a project with 2 folders folder1 folder2 and each folder contains source code with extension *.cpp and *.hpp and other files and folders like CMakeCache.txt CMakeFiles/ etc. and we need to track only the code. The .gitignore should look like this: 

#first ignore everything
/*
#Next "un-ingore" the first folder
!/folder1
#Now ignore all the content of that folder
/folder1/*
#and finaly "un-ignore" the source code
!/folder1/*.cpp
!/folder1/*.hpp
#Similarly for each folder
!/folder2
/folder2/*
!/folder2/*.cpp
!folder2/*.hpp

 

Resolve Conflicts


It is possible that the git push command will refuse to upload the changes. This can happen when two people A and B modify the same file. If A uploads the changes first then git will refuse to push the changes of B.

In that case the B should do the following:

git pull --rebase

First the git pull –rebase will update the local repository. However there will be some conflicts. The git status command will tell which files contain the conflicts (see more detailed description here). Then B has to edit the files and resolve the conflicts. When its done B need to add the files with git add and finally

git rebase --continue

will complete the pull and the local branch will ready for git push.


 

Find the remote URL

git remote show origin

 

Getting help

Last but not least for every command you can get help using the flag –help e.g.

git status --help

2 thoughts on “Git basic commands

  1. Hi Dr George,
    I am supervising a master degree student and she has been asked to use mSim for her thesis. We are planning to replace your optimizer with another evolutionary optimizer. Another colleague of mine is doing her PhD in groundwater modelling. Can we use mSim for recharge estimation for a large aquifer of about 100000 km2 area? If we want to carry out seawater intrusion studies for this large aquifer and want to calibrate the mSim, how do we do that? If you could suggest us and direct us to use mSim for calibration, then coauthorship in the resulting publication is possible in case you agree.

    Thanking you.

    Regards,

    Surajit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.