Common github CLI
| Command | Description | Example |
|---|---|---|
git rebase |
Applies changes from one branch onto another by replaying commits | git rebase main |
git merge |
Integrates changes from one branch into another | git merge feature-branch |
git remote set-url |
Changes the URL of a remote repository | git remote set-url origin https://github.com/username/repo.git |
git remote |
Manages remote repositories | git remote -v (lists all remotes) |
git clone |
Creates a copy of a repository | git clone https://github.com/username/repo.git |
git pull |
Fetches and merges changes from a remote | git pull origin main |
git push |
Uploads local commits to a remote | git push origin feature-branch |
git checkout |
Switches branches or restores files | git checkout -b new-branch |
git commit |
Records changes to the repository | git commit -m "Add new feature" |
git status |
Shows the working tree status | git status |
Common git scenarios
what if I want to merge all my commit history into one commit?
where n is the number of commits you want to squash. This will open an interactive rebase editor where you can choose
to squash commits into one.
- In the editor, change the word "pick" to "squash" (or "s") for all but the first commit you want to squash.
- Save and close the editor.
- Git will then combine the commits into one. You may be prompted to edit the commit message for the new squashed commit.
how to merge two branches into one?
- Replace
target-branchwith the branch you want to merge into andsource-branchwith the branch you want to merge from.
How to switch to a newly forked repo?
first, try to set repo to new url
- try this first, if it does not work, then try the second one
-
second: if the first one does not work, there could be several reasons:
- you need to have permission
- your
pwdneed to be in the parent folder of.gitfolder -
try command
git remote -vto check if the url is correct- if there are multiple remotes, remove all remotes and add the new one
[zi@ieng6-203]:~:377$ git remote -v main https://github.com/ucsd-cse120-sp25/xxx.git (fetch) main https://github.com/ucsd-cse120-sp25/xxx.git (push) main https://github.com/ucsd-cse120-sp25/xxx.git (push) [zi@ieng6-203]:~:384$ git remote remove main [zi@ieng6-203]:~:385$ git remote -v [zi@ieng6-203]:~:386$ git remote add origin <YOUR_NEW_FORKED_URL> [zi@ieng6-203]:~:387$ git remote -v origin <YOUR_NEW_FORKED_URL> (fetch) origin <YOUR_NEW_FORKED_URL> (push)
then, set the upstream