Mateen Kiani
Published on Sat Jul 29 2023·9 min read
Git is a distributed version control system developed by Linus Torvalds back in 2005. It is a free and open source software and was initially designed to make collaboration process for linux kernel development more convenient and streamlined, but later it gained so much popularity and is now the most popular version control system. By using Git you can track changes that you made in your files and it also makes collaboration with other people painless.
There are so many reasons to learn these technologies:
I will not get into the details of installation as there is already an official, quick and easy guide on how to install it on Windows, Linux or macOS. You can check it here.
When you make any change in a file you are changing it in working area not in the actual repository so you need to add it to staging area and then to the repository in the form of a commit.
Note: The terms staging area and index have same meaning and are used interchangeably.
How Git works?
Now you know what git is here is how you can use it to maintain your workflow in a good way.
The very first step you need to do is initialize you repository. Simply create an empty folder add some files to it and open your command line in that directory. Type the following command to create an empty git repository or reinitialize the existing one.
git init //initialize or reinitialize a repository
You can also clone an existing repository like this
git clone https://github.com/mateenkiani/react-tutorials // clone a repository
After initialization your repository is empty and there is no file in the staging area. By executing following commands you can add files to staging area before making a commit.
git add hello.py // add hello.py file to staging areagit add -A // add new files, update modified files and remove deleted files from staging areagit add -u // update modified files and remove deleted files from staging area
You can check the current status of working tree by using git status command, it will show if there are any unstaged changes. It will also show the changes that are in staging area (index) but not commited to the repository yet.
git status // current status of the working tree
To see the all the unstaged (indexed) changes that you have made you can use git diff command, it will show all the changes since the last time you added files to staging area, you can also specify a file name to review what changes you made in a particular file.
git diff // show all the unstaged changesgit diff index.html // show all the unstaged changes in index.html file
To commit all the changes to repository you need to use git commit command. It has various options -a or –all is used to stage all the modified and deleted files to staging area but not newly created files and commit them to the repository as well.
Note: you need to specify commit message with every commit by using -m option or a text editor will open asking you for commit message and if you leave it empty commit will be aborted.
git commit -m "commit message here" // commit staged changesgit commit -a -m "commit message here" // stage and commit changes but ignores newly created files.
If you want to unstage files from index (staging area) you can use git reset. It is the opposite of git add and clears all files from staging area.
git reset // unstage all the filesgit reset file.html // unstage only file.html
You can also use git reset to discard last commit and revert back to the previous one like this
git reset --hard HEAD~1 // discard last commit and checkout the commit done beforegit reset --hard HEAD~3 // discard last 3 commits and revert back to the 4th last commit
To remove a file from working directory as well as index you need to use git rm
git rm index.html // delete index.html from staging area and working directory.
To move or rename a file you need to use git mv command. Its syntax is git mv
Note: After mv you don’t need to re index any change.
git mv index.html file.html // rename index.html to file.htmlgit mv index.html ./src // move index.html to ./src/index.html
For branching you need to use git branches command. It can be used to create a branch based on another branch or a tag its syntax is as follows:
git branch <new-branch> // create a new branch with all the contents of working directorygit branch <new-branch> f71ac24d // create a new branch based on a commitgit branch <new-branch> v1.2 // create a new branch from a taggit branch --track <new-branch> origin/<base-branch> // create a new branch from remote branchgit branch -d <branch-name // delete a branch
You can use git switch to switch between branches like this
git switch <branch-name> // switch to specified branch
Git checkout is used to switch branches and if you don’t specify a branch name it updates files in the working tree to that in the index (staging area).
git checkout // updates the working directory with the files in the index.git checkout master // alternate way to switch between branches
To see all the commits that you have made you can use git log.
git log // shows all the commits you made along with author name and time of commit
To update your code to a cloud storage like github you need to use git push, but before you can push you have to add a remote first with this syntax git remote add <remote-name> <repo-url>
git remote add origin <repo-url> // add a remote named origin to a remote repositorygit remote -v // verify the remotes
After adding a remote you can push your files to the remote repository like this
git push origin master // pushes to a remote master branch, where origin is name of the remote you added
The opposite of push is fetch and you can fetch from a repository by executing this
git fetch origin master // fetch from a remote repository
To merge a branch with current directory use
git merge <branch-name> // merge a branch with current directory
Pull is equivalent to fetch + merge you can use it to fetch the content of remote repo and merge it as well. Its syntax is git pull
git pull origin master // fetch and merge from remote repository
If you want to switch to another branch but have some local changes that you don’t want to commit now you can save those changes by executing git stash.
git stash // save all the local changesgit stash pop // apply and drop all the saved changesgit stash apply // only apply all the previously saved changes
You can use git tag to mark some points as being important.
git tag <tag\_name> <commit\_id> // add a tag to a commitgit tag // display all the tagsgit checkout <tag> // checkout a specific tag
git init //initialize or reinitialize a repositorygit clone https://github.com/mateenkiani/react-tutorials // clone a repositorygit add hello.py // add hello.py file to staging areagit add -A // add new files, update modified files and remove deleted files from staging areagit add -u // update modified files and remove deleted files from staging areagit status // current status of the working treegit diff // show all the unstaged changesgit diff index.html // show all the unstaged changes in index.html filegit commit -m "commit message here" // commit staged changesgit commit -a -m "commit message here" // stage and commit changes but ignores newly created files.git reset // unstage all the filesgit reset file.html // unstage only file.htmlgit reset --hard HEAD~1 // discard last commit and checkout the commit done beforegit reset --hard HEAD~3 // discard last 3 commits and revert back to the 4th last commitgit rm index.html // delete index.html from staging area and working directory.git mv index.html file.html // rename index.html to file.htmlgit mv index.html ./src // move index.html to ./src/index.htmlgit branch <new-branch> // create a new branch with all the contents of working directorygit branch <new-branch> f71ac24d // create a new branch based on a commitgit branch <new-branch> v1.2 // create a new branch from a taggit branch --track <new-branch> origin/<base-branch> // create a new branch from remote branchgit branch -d <branch-name // delete a branchgit switch <branch-name> // switch to specified branchgit checkout // updates the working directory with the files in the index.git checkout master // alternate way to switch between branchesgit log // shows all the commits you made along with author name and time of commitgit remote add origin <repo-url> // add a remote named origin to a remote repositorygit remote -v // verify the remotesgit push origin master // pushes to a remote master branch, where origin is name of the remote you addedgit fetch origin master // fetch from a remote repositorygit merge <branch-name> // merge a branch with current directorygit pull origin master // fetch and merge from remote repositorygit stash // save all the local changesgit stash pop // apply and drop all the saved changesgit stash apply // only apply all the previously saved changesgit tag <tag\_name> <commit\_id> // add a tag to a commitgit tag // display all the tagsgit checkout <tag> // checkout a specific tag
Like and share with friends it really helps a lot. Mention the commands that I missed in the comments below.
Happy coding 😉