- Create or fork a repo from GitHub.
- Clone the repo with
git clone <https_url_to_rep>
orgit clone <ssh_url_to_rep>
.
- This command copies the repo from the url repository to your local system.
- It will, by default, set the existing branch to be that of the cloned repo (usually
master
ormain
). - Verify all branche names and the current working branch with
git branch -a
.- Switch to a new branch with
git checkout -b <new_branch>
assuming no file changes are happening on the current working branch.- New branch name can start with something like
dev
,fixes-2
, or link to a ticket or issue nameISSUE-345
.
- New branch name can start with something like
- Switch to a new branch with
- List credentials with
git config -l
.
- This will show your
git
credentials that have been set on your local machine (like email, username, settings, and so forth). - These two commands below set credentials on your local machine.
- Use
git config --global user.name "my name"
to set your user name - Use
git config --global user.email "my email"
to set your user email
- Use
- Create an alias to a remote repo using
git remote add <remote_short_name> <remote_url>
, for example, usegit remote add upstream https://www.github.com/org/branch
to link to a remote branch called "upstream". - Take a breather and ensure the repo/branch/changes are where they should be using
git status
.
git status
shows recent local changes with respect to files that differ to theHEAD
branch, for example, a remote branch.
- Run the command,
echo "Taking care of business\n\t-by Bachman-Turner Overdrive" | tee -i lyrics.txt
, to create a basic change on your local system. - Run
git add --all
to add the local change (a new file). This is called "staging changes for commit". - Commit the change using
git commit -m "Taking care of business"
.
- Ensure the change is logged with
git log -3
(checks the log for the last three commits).
- Verify the changes against a specific branch like
git diff upstream/main
.
- Add the switch
--name-only
to the previous command to see just the file name with changes.
- Look good? Then use
git push origin dev
to push to your remote repo on thedev
branch. Use the GitHub to create a Pull Request with respect to another branch.
- Do
git push
to the remote repo it will ask for your GitHub username and password unless they are stored. See Bonus Commands to see how to use credential helpers.
- Checking in changes from the remote to your local machine using
git fetch upstream
and thengit merge upstream/main
.
- See Bonus Commands for steps on how to use the stash to make a change to a separate branch when there are pending changes on the current working branch.
- Sometimes the current working branch must be reset to a local or remote branch's state. This can be done using
git reset --soft <commit_hash>
to preserve current changes on the local branch, orgit reset --hard <commit_hash>
to delete current changes on the local branch and check in changes from the other commit.
git mergetool -t <text_editor_name>
will set the default text editor.git remote -v
will show all remote repo's.git remote update
to just update the remote changes but not fetch them.git config --local credential.helper store
to store GitHub or other repo credentials.git stash
is awesome. In a nutshell, it works as a local database where you can save changes staged for commit- View the history using
git stash list
. - Pop off the history stack using
git stash pop stash@{X}
where "X" is a historic id on the stack. - Drop changes using
git stash drop stash@{X}
. - Exmple usage is to save changes but not commit to a current working branch:
- Start with
git add --all
and thengit stash save "some message"
. - Follow-up with a
git checkout -b <new_brach_name>
and thengit stash pop
to carry-over these changes without making a commit to the prior working branch.
- Start with
- View the history using
git rebase -i "remote/branch"
- Rebasing is simple, in theory, but in practice it can lead to merge conflicts which are files that have similar changes on different branches (like the current working branch and a branch on a remote repo). It also rewrites commit history.
- Using
git rebase -i HEAD~X
one can use their default text editor to resolve change conflicts between commits.- Repeat the step above until you can do a
git rebase --continue
or back-out withgit rebase --abort
.
- Repeat the step above until you can do a