Git
Git copied to clipboard
Git - basic commands
- Common Commands
- More commands
- Create repository
- Clone repository
- Submit changes
- Rebase
- Revert changes
- Stashing
- Merge branch
- Troubleshoot
Common Commands
All commands should be run without the <> brackets
git logshows commit ids. The latest commit is at the top.git log -p- lists the changed files
git checkout -b developchecks out a new branch called "develop"git checkout init_devicesswitches branch to init_devicesgit checkout <commit_number>goes to a specific commitgit checkout -b 1234 origin/mainlinechecks out a new branch called "1234", that "tracks" origin/mainline, meaning "local branch has its upstream set to a remote branch" (Reference). Here's more info: Tracking branches
git statuslists new or modified files not yet committedgit diff <commit_number>compares current code to any commitgit add *adds all files to "Staging". Ready for commitgit add <file>adds a specific file to "Staging". Ready for commit.
git commit -m "Put Message Here"commits locallygit commit --amend" updates commit by amending your new changes to it. Must run after "add" command.git commit --amend -m "an updated commit message"updates the commit, and its commit message.
git branchsays which branch we're ongit pushpushes code to the branch we're ongit push origin <my_branch>pushes to specific branch. I think origin is a keyword. Reference
git pullpulls all changed filesgit cherry-pick <commit_number>adds a specific commit to your branch
More commands
git rm <file>removes file from server (Must then do a commit, then push)git ls-files | xargs wc -lcounts # of lines of code in a github repo. Link to Repogit config --global --editEdit git commit signaturegit branch -d the_local_branchRemoves local branch. Linkgit rebase -i HEAD~3combines the last 3 commits into 1 commit. Called Squashing Commits.git reset filename.txtundoes adding a file.
Create repository
- In command line, type
git init <project name>to create a folder that's a Git repo. - Add the repository to Github Desktop and push it through Github Desktop.
Clone Repository
git clone https://github.com/noahprince22/GridWorldMDP.git- clones repository and puts into same folder that command is run from. Repository will be in GridWorldMDP foldergit clone https://github.com/noahprince22/GridWorldMDP.git putIntoThisFolder- same as above but puts GridWorldMDP repository into putIntoThisFolder.
Submit changes
git statusto list new or modified files not yet committedgit add <file>git commit -m "<commit message>"git pushto push the code to the repository. If it fails since code needs to be pulled first, do:git pull- this will do agit fetchand agit merge. (assuming no merge conflicts, go onto next steps)git push
Revert changes
git revert <commit_number>- then
:wqto save in VIM - then rebase
Rebase
Rebasing with Merge Conflicts
- Commit my code on mainline
git pull --rebase- If merge conflicts, read the super-helpful tips in terminal. Basically just
- Do a
git diffto resolve the merge conflicts I have - I think next do a
git rebase --continue
- Do a
Head is not on main branch - Put changes on top of head
Somewhat useful tutorial for git pull
- Do a
git logto get the commit number corresponding to the changes you made. Save it for step 5. - Get the branch we need. Try:
git pull origin <branch_name>orgit checkout <branch> - Do
git branchto make sure we're on correct branch - Do
git reset --hard <commit_number>using the actual 7-digit (or full) commit number, to put changes on top of branch we just switched to. Use commit number from step 1. - If not on latest commit, do a
git pull --rebase. Note: this command doesn't create a merge commit, so it makes other people's code diverge from what they had.
Stashing
git stash- moves your changes to a stash (a location where changes can be saved)git stash save "Custom Message"- stashes changes, with custom message.git stash apply- applies saved changes to your branch. Code also stays in stash.git stash pop- applies saved changes to your branch. Code is removed stash.git stash show stash@{1} -p- shows diff for stash@{1} (the 1st entry in the stash). Remove the-pto get abbreviated diff.git stash list- shows all stashes.- More stash commands, and even More stash commands
Stash topmost commit
git reset HEAD^- basically like an uncommitgit stash(this may not stash new files. Maybe try "tracking" the new files to see if this works)
Merge Branch
To merge 1 branch into another, go to the "giving branch" and do a git pull. Then go to receiving branch, and run 1 of the following merge commands:
git merge <branch_name>- Run this from receiving branch. More info heregit merge <commit_number_from_another_branch>- Merges another branch (up to the commit number) into this branch.
Create new branch and upstream it
git checkout -b myNewBranch- creates branchgit push --set-upstream origin myNewBranch- tells the remote server that a new branch has been created locally, so that it can recreate the same branch
Troubleshoot
"Checkout" or "Pull" not working
Common mistake is to modify files on local machine, and then try to do a "checkout" or "pull". Problem is the checkout/pull will overwrite what we have. If we do want to OVERWRITE our files, we can erase our changes by typing git reset --hard HEAD. Then we can checkout/pull without problems, which gets us the remote files.