Lecture 03 – COMMON GIT COMMANDS
COMMON GIT COMMANDS
Let's explore some Basic Git Commands.
Git is a popular version control system used for tracking changes in source code during software development. It provides a set of commands that allow users to interact with a Git repository. You can do the following tasks when working with Git
Creating Repository
Create a directory project on your ec2 machine, navigate inside to perform the below commands.
- nano text editor – Create 2 text files using nano text editor
COPY & RUN –> $ the below command
nano text1.txt text2.txt
- git init – Enter the command git init to initialize a git repository for your project on the ec2 server.
COPY & RUN –> $ the below command
git init
Making Changes
- git status – Once the directory has been initialized you can check the status of the files, whether they are being tracked by git or not, using the below cmd.
COPY & RUN –> $ the below command
git status
- git add – To track all the files in the project folder.
COPY & RUN –> $ the below command
git add test1.txt test2.txt
- git commit – Once the files or changes have been staged, we are ready to commit them in our repository. We can commit the files using the command “git commit –m “custom message”.
COPY & RUN –> $ the below command
git commit -m "custom message"
Syncing Repositories
Once everything is ready on our server, then we can start pushing our changes to the remote repository.
Create a gitcommands repository on your github repo account.
- git remote – is used in Git to manage the remote repositories associated with your local repository. It allows you to view, add, rename, and remove remote repositories.
COPY & RUN –> $ the below command
git remote add origin "your repo link"
Click on your avatar (right top of the screen) from the drop-down –> settings –> Developers –> to generate access token
- git push – To push our changes to the remote repository. Copy your repository link and paste it in the command.
COPY & RUN –> $ the below command
git push origin master
Your local repository is now synced with the remote repository
- git clone – If we want to download the remote repository to our local repo, we can use the command git clone.
COPY & RUN –> $ the below command
git clone "your repositoty url"
- git pull – The git pull command is also used for pulling the latest changes from the repository, unlike git clone; this command can only work inside an initialized git repository.
This command is used when you are already working in the cloned repository, and want to pull the latest changes, that others might have pushed to the remote repository.
COPY & RUN –> $ the below command
git pull "your repo url"
Parallel Development
- git branch – Until now, we saw how you can work on git. But now imagine, multiple developers working on the same project or repository.
To handle the workspace of multiple developers, we use branches. To create a branch from an existing branch.
COPY & RUN –> $ the below command
Similarly, to delete a branch use the command git branch –D branch-name
git branch name-of-new-branch
- git checkout – To switch to the new branch, we use the command git checkout <branch-name>
COPY & RUN –> $ the below command
git checkout branch-name
Ensure to push the new feature into your remote repo using git push origin “newfeature”
- git log – Want to check the log for every commit detail in your repository? You can accomplish that using the cmd.
COPY & RUN –> $ the below command
git log
- git stash – To stash your staged files without committing just type in git stash. If you want to stash your untracked files as well, type git stash –u. Once you are back and want to retrieve working, type in git stash pop.
COPY & RUN –> $ the below commands
git stash
git stash -u
git stash pop
- git diff – To check the differences between two or more versions of file
COPY & RUN –> $ the below command
commit-id can be found from git log output
git diff commit-id-version1 commit-id-version2
- git revert – Git revert command helps you in reverting a commit, to a previous version
COPY & RUN –> $ the below command
git revert commit-id
- git Merge – To merge the feature branch to master, checkout master and run below cmd.
COPY & RUN –> $ the below command
git merge newfeature
