Introduction to Git
What is Git?
Git is the most popular and widely using Version control system in the software development industry. Hold on, now what is a version control system?
Before talking about Git, let’s take a quick look at what is the version controlling and why we need it.
Version control/ Version management
Now imagine you are working on a project with multiple collaborators. In that case everyone has to use the same source files. What happened when multiple developers change same source file concurrently?. There will be many conflicts. Then how you can manage it?
That’s why you need version controlling.
Version control can be simply described as the management of changes to the source files of a software in software development process. In a version controlling System, different users can use the same source file at the same time and it records the changes to a file or set of files. Version control system keeps track of versions on development, so in the development process, you can recall previous versions at any time and recover if something messed up.
Now let’s talk about Git.
Git was invented in 2005. It is a free and open source platform. Also it supports multiple protocols (Http, SSH). Like mentioned earlier, Git is the widely using Version control system in Nowadays. It is a distributed version control system that allows anyone to get a complete clone of source code. So in git, you can have a local copy of remote repository in your computer.
To use git, git should be installed in your computer and also you have to have an account on github or gitlab. you can initialize remote repositories in those platforms.
Basic Git commands
git init — This command will initialize an empty git repository in your working directory.
How to and Where: Navigate to your project folder inside the command prompt, then type and enter the above command
git add <filename> — This command can add specific file in directory to initialized repository
git add. — This command will add all the files in current directory to local repository
git add* — This command will add all the files in current directory to local repository except files that file name begin with a dot.
git commit -m “commit message” — Commit changes to the repository
git stage <filename> — Stage changes did to a file
git branch <branchName> — This will create a new branch
git checkout <branchName> — Switch to a branch
git remote add origin <remote repository url> — Connect the local repository to remote repository
git push — set-upstream origin <branchName> — This command will push to remote repository by creating upstream.
git push origin <branchName> — Also push to the remote repository by setting upstream.
Note: upstream make the reference from current local branch to the default remote branch.
git fetch< remote repository url> — get the changes from the all branches of remote repository, but do not merge them.
git fetch< remote repository url> <branchName> — get the changes from the specific branches of remote repository
git merge <branchname> — Merge an another branch with current branch
git pull — Fetch and merge changes from the remote repository to local repository.
git clone<remote repository url> — copy and download an identical copy of remote repository into computer.
git branch or git branch — list — View the branches
git branch -d <branchName> — Delete a branch
git status — Gives the necessary information about current branch
Git Restore
The “restore” command is used to unstage or discard uncommitted local changes to files and also to unstage previously added file.
git restore — staged <filename> — Undo the previous git add ( unstage the file)
git restore — staged *<file extension> — Unstage multiple files
git restore <filename> — Discard the uncommitted local changes in the file.
Note: This operation cannot be undo
git restore — source <reference of commit> <filename> — Restore to the specific previous version.
Git Stash
git stash command use to temporary save the uncommitted changes when switching between branches.
git stash — Save the uncommitted changes in current branch.
git stash save <”save message”> — Also you can stash with a message.
git stash branch <branch name> — Save the stash in separate branch. ( To avoid conflicts)
git stash list — To check the saved stashes
git stash show — Display the stashed files and changes did to them(insertions/deletions)
git stash show -p — Display the Changes did to files
git stash apply — Apply the stashed changes. This will apply the last stash
git stash apply <stash id> — Apply the specific stash
git stash pop — Also apply the stashed changes but delete the stash after the apply.
git stash drop — Delete the most recent stash
git stash drop <stash id> — Delete a specific stash
Hope you something learned. Thanks for reading!