> For the complete documentation index, see [llms.txt](https://www.devops.buzz/public/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.devops.buzz/public/git/cheat-sheet.md).

# Cheat Sheet

## Branch

### Move changes to another branch

```bash
git stash

# To create a new branch
git checkout -b correct-branch

# To use an existing branch...
# git checkout correct-branch

git stash pop
```

### Discard local changes

```bash
git reset --hard origin/master
git pull origin master
```

### Update parent branch

```bash
git checkout feature_abc
git checkout -b feature_def
# --> Someone changed something in feature_abc
git pull origin feature_abc
```

## Clone

### Git clone private repo

```bash
git clone https://myemail%40gmail.com:123456@gitlab.com/my-group/my-repo.git
```

Where:

* `myemail%40gmail.com` means `myemail@gmail.com` and it is your username;
* `123456` is your password;

## Fork

### Keeping a fork up to date

Clone your fork

```
git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git
```

Add remote from original repository in your forked repository.

```bash
cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
```

Updating your fork from original repo to keep up with their changes.

```bash
git pull upstream master
```

### Track forks

<https://techgaun.github.io/active-forks/index.html>

## Manage credentials

### Store credentials permanently

```bash
git config credential.helper store
```

### Store credentials on cache

To avoid providing the same credentials every time, you may enable the Git credentials cache through the following command:

```bash
git config --global credential.helper cache
```

The default cache expiry timeout is 900 seconds (15 minutes) and can be changed with the `--timeout`option as follows:

```bash
git config --global credential.helper 'cache --timeout=300'
```

If you want the daemon to exit early, forgetting all cached credentials before their timeout, you can issue an exit action:

```bash
git credential-cache exit
```

## Module

### Remove submodule

```
mv a/submodule a/submodule_tmp
git submodule deinit -f -- a/submodule    
rm -rf .git/modules/a/submodule
git rm -f a/submodule

# or, if you want to leave it in your working tree and have done step 0
# git rm --cached a/submodule
mv a/submodule_tmp a/submodule
```

#### References

<https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule>

## Push

### You cannot push commits for '<xxx@domain.com>'. You can only push commits that were committed with one of your own verified emails.

```bash
git commit --amend --reset-author --no-edit
git config user.email "your-email@domain.com"
git config user.name "Your Name"
git push origin your-branch
```

## Tag

### Create

```bash
# Create tag
git tag -a v1.0 -m "Short description"
# List tag
git tag
# Show tag
git show v1.0
# Push tag
git push origin v1.0
```

### Delete

```bash
git tag -d v1.0
git push --delete origin v1.0
```
