- First-time git setup
- Getting & creating projects (or repositories)
- Snapshotting & working with files
- Branching & merging
- Sharing projects (repositories) through git servers
- Inspection & comparison
- Using gitignore
- Undo things
- Tips
- ToDo
If you are new in git (new installer or first-time user) , you need to add some configuration for the first time.
you can see or write configs using git config
command.
Command | Description |
---|---|
git config --global user.name "your name" |
Add (update) your name into configs. |
git config --global user.email [email protected] |
Add (update) your email into configs. |
git config --list |
Show list of current configs. |
Command | Description |
---|---|
git init |
Initialize a local Git repository |
git clone <repository-url> |
Create a local copy of a remote repository |
Command | Description |
---|---|
git status |
Check status |
git add <file-name.txt> |
Add a file to the staging area |
git add -A |
Add all new and changed files to the staging area |
git commit -m "<commit-message>" |
Commit changes |
git commit -am "<commit-message>" |
Add and commit changes in one command |
git rm -r <file-name.txt> |
Remove a file (or folder) recursively |
git mv <file-name.txt> <path/new-file-name.txt> |
Move or rename a file (or folder) |
git cp <file-name.txt> <copy-file-name.txt> |
Copy a file (or folder) |
Command | Description |
---|---|
git branch |
List branches (the asterisk denotes the current branch) |
git branch -a |
List all branches (local and remote) |
git branch <branch-name> |
Create a new branch |
git branch -d <branch-name> |
Delete a branch |
git push origin --delete <branch-name> |
Delete a remote branch |
git checkout -b <branch-name> |
Create a new branch and switch to it |
git checkout -b <branch-name> origin/<branch-name> |
Clone a remote branch and switch to it |
git checkout <branch-name> |
Switch to a branch |
git checkout - |
Switch to the branch last checked out |
git merge <branch-name> |
Merge a branch into the active branch |
git merge <source-branch> <target-branch> |
Merge a branch into a target branch |
git is a distributed version control system (DVCS), that means you can commit your work locally, and then sync your copy of the repository with the copy on the git server, so your team can have access to the latest files of the project through the git servers.
Command | Description |
---|---|
git remote add <remote-name> <remote-repository-url> |
Add a remote repository. |
git remote set-url <remote-name> <remote-repository-new-url> |
Set a new url for <remote-name> repository. (change the given remote's URL) |
Command | Description |
---|---|
git push origin <branch-name> |
Push a branch to your remote repository |
git push -u origin <branch-name> |
Push changes to remote repository (and remember the branch) |
git push |
Push changes to remote repository (remembered branch) |
git push origin --delete <branch-name> |
Delete a remote branch |
git pull |
Update local repository to the newest commit |
git pull origin <branch-name> |
Pull changes from remote repository |
Command | Description |
---|---|
git log |
View changes |
git log --summary |
View changes (detailed) |
git diff <source-branch> <target-branch> |
Preview changes before merging |
The .gitignore
file, tells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.
(Note that all the gitignore files really concern only files that are not already tracked by git)
Create a file and name it .gitignore
, then use the below rules to ignore files:
- A line starting with
#
serves as a comment. - An optional prefix
!
which negates the pattern. - A leading slash matches the beginning of the pathname. For example,
/*.c
matchescat-file.c
but notmozilla-sha1/sha1.c
. /**/
matches all sub pathnames, for example,a/**/b
matchesa/b
,a/x/b
,a/x/y/b
and so on.- A trailing
/**
matches everything inside. For example,abc/**
matches all files inside directoryabc
, relative to the location of the.gitignore
file, with infinite depth. []
matches one character in a selected range, for example [a-z] matches any character from a to z (a,b,c,d to z).?
matches any one character except/
*
matches anything except/
Example:
# this is a comment
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
We have 2 different states:
- Don’t code for a whole weekend on five different issues and then submit them all as one massive commit on Monday. Even if you don’t commit during the weekend, use the staging area on Monday to split your work into at least one commit per issue, with a useful message per commit.
- Write your commit message in the imperative:
Fix bug
and notFixed bug
orFixes bug
. This convention matches up with commit messages generated by commands like git merge and git revert.
we have something to add or complete in future in this document. you can help us to complete the ToDo list by doing them and creating pull request .
- Complete the Undo things section.
- Add a section for
Rebasing and cherry-pick
technique. - Add a
Tags and versioning
section. - Add a some examples in each section.