Skip to content

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?

git rebase -i HEAD~n

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?

git checkout target-branch
git merge source-branch
  • Replace target-branch with the branch you want to merge into and source-branch with 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
git remote set-url origin <YOUR_NEW_FORKED_URL>
  • second: if the first one does not work, there could be several reasons:

    1. you need to have permission
    2. your pwd need to be in the parent folder of .git folder
    3. try command git remote -v to 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

git pull
git branch --set-upstream-to=origin/main main