RaenonX / Madison-Metro-Sim

Madison Metro System simulator. (UW Madison 2020 Fall CS 638 Project)
2 stars 2 forks source link

git commands #12

Open RaenonX opened 3 years ago

RaenonX commented 3 years ago

Concepts

You are working at your local computer. The code is also stored remotely at the origin.

You pull the code from the server / origin; you push your code from the local to the origin.

The most recent commit at the current branch is HEAD.

It's recommended to either commit or stash before you execute any of these commands except git commit, so you may save time on solving conflictions (if any) and/or reduce the risk of losing your work.

I personally recommends PyCharm as it provides some robust git controls. Despite of this, there are still many cases that you will find terminal/cmd/shell/bash useful than the IDE. So I will say, get an IDE for commit, solving conflictions; use terminal for other things.

Basic Commands

Commit

git commit -m "YOUR_MESSAGE"

Push

git push

Pull

git pull

Advanced Commands

Force push

git push -f

Rebase interactively

git rebase -i <REF>

<REF> can be (not limited to):

Amend commit

git commit --amend

Rebase

git rebase <BRANCH_NAME>

Usual Workflow

Modify the commit that you've already pushed

(This could happen quite frequently)

Scenario

You pushed a commit, and found that despite you passed the tests locally, GitHub Actions tests did not pass, and it's clear that you are the one responsible for it. After you applied some fixes, you want to amend the pushed commit.

Command

git commit --amend
git push -f

Explanation

Squash old commits

Scenario

You find that after you committed A, B and C, A and B are actually the same thing, and it should be squashed into A. However, all of these commits were already being pushed to the origin.

Command

git rebase -i HEAD~3

Then after your text editor pops up with the rebasing commands, change it from:

pick aaaaaaa A
pick aaaaaab B
pick aaaaaac C

to:

pick aaaaaaa A
squash aaaaaab B
pick aaaaaac C

Save the file, close the editor, then the text editor will pop up again, prompting you for the new commit message of aaaaaaa with something similar to this:

# 1st commit message

A

# 2nd commit message

B

Change it to what you want. Assuming we are going to change the new commit message as "AAAAA":

AAAAA

Save it then close the editor.

The rebase will then continue.

After you completed the rebase, run this:

git push -f

Explanation