Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Git help

Benjamin Chauvin edited this page Dec 15, 2017 · 17 revisions

This wiki page provides valuable information to use git.

First steps

Get help

Use git help command to obtain more information about how to use Git. To obtain more information about a specific git command:

git help command

For instance, git help add provides information about the use of the git add command.

Create a git repository

git init is the git command to create a git repository.

mkdir my_repository_name cd my_repository_name git init

Note: this step is a priori not necessary if you develop in RINGMesh.

Clone a repository

git clone is the git command to clone a git repository. In the case of cloning RINGMesh:

git clone https://github.com/ringmesh/RINGMesh.git

If you need to clone a repository without installing a version of the files (so you only get the .git directory containing all the changesets of the repository):

git clone --bare https://github.com/ringmesh/RINGMesh.git

This second kind of cloning is generally not recommended for a classic development in RINGMesh.

Set your personal information

You can define several personal features in git. Two features are mandatory:

  • Your name.
  • Your email.

Generally, this information is common to all your repositories. To define them in your system (/where/is/your/home/directory/.gitconfig):

git config --global user.name "FirstName Surname"

git config --global user.email your_email@something

You can also define this information locally (in /where/is/my/repository/.git/config) by replacing --global by --local. The local definition, if any, has priority over the global definition. Contrary to the global definition, you have to be in the repository directory to define the local configuration with git config --local.

Formatting and Whitespace

See the section Formatting and Whitespace in this page.

File managements

Add new files

git add is the git command to add one or more files to the repository:

git add file_one [file_two [...]]

[X] means that X is optional.

To cancel this file addition, i.e., if you added a file but it was a mistake (and if no commit have been performed yet):

git reset HEAD -- file

Remove files

git rm is the git command to remove one or more files from the repository:

git rm file_one [file_two [...]]

git rm also removes the file from the computer. To just remove the file from the repository but keep it in the computer:

git rm --cache file_one [file_two [...]]

Move a file

Use git mv to move a file from a location to another:

git mv /path/to/file_I_want_to_move /new/path/moved_file

You can use this command to rename a file.

Note: If you want to move or rename a file, DO NOT remove it then change manually its path and readd it to git. Why? Because for git the file before moving has to relation at all this the file after moving. If you need to track the changes of this file through time, you will get a disconnection due to the remove and readd. git mv permits git to know that the files before and after moving are the same and avoid any tracking disconnection.

Cancel your changes

If you changed a file but want to revert your changes:

git checkout -- file_one [file_two [...]]

To revert all the modified files:

git reset --hard

Find the guilty developer

Use git blame to know which developers changed each line of a file (latest change for each file):

git blame file

What have I done?

To know all the changes you have done since the last commit, use git status. You will see the modified files, the added files, the removed files, and the untracked files.

Find when a file was added to Git

git log --format=%aD file_to_check | tail -1

Untracked files

All the files that you have not added to the repository are untracked. git status lists all the untracked files if any.

How to select the untracked files visible by a git status command?

Some files have to be in the repository directory without being added to the repository (e.g., files generated by the compilation, IDE project files, etc.). You may have a lot of necessary untracked files. All of them are listed by a git status, which is a visual contamination of the status of your repository. To not see them, write their names in the .gitignore file at your repository root (add the file .gitignore to your repository if it is not already done). If you write a directory name, all the files within this directory will be ignored.

Note: Do not add all the untracked files to the .gitignore. Indeed, some untracked files are not supposed to be within the repository directory and should be removed (e.g., temporary commit diff files).

How to remove all the untracked files?

To remove all the untracked files which are not written in the .gitignore (so all the untracked files visible by a git status), do:

git clean -f

To remove untracked directories (and files):

git clean -d -f

To see the files to be deleted without removing them:

git clean -n

To remove the untracked files which are ignored by git (so defined in the .gitignore), and do not touch to the other untracked files:

git clean -X -f

Add untracked files and remove deleted files

git add -A adds to the repository all the untracked files which are not ignored (not in the .gitignore), and removes all the tracked files which have been deleted from the computer (rm instead of git rm).

Update the repository with your changes (locally)

Commit your changes

git commit is the git command to save your changes. It is recommended to perform a git status before commiting to avoid to commit something wrong.

git commit will save in a changeset all the files you added or removed. Be careful, a tracked file which has been changed must be added git add before being commited. If not, the commit will not incorporate your changes concerning this file.

