Table of Contents
Git is a distributed version control system for tracking changes in source code during software development. It is a very useful tool for any developer, company, etc for maintaining the day-to-day activities and project respectively. Here is the list of all the common git commands that you probably use in a day-to-day workflow.
1. CONFIG
COMMAND:
$ git config --global user.name "<replace_with_your_username>" $ git config --global user.email "<replace_with_your_email>"
DESCRIPTION:
For GIT to know “Who you are?”, you should run the above-mentioned config commands. Using these commands, you can register yourself in Github. If you want to change the username and email then, repeat the above-mentioned command.
OUTPUT:
$ git config --global user.name "xyz-xyz" $ git config --global user.email "xyz@gmail.com"
2. LIST
COMMAND:
$ git config --list
DESCRIPTION:
To list the git config commands together then, run the above-mentioned command.
OUTPUT:
$ git config --global user.name "xyz-xyz" $ git config --global user.email "xyz@gmail.com"
3. INIT
COMMAND:
$ git init
DESCRIPTION:
Using this command, you can initialize a git repository for a new or an existing project. You just need to provide the name of the directory to be used and run the command.
OUTPUT:
~/Desktop$ git init Initialized empty Git repository in Desktop
4. CLONE
COMMAND:
$ git clone <clone url>
DESCRIPTION:
Using this command, you can get a remote repository on your computer. You can copy a GitHub repository from a remote source and also, it sets the remote source to the original source so that the data can be pulled again.
OUTPUT:
$ git clone https://github.com/django/django.git Cloning into 'django'... remote: Enumerating objects: 631, done. remote: Counting objects: 100% (631/631), done. remote: Compressing objects: 100% (405/405), done. remote: Total 16750 (delta 389), reused 393 (delta 192), pack-reused 16119 Receiving objects: 100% (16750/16750), 17.33 MiB | 2.17 MiB/s, done. Resolving deltas: 100% (11326/11326), done. Checking connectivity... done.
5. ALIAS
COMMAND:
$ git config --global alias.<new_name> <git_commands>
DESCRIPTION:
Using this command, shortcuts can be created. It is a powerful tool that wraps a sequence of commands and saves them under some name. It is not a standalone command and is used together with the config command.
OUTPUT:
$ git config --global alias.cpick cherry-pick cherry-pick alias
Another way to directly add the aliases in the config file is given below.
The command mentioned above can be translated into the following configuration in the global file.
[alias] cpick = cherry-pick
6. STATUS
COMMAND:
$ git status
DESCRIPTION:
Using this command, you can find the changes made in all the files related to the current working project. Also it will display the name of the branch that you are creating working on. This command is necessary for the developers before committing the changes. This command will list out all the file names in which changes are done. And will also show if there is no change done. There are 3 categories in which the new/modified files are grouped.
- Changes to be committed
- Changes that are not staged to commit
- Untracked files
OUTPUT:
Without any changes:
$ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
With some changes:
On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: mysite/settings.py Untracked files: (use "git add <file>..." to include in what will be committed) mysite/__pycache__/ no changes added to commit (use "git add" and/or "git commit -a")
7. ADD
COMMAND:
$ git add <filename> $ git add.
DESCRIPTION:
Using this command, you can add the new file which is added to the project.
- Using the 1st command we can add the files one by one.
- Using the 2nd command we can add all the files at once.
OUTPUT:
$ git add "html/css/display.html" $ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: html/css/display.html Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: mysite/settings.py Untracked files: (use "git add <file>..." to include in what will be committed) mysite/__pycache__/
8. FETCH
COMMAND:
$ git fetch [name_of_your_remote] $ git fetch --all
DESCRIPTION:
Using this command, you can update your branch when the original branch from the official repository has been updated.
COMMAND:
$ git fetch --all Fetching origin remote: Enumerating objects: 4000, done. remote: Counting objects: 100% (4000/4000), done. remote: Compressing objects: 100% (102/102), done. remote: Total 5396 (delta 3949), reused 3939 (delta 3892), pack-reused 1396 Receiving objects: 100% (5396/5396), 686.04 KiB | 3.00 KiB/s, done. Resolving deltas: 100% (4263/4263), completed with 1431 local objects. From github.com:django/django-ui * [new branch] 3435-faq -> origin/3435-faq * [new branch] 3436-about -> origin/3436-about
9. CHECKOUT
COMMAND:
$ git checkout [name_of_your_branch] $ git checkout <file_name> $ git checkout.
DESCRIPTION:
Using this command, you can do the following.
- Switch to another branch from the current working branch, use 1st command.
- Undo the changes made in a single file, use 2nd command.
- Undo the changes made in all the files, use the 3rd command.
OUTPUT:
Switched to branch 'master' Your branch is up-to-date with 'origin/master'.
10. CREATE BRANCH
COMMAND:
$ git checkout -b [name_of_your_new_branch]
DESCRIPTION:
Using this command, you can create a new branch and work in that and do the other processes like commit, pull push etc. in the same branch.
OUTPUT:
$ git checkout -b development Switched to a new branch 'development'
11. BRANCH
COMMAND:
$ git branch
DESCRIPTION:
Using this command, you can know the list of how many branches are created in total in the current working project and also know in which branch you are currently working in. Asterix (*) denotes the current working branch.
OUTPUT:
$ git branch * master development hotfixes
12. DELETE BRANCH
COMMAND:
$ git branch -d [name_of_your_new_branch] $ git branch -D [name_of_your_new_branch]
DESCRIPTION:
- Using the 1st command, you can delete a branch on your local filesystem.
- Using the 2nd command, you can force a delete local branch on your filesystem.
OUTPUT:
$ git branch -D development Deleted branch development (was b560dc8).
13. PULL
COMMAND:
$ git pull origin <branch_name>
DESCRIPTION:
Using this command, you can update the changes done by the other developers working along in the same project and in the same branch. This is a best practice to pull the changes done by the other developers and then push changes done by us.
OUTPUT:
$ git pull origin master From https://github.com/thealphaking01/django * branch master -> FETCH_HEAD Already up-to-date.
14. DIFF
COMMAND:
$ git diff <filename> $ git diff .
DESCRIPTION:
Using this command, you can see the difference made in each file (detailed changes made in each file). The output will have “+” and “-” signs which indicate new added and old deleted changes.
- For finding changes made in each file separately use the 1st command.
- For finding changes made in all files together use the 2nd command.
OUTPUT:
$ git diff mysite/settings.py diff --git a/mysite/settings.py b/mysite/settings.py index 1150657..b97f24f 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -29,7 +29,7 @@ ALLOWED_HOSTS = [] # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # In a Windows environment this must be set to your system time zone. -TIME_ZONE = 'America/Chicago' +TIME_ZONE = 'Asia/Kolkata' # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html
15. COMMIT
COMMAND:
$ git commit -am "Conflict resolved" $ git commit -m “<message>” . $ git commit -m “<message>” <filename>
DESCRIPTION:
Using this command, you can save the changes made in the files by the developers. Where “-m” represents “message”.
- When conflict occurs, you should use the 1st command mentioned above.
- When changes made in all the files should be committed at once, you should use the 2nd command mentioned above.
- When you want to commit only one file out of many then, the 3rd command should be used.
OUTPUT:
$ git commit -m “hot-fixes made in the homepage (#277)” a/mysite/homepage.py [master 823657c] Changes made in settings file 1 file changed, 6 insertions(+), 6 deletions(-)
16. PUSH
COMMAND:
$ git push origin <branch_name>
DESCRIPTION:
Using this command, you can update your changes in the current working branch. So that while the other developers work they can pull the changes and avoid conflicts.
OUTPUT:
$ git push origin development Counting objects: 24, done. Delta compression using up to 4 threads. Compressing objects: 100% (24/24), done. Writing objects: 100% (24/24), 3.95 KiB | 0 bytes/s, done. Total 24 (delta 12), reused 0 (delta 0) remote: Resolving deltas: 100% (12/12), completed with 11 local objects. To git@github.com:django/django.git 712a806..5421ec7 development -> development
17. LOG
COMMAND:
$ git log <options>
DESCRIPTION:
Using this command, you can view all the commits made by all the developers in the current working project(regardless of the branch checked out). It has many options like you can find the log for the latest commits, logs of a particular developer, date range, etc.
OUTPUT:
$ git log --all commit c36d2103222cfd9ad62f755fee16b3f256f1cb21 Author: Xyz <xyz@example.com> Date: Tue Mar 25 22:09:26 2020 -0300 Ut sit. commit 97eda7d2dab729eda23eefdc14336a5644e3c748 Author: Abc <abc@example.com> Date: Mon Mar 24 10:14:08 2020 -0300 Mollis interdum ullamcorper sociosqu, habitasse arcu magna risus congue dictum arcu, odio.
18. STASH
COMMAND:
$ git stash $ git stash apply
DESCRIPTION:
Using this command, you can take a backup of the changes made in all the files in case of an emergency.
- If you want to do some hot-fixes in another branch and you are not able to check out the branch because of the changes made in the current working branch then, the 1st command is used.
- If you want to apply the changes made then, the 2nd command is used.
Note: Changes will not be applied on its own until the user give the apply command in the same branch where the stash was made.
OUTPUT:
$ git stash Saved working directory and index state WIP on development: c353ee4 Confilict resolved HEAD is now at c353ee4 Confilict resolved $ git stash apply On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: mysite/settings.py Untracked files: (use "git add <file>..." to include in what will be committed) mysite/__pycache__/ no changes added to commit (use "git add" and/or "git commit -a")
19. RESET
COMMAND:
$ git reset <filename>
DESCRIPTION:
Using this command, you can reset the committed files. And again if you type “git status” then, the committed files will be uncommitted.
OUTPUT:
git reset html/css/display.html Unstaged changes after reset: M mysite/settings.py
Do you find it interesting? you might also like these articles. Top 10 Best Tech Companies For Employees To Work In The USA In 2020 and Top 10 IT Staffing and Recruiting Agencies in the USA.
If you have a business idea in your mind and in search of a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.