Summary of useful Git commands
Configuring:
Set up your username and email
git config --global user.name "Xiaomeng Jin"
git config --global user.email "xiaomeng.jin@rutgers.edu"
Branches:
Branches are an important part of working with Git. Any commits you make will be made on the branch you're currently “checked out” to. Use git status to see which branch that is.
git branch [branch-name] #creates a new branch
git checkout [branch-name] # switch to the specified branch and updates the working directory
git merge [branch] # combines the specified branch's history into the current branch.
git branch -d [branch-name] #deletes the specified branch
Create repositories:
Start out a new repository:
cd my_project
git init
Or clone a repository using git clone
git clone https://github.com/rcaes2023/assignment_1_python-MazvitaChikomo.git
Or clone a repository using GitHub Command Line OWNER/REPO syntax.
gh repo clone rcaes2023/assignment_1_python-MazvitaChikomo
cd assignment_1_python-MazvitaChikomo
If you want to get a repository that you don't have permission to push to, you can fork the repository.
gh repo fork cli/cli
Make Changes
Browse and inspect the evolution of project files
git status # tells you which branch you are at, what files are staged, which ones have been modified, are new,...
git log # view the commit log
git diff # view file content differences
Version control
git add <filenames> #Snapshots the file in preparation for versioning
git commit -m "your brief commit message goes here" #Records file snapshots permanently in version history
Synchronize changes:
git push #uploads all local branch commits to GitHub
git pull # updates your current local working branch with all new commits from the corresponding remote branch on GitHub.
Basic GitHub workflow:
- clone your local repo with
gh repo clone <REPO>
, - make your changes and stage them with
git add <filenames>
, - commit your changes with
git commit -m "your brief commit message goes here"
, and - upload the changes to GitHub with
git push