CHEAT SHEET ON GIT

Configure Git

After installing Git, configure name and email address, ** in this order ** :

$ git config --global user.name "name surname" $ git config --global user.email "e-mail"

From now on, my «commit» will indicate by whom (and his email) they were made.

Initialize a project

Place in the project folder and then do $ git init
This will create a hidden folder (.git) with everything GIT needs to manage the project.

join an existing project

$ git clone <repository URL>   ← Download a complete project and its version history $ git remote add <my branch> git@github.com:<user>/<name of the projet>.git   ← here, we add a connection to the depot

Work whith Git

Definition of "add" → indexing Whenever you edit/add/delete code and/or file/folder do an "add", and a "commit" and a "commit" as soon as the improvement is finished.
$ git add page.html style.csshere, 2 files at once
$ git commit -m "My comment"Saves a snapshot of file(s) permanently in the version history
$ git statusList all new files as well as those that have been modified to "commit"
$ git diffShows all the changes since the last "git add"
$ git diff <file>Displays the changes of 1 particular file since the last "git add"
$ git reset <file>Removes the file from the index, but keeps its contents !
$ git showView metadata and content changes included during the last "commit"

on the remote repository

$ git remoteList the remote servers that you have registered
$ git remote -vDisplays the URL that Git has stored for each short name
$ git remote show <remote name>Show info about a remote repository
$ git ls-remoteList the fingerprints of all branches

make changes to the project

$ git rm <file>Deletes the file from the working directory & updates the index
$ git rm --cached <file>Deletes the file from the version tracking system but preserves it locally
$ git mv <file_new_name>Renames the file and prepares the change for a "commit"

Cancel a "add"

$ git reset <file>

Cancel a "commit"

$ git reset HEADCancel the last "commit $ git reset HEADˆcancels the penultimate "commit" $ git reset d6d98923868578a7f38dea79833b56d0326fcba1Undo a commit whose footprint is "d6 ..." $ git reset d6d98923short version of the previous command

switch to a "commit" or "branch"

$ git checkout <nom de la branche> $ git checkout <empreinte du commit>

List the commits

Show the last x commits : $ git log -n x

Redo commits

$ git reset <commit>   ← Cancel all commits after <commit> with the changes $ git reset --hard <commit>   ← Removes all history and changes made after the specified <commit>

Branches

Create a branch

→ With 2 lines : create and switch on the new branch

$ git branch <name_new_branch> $ git checkout <name_new_branch>

→ With 1 line only : create and switch

$ git checkout -b <name_new_branch>

Delete a branch

← If the branch is local and is not created on the remote repository

$ git branch -d <name_local_branch>

→ If the branch is present on the romote repository

$ git push origin --delete <name_remote_branch>

List the branches

$ git branch   ← local $ git branch -a   ← local + remote

Check history of version

$ git diff <first_branch> ... <second_branch>   ← Show differences in content between 2 branches $ git diff show <commit>   ← Show changes to the metadata and content included in the specified commit

merging the branches

$ git merge <name_branch>   ← Combines / merges the history of the specified branch into the current branch

**To do the following, you must have created a new repository on github and set up his account with his SSH key !!**

synchronize changes

$ git fetch <name_of_repository>Get all the history of the named repository
$ git merge <name_of_repository/<branch>Merges the repository branch into the current local branch
$ git push <alias <branch>Send all commits from the local branch to GitHub
$ git pullRetrieves all the repository history and incorporates changes
$ git push -u origin <branch>Here, we update the "remote" by pushing the branch <branch>

Send a project to the project manager (Github)

Make sure you are registered before;) and send commits to remote repository : $ git push -u origin <name_branch>

Retrieve a project

Update the local repository : $ git pull   ← We recover only the details of the modifications !