Git

Update ("svn update")

git fetch
git pull

Commit ("svn commit")

Commit some changes

git status
git add file1.txt
git add folder2
git commit -m "<commit message>"

Commit all changes

git commit -am  "<commit message>"

Push changes remotely

git pull --rebase
git push

You fucked up ?

You want to reset your master to the state of origin's

git checkout master
git fetch origin
git reset --hard origin/master

Your commit was wrong/incomplete

Cancel your last commit (keeps the file changes)

git reset HEAD~1

Modify your last commit (keeps the file changes)

// do some changes, then...
git commit --amend
// do some changes, then...
git commit --amend -m "If you want to change the message as well"

Remove a file you accidentally commited and pushed

git reset --soft HEAD^1
git rm --cached path/to/the/file.txt
git commit -m "The commit message"
git push -f

Your changes to the code were wrong ("svn revert")

Revert local changes and remove all non-versioned files/folders

You will lose file modifications.

git reset --hard
git clean -f -d

Revert local changes to one file

You will lose file modifications.

git checkout -- your_file.ts

You want to work on something else for now?

Put your current changes away

git stash

List the stashes

git stash list
git stash list --date=local

Get back your last stash

git stash pop

Drop stash #3

git stash drop stash@{3}

Show the content of stash

List modified files

git stash show stash@{3}

Diff

git stash show -p stash@{3}

See last push

git log

List the files who changed between two commits

git diff --name-only first_commit second_commit

Authentication

Change author

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Then restart the shell.

Store credentials in a plain-text file

git config --global credential.helper store

The credentials will be stored in plain text in ~/.git-credentials

Change the saved credentials

Windows' “Credential Manager”. Remove the account.

Remote

List remotes

git remote -v

Add a new remote

git remote add upstream https://github.com/cloudtrust/keycloak-rest-api-extensions.git

Checkout PR #XX in a temporary branch

git checkout master
git pull upstream master
git ls-remote upstream
git fetch upstream refs/pull-requests/XX/merge
git checkout FETCH_HEAD
git checkout -b pull_request_XX

Work on somebody else's branch

git remote add lpa ssh://git@git.elcanet.local:7999/~lpa/2019.lpa.kc.paper-card.git
git fetch lpa cloudtrust-1009-add-self-service
git checkout cloudtrust-1009-add-self-service
... do your commit
git push lpa cloudtrust-1009-add-self-service

Feature branches

Create your new feature

git flow feature start JIRA-1234_some_text

Then work on it normally (git add, git commit…). After your first commit (or later), you can push your branch remotely :

git push -u origin feature/JIRA-1234_some_text

Each time you do a push, it shows you on what URL to click to create a PR.

After creating your PR, you can go back to develop :

git checkout develop
git pull --prune

Once your PR has been accepted, and the feature has been merged, you can then delete your local branch, and go back to

git branch -d feature/JIRA-1234_some_text

Branches

Create a branch with the current changes on master

If no commit were done...

git checkout -b newBranch

If commits were done...

git branch newBranch
git reset --hard HEAD~3  # (2)
git checkout newBranch

Push to a branch

git push -u origin nom_branche

Create a branch

git checkout -b nom_branche
git push origin nom_branche

Change branch

git fetch
git checkout nom_branche

Merge changes of branch B into A

git checkout B
git pull
git checkout A
git pull
git merge B
git push

Delete a branch from the server

git push origin --delete branch_name

Delete a local branch

git branch branch_name -D

Go back to master

git stash // You will lose all your uncommited changes!
git checkout master

Create a new branch starting from a commit

git checkout -b name_of_branch number_of_commit

Pull new commits from upstream repository

git pull <address_of_upstream_repo> master

Get your branch up-to-date with master

We created branch A from master. Then, after the branch creation, changes have been done to master. We want to branch to still start from the tip or master.

git checkout master
git pull
git checkout A
git rebase origin/master
git push -f
Print/export