This is a practical git tutorial for beginner, focus on the commands that are used daily by every programer.
Set Up Name and Email Info
First, you need to setup a default name and email. Else, git won't let you commit.
Everytime you commit, your user name and email is in part of the commit info. This allows git to show who did what, when working in a team. That's why user name and email are required. (you can use random name and email if you want)
Type the following in a terminal.
# set up default name and email git config --global user.name "Mary Lee" git config --global user.email "email@example.com"
the above will create a file at
~/.gitconfig. You can run these commands again to change. (you can also edit the file directly.)
Create a Project
cd project_dir→ cd to the directory of your source code.
git init→ create a
.gitdir in the current dir. This dir holds all git's data about the current project.
git add .→ add current dir (and subdir) files into git staging area.
git commit -m"first commit"→ commit to local repository.
Important: when using git, you should always cd to your project's directory first.
# cd to the dir containing your code cd my_project # initialize. This creates a .git dir. git init # add all files to “staging area” git add . # commit to local repository. git commit -m"my first commit"
Git will create a dir named
.git in the same dir. This dir is used by git for all its info, including its database.
Note: as of , Adding 20k files takes about 6 minutes. Commiting 20k files takes 6 minutes.
Clone a Project
Clone a projet means making a copy.
cd to a directory where you want the new project dir to be, then:
git clone source_path_or_url
# get a copy of git source code (~10MB) git clone git://git.kernel.org/pub/scm/git/git.git
# get a copy of the js jquery source code git clone https://github.com/jquery/jquery.git
Now, what you need to learn is about committing files and sync files. Before we do that, here's a overview of git commands.
Most Frequently Used Git Commands
The following are most frequently used git commands. Don't worry if you don't understand it. We'll have Detailed tutorial with step-by-step instruction in this tutorial.
Those marked 🔷 are use daily.
- Create a git repository in current dir. This will create a directory named “.git” in current dir. The “.git” directory holds all git's internal data.
git clone url_or_path
- Clone a new git repository from url_or_path to current dir. “clone” basically means copy. The newly created dir will have the same name as project name of the remote repository at url_or_path. If a non-empty directory of the same name already exist, git will complain. The url_or_path can be a local directory/folder path that contains “.git”.
Once you have a project, you edit files in that dir to make changes, then you need to “add” to a staging area, then “commit”. (these are done to your local repository.)
Commit Changes to Local Repository
- Show current status. (that is, list changed files.)
git add .
- Add changes from current dir to local repository's staging area.
git commit -m"message"
- Commit changes to the local repository. The message is a short description of changes you made.
Sync with Remote Repository
To get update from a server, or put your changes to a server , you need to pull or push.
- Pull from remote to your local repository.
- Push your local repository to remote.
If you forgot some git commands, you can type
Note, git help is not a tutorial, and is usually difficult to understand for beginner.
- Git Ignore File
- Commit Files
- Push to Server
- Pull from Server
- Find Difference
- View Log
- Revert Change
- Temp Save: git stash
- What's HEAD
- Fix Detached Head
Git in Emacs
Emacs: magit-mode Tutorial