Learn the basics of local git
Setup
$ brew install git
Create a new repository
git init
Check out a repository
Create a working copy of a local repository by running the command
git clone /path/to/repository
When using a remote server, your command will be
git clone username@host:/path/to/repository
In case of GitHub repo, it will be
git clone https://username@github.com/username/repository.git
Understand workflow
Your local repository consists of three “trees” maintained by git.
- Working Directory, which holds the actual files.
- Index, which acts as a staging area.
- HEAD, which points to the last commit you’ve made.
Add and commit
Propose changes (== add it to the Index) using
git add <filename>
git add *
For example,
echo "# gittest" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/username/repository.git
git push -u origin master
If you added something by mistake, you can remove it from the index with:
git rm --cached <filename>
This is the first step in the basic git workflow. To actually commit these changes use
git commit -m "Commit message"
Now the file is committed to the HEAD, but not in your remote repository yet.
Pushing changes
Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute
git push origin master
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <server>
In case of GitHub repo, it will be
git remote add origin https://github.com/username/repository.git
Now you are able to push your changes to the selected remote server.
Branching
Isolating feature development from each other. The master branch is the default branch. Use other branches for development and merge them back to the master upon completion.
<img src="/assets/images/0424git/branching.png" width="800">
Create a new branch and switch to it
git checkout -b <branch>
Switch back to master
git checkout master
A branch is not available to others until you push the branch to your remote pository
git push origin <branch>
Update & Merge
To update your local repository to the newest commit, execute the command below in your working directory to fetch and merge remote changes.
git pull
To merge another branch into your active branch (e.g. master), use
git merge <branch>
git push origin <branch>
Log
Each commit has a commit id. You can get it by looking at the log.
git log --author=bob
git log --pretty=oneline
git log --graph --oneline --decorate --all
git log --name-status
- To see only the commits of a certain author
-
To see a very compressed log where each commit is one line
-
To see an ASCII art tree of all the branches, decorated with the names of tags and branches
- See only which files have changed
Tagging
It’s recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag like below. If you want to include a description with your tag, add -a to create an annotated tag.
git tag <tagname> 1b2e1d63ff -a
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag.
This will create a local tag with the current state of the branch you are on. When pushing to your remote repo, tags are NOT included by default. You will need to explicitly say that you want to push your tags to your remote repo:
git push origin --tags
Or if you just want to push a single tag
git push origin <tag>
Referernces
- Simple guide
- Basic git commands with examples
- GitHub git cheat sheet
- Linux command
- Learn git branching
Errors
fatal: remote origin already exists.
The error fatal: remote origin already exists
is caused when you attempt to create a link to a remote repository called “origin” when a remote link with that name is already configured.
The solution is to update the URL of the remote repository with the name “origin” to the URL of the remote repository you want to add, instead of trying to create a new remote repository with that name.
You can do that with this command:
git remote set-url origin https://github.com/your/repository