git commit will open a text editor to make you write a commit message. Use git commit -m "your message" to directly write a message commit when you run the git commit` command. Do not neglect the importance of the commit messages. Write clear and detailed messages.

Add all the files before a commit

The safe way to add all the tracked files which have been modified for the next commit is to use the option -a:

git commit -a -m "your message"

Another method is:

git add . The problem of this method is that it will add all the untracked files which are not ignored.

Modify the commit message

If you want to change the message of the commit:

git commit --amend

Cancel the last commit

git reset HEAD^ is the git command to cancel the last commit. The files within your repository contain the modifications corresponding to this cancelled commit. So you can rework these files and commit again. This is convenient if your previous commit was almost done but needed a minor change.

If you do not want to keep the changes corresponding to the last commit:

git reset --hard HEAD^

Fake commit

If you want to go back to a previous revision or go to another branch, you cannot do it if you have changed tracked files. An option is to first commit these files. For any reason, you may not want to commit your changes yet. To avoid the commit and keep your change:

git stash

This will revert all your files but keeps your changes locally. You can do whatever you want such as going to another revision. To recover your changes:

git stash apply

This command do not remove the saved changes from the stash. If you want to remove the changes from the stash, use instead:

git stash pop

Branches

Branch enables to have different versions of the code. Each developer can develop his/her own version without any bad implication on the work of another developer. In RINGMesh (and in Git in general) the default branch is called master. This branch is the main branch of RINGMesh and corresponds to the current stable development branch of RINGMesh. The branch called stable is a less advanced branch in terms of changes, but its goal is to be more stable (not a development branch but a release).

You have to work on your own branch and then submit a pull request on GitHub to be able to merge your changes into the master branch.

Local branches versus remote branches

In Git, there are local branches and remote branches. The remote branches are the one of the GitHub repository (remote server). The local branches correspond to the branches you are currently modifying (and are linked to a remote server branch (generally with the same name).

In which branch I am

To see your local branches:

git branch

In the output of this command, a star indicates your current branch. To see the remote server branches in addition to your local branches:

git branch -a

The remote server branches begin by the name of the remote server (generally origin).

Go to a branch

git checkout branch_I_want_to_go

Create a new branch

git checkout -b my_new_branch

Delete a local branch

git branch -d local_branch_I_want_to_delete

This command only works if the local branch has been merged with the local mastewr. In theory you will never put the changes of a local branch into your local master since this must be done on GitHub remotely. After a merge on GitHub, you have to delete your local branch in a hard way (no check if the local branch has been merged with the local master):

git branch -D local_branch_I_want_to_delete

Merge two branches

Even if you will not merge your own branch into the master branch (done in GitHub), you will often do the reverse: merge the master branch into your development branch to take into account the works made by the other developer. Before merging be sure that you have the last version of the master (git pull, see the section about pull and push). Then, go to your development branch and:

git merge origin/master

If no conflict you are lucky! Else, Git will list the files that you have to manually modify. If for the merging you did:

git merge master

That may work if you pull the master from the remote server and that the changes have been applied to your local master (else you will merge with an old version of the master). The simplest way to have your local master up-to-date is: git checkout master && git pull.

Rename a branch

git checkout branch_I_want_to_rename git branch -m new_branch_name

Go to a specific revision instead of a branch

git checkout branch_name enables you to go to the current status of a branch. To go to a specific revision (= changeset or = a commit):

git checkout changeset_id

To get the changeset id, explore the history of the RINGMesh development with git log.

Remote server

A remote server is a repository in which the work of all the developers is synchronized. Several servers can be defined. For RINGMesh, there is only one official remote server which is the GitHub repository. This section describes how to handle your repository with the GitHub repository.

If you cloned your repository from GitHub, by default your repository knows that your remote server is the one on GitHub.

Push your changes

git push is the base command to send your changes from your repository to GitHub. Basically, your commits will be sent to the RINGMesh repository on GitHub. If you have several remote servers, after the push in this command, add the label of the remote server to which you want to push your changes. For instance, if the GitHub repository is labelled origin (usually the case), write: git push origin.

This command alone only sends the commits of the current branch (if you do a git branch). If you want to push a specific branch:

git push my_branch_to_send If your current branch is my_branch_to_send, so it is equivalent to git push. To push all the branches:

git push --all

You will get an error if you try to push a branch which does not exist in the GitHub repository. To overcome that:

git push --set-upstream origin new_branch_name

Pull the changes

To get the changes made by the other developers and pushed in the GitHub repository:

git pull

This command takes the changes and merges your current branch with the remote version. For instance, if you are in the branch called my_work and if there are new changes in this branch on the GitHub repository, the files in your local branch are merged with the changes. To avoid this merge:

git fetch

Useful links