git: useful commands
Basics
git status
git pull
git add <filename>
git add .
git commit -m "Example commit message"
git push
What if I try to do a git push, and I get an error?
git pull
What if I do a git pull, and I pull in changes I didn't want?
git reset --hard HEAD@{1}
Pushing changes to free-speech and using branches
The repository blab-lab/free-speech is restricted -- you cannot push changes directly to the master branch. This protects the master branch from bad code changes which would mess up other people's workflows, since free-speech is used by many people for many different tasks.
Instead, you must push changes to another branch, then you can request to merge that branch with the master branch. This is called a "pull request," or "PR". PRs must be reviewed by another person before they can be merged. See this link for a conceptual understanding of branches: https://www.toolsqa.com/git/branch-in-git/
(Preferred option) If you have not edited any files yet, make a new branch and switch to it. Make your changes in that branch, then commit and push.
git checkout -b newBranchName
- [edit whatever files you need]
- [stage your edits with
git add
] git commit -m "comment"
git push
- Make a pull request for your branch newBranchName (eg,
gh pr create --web
)
If you have edited files but have NOT committed the files yet, use git stash
to move those changes to a new branch, then commit and push from that branch.
git stash
#puts all changes away - allows you to switch branchesgit checkout -b newBranchName
# makes a new branch titled newBranchName and switches to that branchgit stash pop
#pulls your changes back out- [commit any/all files you want to push]
git push
- Make a pull request for your branch newBranchName (eg,
gh pr create --web
)
If you have ALREADY committed the files to the MASTER branch, you need to first move your changes to a new branch, then "reset" the master branch on your local (local computer).
git checkout -b newBranchName
#makes newBranchName and switches to it. Carries over current commits from master branchgit checkout master
#switches back to master branchgit reset --hard head~10
#reverts the master branch to what it was 10 commits ago (presumably you haven't made more than 10 commits before trying to push)git pull
#restores the master branch to its current state on the servergit switch newBranchName
git push
- Make a pull request for your branch newBranchName (eg,
gh pr create --web
)
These steps leave you on the new branch you just created. You can switch back to master or another branch (e.g., git switch master
) if you need to (in which case you won't see your changes until your new branch is merged).
Git Etiquette
Since we are working with a common codebase, we need to follow some simple conventions to keep things running smoothly.
Don't leave uncommitted changes on shared computers
When you finish working on a lab machine, the repositories should be in a clean state. Make sure you commit (or trash) any changes so the next person to use the machine doesn't run into conflicts when they try to pull in changes.
Pull regularly
You should git pull
on the main lab repos (current-studies
, free-speech
) at least once a week. This should be totally painless if you've followed the above rule of not leaving uncommitted changes sitting around!
Outside of those regularly-scheduled pulls, you should also pull down a repo before you start editing any code in that repo.
Repositories are for code (or code-like files) only
See this KB doc for more info, but basically, many types of files should not be put in a git repo:
- data (.mat files)
- images (figures, pictures)
- sound files
- other files such as Powerpoint slides or Word documents
- Matlab Live Scripts (.mlx) -- these are actually binary files. If you want to keep track of a Live Script, save it as a regular .m file and add that to the repo.
These types of files can go on the Waisman /smng/ drive or M:/ drive instead.