Git & GitHub
git init
initializes a local git directoryrm -fr .git
removes a local git directorygit status
status of staging areagit log
show log of changesgit diff filename.txt
see recent changes made to code
Navigate
git branch
lists all branchesgit checkout -b branchName
creates a new branch and moves into itgit checkout branchName
move into branch
Commit
git add <filename>
add file to staging areagit add .
add all files that aren’t currently stagedgit reset
removes all files from git status staging areagit commit -m “change message”
commits files locallygit commit -m “title” -m “longer description”
add a description of changesgit commit --amend -m "change message"
amend a commit and change the commit messagegit commit --amend --no-edit
amend a commit without changing the commit message
How to write a good commit message
Local branches
git checkout main
thengit branch -D <branchname>
delete local branch
Remote branches
Origins
git remote add origin <repository-url>
add remote origingit remote remove origin
remove remote origingit remote set-url origin url
update remote origingit remote -v
view connected remote repositories
Pull and push
git pull origin main
pull remote main branch into local branchgit push origin branchName
push branch to the remote repo feature branch orgit push -u origin branchName
push feature branch to the remote repo and set it as the upstream branch, thengit push
will push to that branchgit checkout -b new-branch origin/branchname
checkout remote branch, good for testing PRs
Problems / Solutions
Wont push to a remote repo that has a README
git fetch --all
git rebase origin/main
Merge conflicts
git pull origin main
git checkout feature-branch
git rebase main
# fix conflict
git add .
git rebase --continue
# repeat until conflicts are resolved
git push origin feature-branch --force