Using Git
About
Git is a free and open source distributed version control system – which just means that when you do a git clone (+url of your repository) what you are actually getting is a complete copy of your entire history of that project. This means all your commits! Woot!
Git has a staging area. This just means that if you made 100 new changes to your code, you can break these 100 changes into 10 or 20 or more commits each with their own comments and their own detailed explanation of what just happened!
Some claim Git is better than SVN because it works well even for developers who aren’t always connected to the master repository, as it is available offline.
Branching and merging support are thought to be superior with Git, but it is not as efficient at compressing and storing binary files as other versioning systems.
Make a New Local Repo
Create a new git repo on a local file system by changing into what will become the new repos root directory and enter git int
:
$ cd /path/to/my/local/git/repo
$ git init
Initialized empty Git repository in /path/to/project/.git/
After this, add and commit files as normal.
Make a .gitignore File and Commit it
Create the file .gitignore
in the root directory of the repo.
See the .gitignore example file for more content examples.
$ touch .gitignore
Run git status
to see the state of the repo.
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
Add all untracked files with git add .
.
Now run git status
again to see the added files.
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
Now commit the files to the local repository.
$ git commit -m "Added a .gitignore file."
Add the Local Repo to Github
- Create a new repository in your GitHub.com account. Go to the account reposiroies page (https://github.com/daniel-watts?tab=repositories) and then click the "New" button.
- On the New repository page, name the repo. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
- Click the "Create repository button".
- From the new rpository page, look at the repo URL. It will be something like https://github.com/daniel-watts/gittest.git.
Back in the local repo directory, tell github you want your local repo to get connected to the remote repo URL. You will need to connect with your ssh keys so the start of the URL will be git@github.com:
and NOT https://github.com/
., followed by the github username and repo path daniel-watts/gittest.git
.
$ git remote add origin git@github.com:daniel-watts/gittest.git
Now push your local code up to the remote repo:
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 225 bytes | 225.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:daniel-watts/gittest.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
Now your local repo is set to talk with the remote repo. The previous process only needs to be done once per repo-setup.
Add Files to The Remote Repo
Make a new test.txt
file, add it, and then commit it to the local repo.
$ touch test.txt
$ git add .
$ git commit -m "Added a new test file."
From now on, all you have to do to push locally committed files up to the remote repo is use git push
.
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 254 bytes | 254.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:daniel-watts/gittest.git
371dc98..684ea80 main -> main
Make a new Branch
A new branch will always be made from whatever branch you are currently on. We are currenlt on the main
branch, but just to be sure, you can switch to a branch using the git checkout
commnad. So switch to the main branch first with git checkout main
$ git checkout main
Already on 'main'
Your branch is up to date with 'origin/main'.
Make a new local branch named my-new-branch
with git checkout -b my-new-branch
.
$ git checkout -b my-new-branch
Switched to a new branch 'my-new-branch'
When you make a new branch you will automatically get switched into it. Just to prove it, check with get status
.
$ git status
On branch my-new-branch
nothing to commit, working tree clean
Now tell the remote repo to track this new branch with git push -u origin my-new-branch
$ git push -u origin my-new-branch
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote:
remote: Create a pull request for 'my-new-branch' on GitHub by visiting:
remote: https://github.com/daniel-watts/gittest/pull/new/my-new-branch
remote:
To github.com:daniel-watts/gittest.git
* [new branch] my-new-branch -> my-new-branch
branch 'my-new-branch' set up to track 'origin/my-new-branch'.
Now you can switch back and forth from the main
branch to the my-new-branch
branch with the git checkout
command.
Switch to the main
branch with git checkout main
.
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Now switch back to the my-new-branch
branch with git checkout my-new-branch
.
$ git checkout my-new-branch
Switched to branch 'my-new-branch'
Your branch is up to date with 'origin/my-new-branch'.
You can now do work and commit files to any branch you like. Just be sure to always remember what branch you are currently working on as to not confuse yourself.
MacOS Credentials Management
If you are prompted to enter your username and password before each remote commit, then you are not using stored credentials. Git itself cannot store passwords, but it can use external apps like Keychain to store passwords for it.
Keychain for MacOS
- Run
git config --list
to see the git configurations. - If this line is present:
credential.helper=osxkeychain
, then Keychain has already been setup to hold the actual credentials. - Open the Keychain app. Go-to the “Login” tab. You should be able to find a
github.com
entry.
Make Keychain Manage Git Accounts
- From the git repos root directory run:
git config --global credential.helper osxkeychain
- On the next action taken in the repo, you will be prompted to enter your credentials again, which will be saved to Keychain.
Reset the Password in Keychain
- Simply delete the
github.com
entry from the Keychain app, then re-set the credentials.
On the next action taken in the repo, you will be prompted to enter your credentials again, which will be saved to Keychain.
Use Multiple Github Accounts
- https://stackoverflow.com/questions/2041993/how-to-rename-a-git-repository/2042020
- https://coderwall.com/p/9ub-6a/using-multiple-accounts-with-git-or-github
- https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches
Terminology
repository
A version-controlled collection of files and directories grouped under a name.
clone
A clone is a copy of a repository. And not just the current iteration, a clone contains the entire repo complete with its entire history.
origin
The origin is the original copy of a repository from which all clones are copied. Developers pull and push to this central copy and use it as the system-of-record for the repository.
branch
A branch is a copy of the repository...inside the repository. The files in a branch are kept separate from all other branches, but all branches are kept in one repository.
main
The main, or master branch is the default branch name given to the repository when creating it with git init
.
merge
To combine two branches together in git is called merging. The process is the same as a commit, where files are compared and combined together. Unresolvable combination failures result in a conflict which must be manually resolved before the merge will complete.
conflict
A merge conflict or simply a conflict.
Common Commands
Git Version Number
git version
Check Where Git is installed
which git
Clone a remote origin
Clone a remote repository (origin) located at https://github.com/my-account/example.git
into the directory /path/to/local/gitrepo/dir
on the local machine:
git clone https://github.com/my-account/example.git /path/to/local/gitrepo/dir
Status of the local repo
From within a repository directory:
git status
git status
will display the current branch, its relationship to the origin, any files to be committed and any untracked files.
Create a new branch
Create a new branch in the repo named develop
.
git branch develop
Switch branches
Switch from the current branch to the develop
branch.
git checkout develop
Create and switch to a new branch
Create a branch and switch to it in one line:
git checkout -b develop
Push a new local branch to the origin
Push the new develop
branch to the origin:
git push -u origin develop
Add (stage) untracked files
Add newly created or edited files to the staging area in preparation for a commit.
Add one file at a time:
git add my_file.js
Or add all untracked files:
git add --all
Pull updates from the origin
Make sure you are working with the latest version of the origin by pulling-down any changes from it often, and before committing.
git pull
Commit staged files
Commit all staged files to the local repo:
svn commit -m "A short message about the commit."
Commit message examples:
Fix failing CompositePropertySourceTests
Rework @PropertySource early parsing logic
Add tests for ImportSelector meta-data
Update docbook dependency and generate epub
Polish mockito usage
The following is a valid commit message containing tips for writing good git commit messages:
Short (50 chars or less) summary of changes
More detailed explanatory text, if necessary, describing the _what_
and _why_, not the _how_. Wrap lines at about 72 characters or so. In
some contexts, the first line is treated as the subject of an email
and the rest of the text as the body. The blank line separating the
summary from the body is critical (unless you omit the body entirely).
Further paragraphs come after blank lines.
Write everything in the imperative mood, as if you were commanding
someone. Start the line with "Fix", "Add", "Change" instead of
"Fixed", "Added", "Changed".
- Bullet points are okay, too.
- Typically a hyphen or asterisk is used for the bullet, preceded by
a single space, with blank lines in between, but conventions vary.
If you use an issue tracker, put references to them at the bottom,
like this:
Resolves: #123
See also: #456, #789
Push local commits to the origin
git push