1. git config
This command used to get and set configuration variables that control all aspects of how Git looks and operates. Use
--list to get all configuration as following command:
git config --list
Use the following two commands to set name and email in global level:
git config --global user.name "John Doe" git config --global user.email email@example.com
2. git init
This command is used to create an empty Git repository. Simply run the following command to initialize Git repository:
To start a new Git repository for an existing code base, run the following commands in the main folder of our code base:
git init git add . git commit
git init docs
3. git status
This command is used to display the state of the working directory and the staging area, including which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
Run the following command to see the state of our repository:
Git Status: Inspecting a repository
4. git add
This command is used to move modified files to
staging index which is area where Git stores files that is ready to commit.
Run the following command if we want to move all modified files into staging index:
git add .
Or run the following command to add specified files, for instance we want to add
git add index.html css/site.css
To unstage files in staging index, run the following command:
git reset HEAD index.html
The preceding command will unstage
index.html from staging index since
HEAD is the last commit of the current branch.
5. git commit
This command is used to commit all files in staging index. We must provide a descriptive message when committing the staging index, for instance
add unit test for GetUser() function
Run the following command to commit staging index in Git:
git commit -m "<descriptive message>"
git commit docs
6. git clone
This command is used to grab a complete copy of another user’s repository. In GitHub, use pattern
https://github.com/USERNAME/REPOSITORY.git as target repository.
For instance, if we want to clone
https://github.com/WisnuAnggoro/hello-world to our active local directory, simply run following command:
git clone https://github.com/WisnuAnggoro/hello-world.git
7. git fetch
This command is used to grab all the new remote-tracking branches and tags without merging those changes into our own branches.
To fetch remote repository, we need to specify
remote-name as the target.
origin is common remote name.
Run the following command to fetch all branches from remote repository (
git fetch origin
We can also fetch spesific remote branch to local repository. For instance, the following command will only fetch
master branch in
git fetch origin/master
Git Basics - Working with Remotes
8. git remote
This command is used to add a new remote. Run the following command to create
origin remote name:
git remote add origin https://github.com/user/repo.git
To find out our remote name, use this command:
git remote -v
git remote docs
9. git merge
This command is used to combine our local changes with changes made by others.
Run this command to merge
remotename/branchname to our active local branch:
git merge remotename/branchname
git merge docs
10. git pull
11. git push
12. git branch
This command is used to list, create, or delete branches. To list local branches only, simply run the following command:
To list all branchss in both local and remote, run the following command:
git branch -a
When listing all branches in both local and remote repositories, press
d to continue to next list and press
q to quit the list.
To delete local branch, we can run the following command:
git branch -d [branch_name]
-d argument stands for
--delete which will delete the local branch only if we have already pushed and merged it to remote branches.
Another delete branch command is as follow:
git branch -D [branch_name]
-D argument stands for
--delete --force. The preceding command will delete
branch_name branch regardless of its push and merge status.
For SAFETY REASON, please use only
-d argument when deleting local branch.
13. git show-branch
This command is used to show branches and their commits. Just simply run the following command to show all branches and their commits:
git show-branch docs
14. git checkout
This command is used to switch to different local branches. Run the following command to switch to
git branch other-branch
To create and switch to a
new-branch, run the following command:
git branch -b new-branch
git checkout docs