LinuxSysAdminsDoc/Git/readme.md

237 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Git
## 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.
## Usefull links
- [Git Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf)
## Git System
![Git System](img/Git_Guide.png)
=
# 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"
After doing this, you may fix the identity used for this commit with:
> $ git commit --amend --reset-author
## Git log
#
Show all commits in the current branchs history
> $ git log
### Graphs
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:
> $ git log --graph --oneline --decorate
![git log](img/git_log.PNG)
=
## Tig
#
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
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.
![tig](img/tig.PNG)
=
## Git diff
#
Show unstaged changes between your index and working directory.
> $ git diff
#
# SHARE & UPDATE
#
### 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 doesnt exist; otherwise, it is reset. This is the transactional equivalent of
>git merge [branch]
merge the specified branchs history into the current one
>git log
show all commits in the current branchs 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
### Checker le satus
> 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
# Setup
## Configuring user information used across all local repositories
>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
## Saving your uncommitted work for a quick fix then getting it back
>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
## Rewriting history
>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
## Ignoring files
>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