linux:git
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| linux:git [2025/09/12 09:57] – created v1ctor | linux:git [2026/02/13 18:52] (current) – [SQUASH] v1ctor | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== GIT ====== | ====== GIT ====== | ||
| - | ==== Check out remote branch | + | ==== CHECK OUT REMOTE BRANCH |
| <code bash> | <code bash> | ||
| Line 8: | Line 8: | ||
| // 3. Move your working directory to that branch | // 3. Move your working directory to that branch | ||
| </ | </ | ||
| + | --------------- | ||
| + | ==== DELETE REMOTE BRANCH ==== | ||
| + | Your local Git keeps a list of remote branches under '' | ||
| + | <code bash> | ||
| + | git branch -a | ||
| + | git fetch -p # Remove any stale references from your local list that no longer exist remotely | ||
| + | </ | ||
| + | |||
| + | Delete remote branch: | ||
| + | <code bash> | ||
| + | git push origin --delete < | ||
| + | </ | ||
| + | --------------- | ||
| + | ==== ACCIDENTALLY COMITED ON MASTER ==== | ||
| + | |||
| + | If changes were accidentally committed on '' | ||
| + | <code bash> | ||
| + | git switch -c new-branch | ||
| + | git checkout master | ||
| + | |||
| + | # Cleaning master - Option 1 | ||
| + | |||
| + | git reset --hard origin/ | ||
| + | # but it still exists on new branch | ||
| + | # Resets to match the remote branch exactly. | ||
| + | # Used to discard all local commits since last fetch/pull | ||
| + | |||
| + | # Cleaning master - Option 2 | ||
| + | |||
| + | git reset --hard HEAD~1 | ||
| + | # Used to undo the last local commit (or a few with HEAD~2) | ||
| + | |||
| + | # Bonus - local history in compact form | ||
| + | |||
| + | git log --oneline master | ||
| + | </ | ||
| + | --------------- | ||
| + | |||
| + | ==== SQUASH ==== | ||
| + | |||
| + | Scenario - you would like to " | ||
| + | |||
| + | **Step 1** - Find the **base commit** where your branch diverged from main | ||
| + | <code bash> | ||
| + | git merge-base master HEAD | ||
| + | </ | ||
| + | |||
| + | **Step 2** - Start interactive rebase from that commit: | ||
| + | <code bash> | ||
| + | git rebase -i abc123 | ||
| + | </ | ||
| + | |||
| + | **Step 3** - In editor which will open, change all but the first pick to squash or just s: | ||
| + | <code bash> | ||
| + | pick abc123 My first commit | ||
| + | squash def456 My second commit | ||
| + | squash ghi789 My third commit | ||
| + | </ | ||
| + | |||
| + | **Step 4** - Editor will open again for a commit message. Edit as you wish. | ||
| + | |||
| + | **Step 5** - Push with force: | ||
| + | <code bash> | ||
| + | git push --force | ||
| + | </ | ||
| + | |||
| + | **Step 6** - When trying to pull after these changes, you might need to rebase: | ||
| + | < | ||
| + | git config pull.rebase true | ||
| + | git pull | ||
| + | </ | ||
| + | |||
| + | ==== CHANGE COMMIT MESSAGE ==== | ||
| + | |||
| + | If the commit has been already pushed to the upstream, and it's the latest commit: | ||
| + | |||
| + | <code bash> | ||
| + | git commit --amend # edit the commit msg | ||
| + | git push --force | ||
| + | </ | ||
linux/git.1757671045.txt.gz · Last modified: by v1ctor
