Git Commands with Examples
In this step by step guide I am going to run through the basic Git commands with some real world examples.
We are going to assume you already have Git installed.
If you are new to Git please check out my Git Tutorial for Beginners first
1. Git Version
The first Git command we are going to look at is git version.
This will show what version of Git you have installed.
➜ ~ git version git version 2.25.1 ➜ ~
At the time of writing, the current git version is 2.25.1
2. Git Init
The first step you will normally perform is to initialise a folder so it can be version controlled.
To perform this action you will cd into the folder and then issue the command git init.
For this example I am going to create a new directory, cd into it and then initialise it for git.
➜ ~ mkdir git-tutorial ➜ ~ cd git-tutorial ➜ git-tutorial git init Initialized empty Git repository in /home/roger/git-tutorial/.git/ ➜ git-tutorial git:(master)
Note: You will notice my prompt changes to git:(master) after I had initialised the folder. This is because I am using ZSH.
This is a great shell that displays current git status at the prompt.
3. Git Status
Once you have initialised your folder the first command you will always run is git status. This is also a command you will use a lot. It shows you if the folder is initialised and also what files are being tracked, and which files are committed and ready to be pushed.
➜ git-tutorial git:(master) git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
The git status command will also show you what branch you are working on.
➜ git-tutorial git:(master) git status On branch master nothing to commit, working tree clean ➜ git-tutorial git:(master)
You can now see the benefit of the ZSH as it shows the branch you are on within the prompt!
4. Git Add
So now we have a folder, which has been initialised for git use we need to add a few files and then track them.
➜ git-tutorial git:(master) touch file1.txt ➜ git-tutorial git:(master) ✗ touch file1.txt ➜ git-tutorial git:(master) ✗ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) file1.txt nothing added to commit but untracked files present (use "git add" to track)
I created a file called file1.txt, when I run the git status command you can see the file is untracked.
To add the file to the git repository we issue the command git add <filename>
➜ git-tutorial git:(master) ✗ git add file1.txt ➜ git-tutorial git:(master) ✗ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: file1.txt
After issuing the git add file1.txt command I then issued the git status command to verify the file has been added to the git repository.
It now needs to be committed
5. Git Config
Before you make a commit however, you need to enter a few pieces of information into git, so it can identify when a change was made and more importantly, who made it!
You just need to enter your name and email address using the following commands.
➜ git-tutorial git:(master) git config --global user.name "Roger Perkin" ➜ git-tutorial git:(master) git config --global user.email [email protected]
6. Git Commit
We now issue the command git commit -m “<message to describe commit>”
➜ git-tutorial git:(master) ✗ git commit -m "Initial Commit" [master (root-commit) 38653de] Initial Commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1.txt ➜ git-tutorial git:(master) git status On branch master nothing to commit, working tree clean
Note: Again you can see my prompt had a X indicating un-committed files in Git, once I had committed them the prompt returns to normal.
7. Git Clone
This command enables you to get someone else’s code onto your machine. You simply use the command git clone and the location of the repo on github.
git clone https://github.com/rogerperkin/python-scripts-for-network-engineers
This will pull the entire repository from my github onto your computer. If you want to specify where it goes you can follow the command with a folder e.g. folder1
If you do not specify a folder, git will create a folder with the same name as the repository you are cloning.
➜ ~ git clone https://github.com/rogerperkin/python-scripts-for-network-engineers folder1 Cloning into 'folder1'... remote: Enumerating objects: 63, done. remote: Counting objects: 100% (63/63), done. remote: Compressing objects: 100% (47/47), done. remote: Total 63 (delta 29), reused 44 (delta 15), pack-reused 0 Unpacking objects: 100% (63/63), 10.39 KiB | 82.00 KiB/s, done. ➜ ~ ls core Downloads Music projects Templates Desktop folder1 network-programmability Public VENVS Documents git-tutorial Pictures snap Videos ➜ ~ cd folder1 ➜ folder1 git:(master) ls backup-multiple-routers.py LICENSE README.md backup-router.py netmiko-save-config.py ssh-to-multiple-routers.py devices.txt ping.py ssh-to-router.py device-types.py ping-trace.py ➜ folder1 git:(master)
When you clone a repository, you will get everything including the full list of commits, i.e. every change that was ever made.
7. Git Log
To view a history of all commits that have ever been made to a repository you issue the git log command. This command will open a file showing every commit made, the hash and the name of the person that made it and the date and time it was made.
commit 38653defd6574ab2aa565c7f37a97e38f6ac86ba (HEAD -> master) Author: Roger <[email protected]> Date: Wed Jul 7 07:53:53 2021 -0700 Initial Commit
Note: To exit the git log type q and hit enter.
9. Git Checkout
To create a new branch (a copy of the code so you can work on it) you enter the command git checkout -b mycode
The -b flag will create a new branch and then switch to it the name of your new branch can be whatever you like.
➜ git-tutorial git:(master) git checkout -b mycode Switched to a new branch 'mycode' ➜ git-tutorial git:(mycode)
Git checkout is also used to revert changes. You can “checkout” the last version of a file to overwrite your local file to the last committed version.
git checkout <filename>
10. Git Diff
To compare differences between two commits you can use the git diff command
You need to find the hash of the commit for the two files you want to compare or diff against. you find these using git log
Then issue the git diff <hash of commit1> <hash of commit2> command
git diff 6bb47c5c76cd54ecdc15f5af03a2e474c41c127f 91980a6c61163f66c4589eb633d7eac67a9de9a5 diff --git a/file1.txt b/file1.txt index 671f2d1..f5b8799 100644 --- a/file1.txt +++ b/file1.txt @@ -1,3 +1 @@ -added some tex - -added some more text +added some text
Repository or Repo is where all the version controlled files and data are stored. The git repository is a folder on your computer that is hidden and can be found at .git/. You should never need to worry about this, just know that it is there.
Working Directory is the folder on your computer with both tracked and un-tracked files.
Versioned Files are files that you are tracking and have added to git with the git add command.
Un-Versioned Files are files that you are not tracking in git.
Commit is when you take a snapshot of your version controlled files. The commit is a point in time and all changes are locked into the repository. You can always revert back from a committed version.
Branches enable people to work on different parts of the project in a different branch (or copy of the project) once the changes have been made to the branch they can then be merged back into the main branch.
Learn more with these other posts – Gitlab vs Github which is better?
There are so many more commands you can use with Git but these are the basics and will cover 90% of what you need to do with git for version controlling your code, working locally on your machine.
Learn more about Git here – https://git-scm.com/doc/ext
How do I use Git Commands?
The basic operations of Git look like this.
1. Initialise a folder
2. Add some files
3. Add the files to Git
4. Commit the files
5. Version control all those files.
What is Git Clone?
git clone is a Git command which is enables any user to clone a git repository to their local machine. Or take a copy of the target repository. You can use shallow options to partially clone repositories. Most people will normally take a full git clone