Git:

master all secrets


Git: master all secrets


hexagon

     Laurent RICHARD


OpenClassrooms Infrastructure Manager

space space space

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

right OpenClassrooms

At OpenClassrooms, we believe that everybody can learn anything and that everybody has something to share.

Our mission is to make education accessible. To everybody. Everywhere. We’re working to do that every day, and we have been doing it for 20 years.

Mathieu Nebra started OpenClassrooms when he was 13 years old and was soon joined by Pierre Dubuc.

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Join the Infrastructure Team

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Initialize an existing directory as a Git repository


$ git init
$ git init --initial-branch=main # define the initial branch name
$ git init -b=main # short version
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Makes a duplicate of an existing Git repository.
Cloning is the most popular method through which developers receive a working copy of a central repository.


$ git clone <url> 
$ git clone <url> <repo_name>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Add a git URL as an alias


git remote add <alias> <url>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

It tells Git that you want to include updates to all your files in the next commit.


git add .
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Add a file as it looks now to your next commit


git add <file>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Create a new commit containing the current contents of the index and the given log message describing the changes.


git commit -m "description"
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

It moves all your committed changes to the remote repository.


git push
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Transmit local branch commits to the remote repository branch


git push <alias> <branch>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Push all of your local branches to the specified remote


git push --all
git push <remote> --all
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Tags are not automatically pushed when you push a branch or use the --all option. The --tags flag sends all of your local tags to the remote repository.


git push --tags
git push <remote> --tags
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Displays the current status of the working directory. You should use it in combination with git add and git commit to know what files were added and committed.


git status
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

List your branches. a * will appear next to the currently active branch


git branch
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Create a new branch at the current commit


git branch <branch_name>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Create a new branch at the current commit


git branch -M main
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Creates a new branch on your local machine and opens it.


git checkout -b <new_branch_name>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Delete a branch on your local filesystem.


git branch -d <branch_name>

Note: I always delete branches locally through this command and use GitHub UI to delete them globally. Seems for me like the safest method.

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Delete a branch on remote.


git branch -D <branch_name>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Integrate all changes from another branch into yours.


git merge <branch_name>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

By calling it, you will get all changes from the remote branch to your local copy.


git pull
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Fetch down all the branches from that Git remote

git fetch
git fetch <remote>
git fetch <remote> <branch>
git fetch --all # fetches all branches of all remotes
git fetch --dry-run

The --dry-run option will perform a demo run of the command. It will output examples of actions it will take during the fetch but not apply them.

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Stores uncommitted changes (both staged and unstaged) for later use and subsequently reverts them from your working copy.


git stash

You aren't limited to a single stash

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

List stashes


$ git stash list
stash@{0}: WIP on main: 5002d47 our new homepage
stash@{1}: WIP on main: 5002d74 our new contact page
stash@{2}: WIP on main: 5002d88 our new support page
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Reapply previously stashed changes

git stash pop

Choose which stash to re-apply

git stash pop stash@{2}
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Discard the changes from top of stash stack


git stash drop
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Show all commits in the current branch’s history

git log

Limit the number of commits that are displayed

git log -3
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Show all commits in the current branch’s history and display message in one line.


git log --pretty=oneline
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

"A Dog" = --all --decorate --oneline --graph

git log --all --decorate --oneline --graph

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Looking for a commit from a specific time frame.

git log --before="2021-12-25"
git log --after="2021-12-1"
git log --after="yesterday"
git log --after="1 week ago"
git log --after="2021-12-1" --before="2021-12-25"

Note that the --since and --until flags are synonymous with --after and --before, respectively.

logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Filter commits by their commit message.


git log --grep="JRA-123:"
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

If you’re only interested in changes that happened to a particular file.


git log -- foo.py bar.py
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Show the commits on branchA that are not on branchB.


git log branchB..branchA
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Diff of what is changed but not staged


git diff
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Diff of what is staged but not yet commited


git diff --staged
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Show the diff of what is in branchA that is not in branchB


git diff branchB...branchA
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

List existing tags in Git.


git tag
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

It removes everything in the staging area


git reset 
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Unstage a file while retaining the changes in working directory


git reset <file>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Clear staging area, rewrite working tree from specified commit


git reset --hard <commit>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

Apply any commits of current branch ahead of specified one


git rebase <branch>
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets

improve your productivity

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.unstage 'reset HEAD --'
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12
Git: master all secrets


# ~/.gitconfig
    [alias]
        co = checkout
        br = branch
        ci = commit
        st = status
        unstage = 'reset HEAD --'
logo Git Tips - Laurent RICHARD - 2022 - last update 2022/01/12

> Takes all added files and commits them to the project history. Combined with git add, this defines the basic workflow for all Git users.