Git Good
A guide to git
for Carter to refer people to.
Adding code to git
Once code has been written, it must be staged, committed, and pushed.
First, run git status
.
Verify that nothing is listed in Changes to be committed
and the files you have changed are listed in Changes not staged for commit
or Untracked files
.
If something is listed in Changes to be committed
, run git reset
.
Next, stage changes.
- To stage all changes by running
git add --all
. - To stage just one file, run
git add [filename]
. - To stage part of a file, run
git add -p [filename]
. - To remove just one file (i.e. when not using
git add --all
), rungit rm [filename]
.
Then, commit changes. Run git commit
for a long commit message, or git commit -m "[message]"
for a short message.
Finally, run git pull
(in case there are remote changes to your branch that need to be merged), followed by git push
.
If this is your first time pushing this branch, run git push --set-upstream origin [branch name]
.
Branch Managing
In a shared repository, or a repository with a stable working version, all development should be done in branches, then merged.
Branch Creation
The following steps will create a branch from the main branch.
If you wish to use another branch, substitute [main branch]
for whatever branch you wish to base the new branch on.
git checkout [main branch]
git pull
git checkout -b [new branch]
Branch Merging for the Individual
If you are not using a pull request system, use the following steps to merge a branch ([new branch]
) back into the main branch.
If you wish to use another branch, substitute [main branch]
for whatever branch you wish to base the new branch on.
git checkout [main branch]
git pull
git checkout [new branch]
git merge [main branch]
- Resolve (edit, add, commit, but no need to push) merge conflicts with your favorite text editor.
git checkout [main branch]
git merge [new branch]
(No merge conflicts because they are already resolved.)git push
git branch -d [new branch]
Branch Merging for the Team
If you are using a pull request system, use the following steps to prepare a branch for a pull request.
If you wish to use another branch, substitute [main branch]
for whatever branch you wish to base the new branch on.
git checkout [main branch]
git pull
git checkout [new branch]
git merge [main branch]
- Resolve (edit, add, commit, and push) merge conflicts with your favorite text editor.
- Open pull request via shared git repository host.
Deleting local changes
Warning: this could lead to loss of code.
To reset a local repository to the latest commit, run git reset --hard
.
Creating a git repository
To create a local repository, run git init
.
Consult the documentation of a git repository host to synchronize with them
(this usually involves git remote add origin [url]
followed by git push --set-upstream origin [main branch]
).