git

To share this page click on the buttons below;

Basics on repositories

Create a repository

To create a new repository into an existing directory:

git init

Clone a repository

By cloning a repository the exact copy of the repository is obtained. To clone an existing repository:

git clone <existing_repository> <cloned_repository>

Get the status of the repository

To get the current status of a repository:

git status

Commit

To commit files into a repository:

  1. first add the files to the stage area
  2. then commit the files from the stage area to the repository

The stage area is a place where you put the files you are going to put into the next commit.

Stage files

To add <files> to the stage area:

git add <files>

Actual commit

To commit staged files, into a repository with a "<commit message>", specifing the author of changes contained in the commit:

git commit -m "<commit message>" --author="<someone@mail.com>"

To commit a modified file without staging it:

git commit <file>

Basic configuration

Set the editor

If the "<commit message>" is empty git can automatically show an editor to fill the commit message. To set which editor to use (on bash)

export GIT_EDITOR=<editor>

Set the commit author

To set the author (and avoid to specify it on commit message).

git config user.name "<name>"
git config user.email "<email>"

The environment variables GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL override the settings made with previous commands

Basic repository information

Log

To see the list of commits:

git log

Commit details

To see details of a certain commit

git show <commit number>

Used without <commit number> shows the details of the last commit

Differences

To see differences between 2 commits

git diff <commit number> <commit number>

git diff used without argument perform a comparison between staged area and working copy.

Basic repository changes

Remove a file

Remove a file from the repository is a 2 steps procedure:

  1. remove the file with git rm <file>
  2. commit the change

Rename a file

To rename a file in the repository there are 2 possibilities:

  1. Manually rename the file, remove the old filename from the repository and then commit the renamed file
  2. Use git mv <file> and the committing the change

Change last commit

To change the last commit

git --amend

Remove staged files

To remove files from the stage area:

git reset HEAD <file>

To overwrite changes

To overwrite file changes with the content of the last version committed

git checkout <file>

To share this page click on the buttons below;