2021-04-03 10:58:27 +02:00
# Git
2021-04-03 12:17:49 +02:00
## Description
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
2021-04-03 10:58:27 +02:00
## Usefull links
2021-04-03 12:17:49 +02:00
- [Git Cheat Sheet ](https://education.github.com/git-cheat-sheet-education.pdf )
2021-04-03 10:58:27 +02:00
## Git System
2021-04-03 12:21:01 +02:00
![Git System ](img/Git_Guide.png )
2021-04-03 12:17:49 +02:00
=
# Basic commands
## Git clone
#
Clone repo located at ** \<repo>** onto local machine. Original repo can be located on the local filesystem or on a remote machine via **HTTPS** or **SSH** .
> git clone <repo>
## Git init
#
Initialize an existing directory as a Git repository
> git init
Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
> $ git init \<directory>
=
## Git status
#
List which files are staged, unstaged, and untracked.
> $ git status
## Git config --global name & email
#
Must be identifiable for credit when review version history.
> $ git config --global user.name “[firstname lastname]”
> $ git config --global user.email “[valid-email]”
## Git add
#
Stage all changes in ** \<directory>** for the next commit. Replace ** \<directory>** with a ** \<file>** to change a specific file.
> $ git add readme.md image.jpg
## Git commit
#
Commit your staged content as a new commit snapshot
> $ git commit -m "First Commit"
2021-04-03 10:58:27 +02:00
After doing this, you may fix the identity used for this commit with:
2021-04-03 12:17:49 +02:00
> $ git commit --amend --reset-author
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
## Git log
#
Show all commits in the current branch’ s history
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
> $ git log
### Graphs
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
The --graph option draws an ASCII graph representing the branch structure of the commit history. This is commonly used in conjunction with the --oneline and --decorate commands to make it easier to see which commit belongs to which branch:
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
> $ git log --graph --oneline --decorate
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
![git log ](img/git_log.PNG )
=
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
## Tig
#
2021-04-03 10:58:27 +02:00
Tig allows you to browse changes in a Git repository and can additionally act as a pager for output of various Git commands. When used as a pager, it will display input from stdin and colorize it.
> $ tig
2021-04-03 12:17:49 +02:00
When browsing repositories, Tig uses the underlying Git commands to present the user with various views, such as summarized commit log and showing the commit with the log message, diffstat, and the diff.
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
![tig ](img/tig.PNG )
=
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
## Git diff
#
Show unstaged changes between your index and working directory.
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
> $ git diff
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
#
# SHARE & UPDATE
#
2021-04-03 10:58:27 +02:00
### Retrieving updates from another repository and updating local repos
=================================
add a git URL as an alias
>git remote add [alias] [url]
fetch down all the branches from that Git remote
>git fetch [alias]
merge a remote branch into your current branch to bring it up to date
>git merge [alias]/[branch]
Transmit local branch commits to the remote repository branch
>git push [alias] [branch]
>git pull
fetch and merge any commits from the tracking remote branch
## BRANCH & MERGE
### Isolating work in branches, changing context, and integrating changes
![Remote Execution ](img/Branching.png )
>git branch
list your branches. a * will appear next to the currently active branch
>git branch [branch-name]
create a new branch at the current commit
>git checkout
switch to another branch and check it out into your working directory
>git checkout -b|-B < new_branch > [< start point > ]
Specifying -b causes a new branch to be created as if git-branch(1) were called and then checked out.
If -B is given, < new_branch > is created if it doesn’ t exist; otherwise, it is reset. This is the transactional equivalent of
>git merge [branch]
merge the specified branch’ s history into the current one
>git log
show all commits in the current branch’ s history
## TRACKING PATH CHANGES
### Versioning file removes and path changes
=================================
Delete the file from project and stage the removal for commit
>git rm [file]
Change an existing file path and stage the move
>git mv [existing-path] [new-path]
Show all commit logs with indication of any paths that moved
>git log --stat -M
2021-04-03 12:17:49 +02:00
### Checker le satus
2021-04-03 10:58:27 +02:00
> git status
Ajouter un element dans la branche
> git add Readme.md
Commit les changement
> git commit -m "First commit"
Show all commit
>git checkout
Merge
>git merge
2021-04-03 12:17:49 +02:00
# Setup
2021-04-03 10:58:27 +02:00
2021-04-03 12:17:49 +02:00
## Configuring user information used across all local repositories
2021-04-03 10:58:27 +02:00
>git config --global user.name “[firstname lastname]”
set a name that is identifiable for credit when review version history
>git config --global user.email “[valid-email]”
set an email address that will be associated with each history marker
>git config --global color.ui auto
set automatic command line coloring for Git for easy reviewing
2021-04-03 12:17:49 +02:00
## Saving your uncommitted work for a quick fix then getting it back
2021-04-03 10:58:27 +02:00
>git stash
temporarily stash your work since your last commit
>git stash pop
fetch your stashed work to continue it
$ git stash pop stash@{2}
in case you want to apply a specific Stash item (not the most recent one), you can provide the index name of that item in the "pop" option
2021-04-03 12:17:49 +02:00
## Rewriting history
2021-04-03 10:58:27 +02:00
>git commit --amend -m "New commit message"
replace the last commit
>git commit --amend --no-edit
replace the last commit without changing the commit message
>git rebase -i {branch}
take the precedent commit and add it to your branch
2021-04-03 12:17:49 +02:00
## Ignoring files
2021-04-03 10:58:27 +02:00
>touch .gitignore & & echo {what you want to ignore} >> .gitignore
create a file and specify what (extensions, directories, files) to ignore in it
>git rm --cached {fileignored}
remove from the tracking index a file that should be ignored but wasnt because already tracked when the ignore rule was created