git commands

devfish·2023년 1월 9일
0

git

목록 보기
2/2
post-thumbnail

img source

Extra work

  • combining branches: merge, rebase, ff(fast forward)
  • need to link all the pics i downloaded indiscriminately

set up vscode as default editor

  • git config --global core.editor "code --wait --new-window"

Create Local Git Repository

# create directory and files
mkdir ~/bootcamp/VSCode/foldername
cd ~/bootcamp/VSCode/foldername
mkdir my-app
cd my-app
touch index.html style.css
git init #add git tracker

# git add <path>/. 
git add . #. means everything in current path
git add index.html # add individual files
git add style.css

git status # check update to staging area
git commit -m 'commit msg' #commit to local repository
git branch -M main #setup a branch

Create Remote Repository

git remote add origin git@.... #connect remote repo: git remote add <name> <url>
git remote -v # check the connection
git push -u origin main #git push -u command

set up upstream branch

git push -u <remote> <branch name>

  • The first-time -u option creates a persistent upstream tracking branch with your local branch (it means set upstream)
  • After that, you can just type a shorter command: >git push
  • You can skip the branch name and write, git push -u origin because this pushes the current branch to a matching branch of the same name

change upstream branch

Track a different branch than the one you just set up

  • git branch -u <remote/branch name>

check which remote branches are tracking which local branch

  • git branch -vv

pull and update one file in git

for those who want to pull changes from the remote but only apply changes for one file

fixing >git push errors

  • “the tip of your current branch is behind its remote counterpart” means that there have been changes on the remote branch that you don’t have locally.

  • If you created a README.md file when creating the remote repo, this will inevitably happen. To fix this, you need to pull and push again

    • git pull origin main -> git push --set-upstream origin main
      • git push --set-upstream <remote-name> <branch-name>: sets up upstream branch (creates a new branch in remote repository and sets it up as the main branch for git push)
      • --set-upstream === -u
    • patch solution: git push -u origin +main: force pushes this specific branch
      • to force every branch: git push --force
  • When you have divergent branches and need to reconcile them

    • git config pull.rebase false to merge
    • git pull origin main --allow-unrelated-histories
      • The fatal: refusing to merge unrelated histories Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other’s existence and have mismatching commit histories)
  • Here are many different ways of fixing this error, depending on your needs (warning: TLDR..)

canceling commits

  • git reset HEAD^: cancels the most recent commit (used only when you didn't push it to the remote directory)
    • If you are not familiar with this notation, “HEAD~1” means that you want to reset the HEAD (the last commit) to one commit before in the log history (head - 1)
    • git reset --soft HEAD^: you will not lose the uncommitted changes you may have
    • git reset --hard HEAD^: discards all changes in the working dir
  • git log --oneline: shows all your commits and commit ids neatly
  • git reset <commitid>: returns it to that specific commit

reset vs revert

Unlike the git reset command, the git revert command creates a new commit for the reverted changes. The commit where we reverted from will not be deleted. (ref)

You should use git reset when working on a local repository with changes yet to be pushed remotely. This is because running this command after pulling changes from the remote repo will alter the commit history of the project, leading to merge conflicts for everyone working on the project.
.
git reset is a good option when you realize that the changes being made to a particular local branch should be somewhere else. You can reset and move to the desired branch without losing your file changes.
.
git revert is a good option for reverting changes pushed to a remote repository. Since this command creates a new commit, you can safely get rid of your mistakes without rearranging the commit history for everyone else.

exit git.log mode

If you get stuck after writing git push --help for instance, exit by typing q or z (Ctrl+C won't work!)

merge vs. rebase, ff

merge

rebase

git clone

git clone <url> <dir> - links it directly

profile
la, di, lah

0개의 댓글