Git Ready to Git Going: Understanding Git and GitHub
As a developer, it is very important for you to learn Git and GitHub. In this brief introduction to Git and GitHub, you will learn about Git and GitHub and some extremely important commands. I have compiled a list of the most useful Git commands you can use while working on a project.
Git
Git is a tool that you can use to manage the versions of your files and create a local repository on your computer. It helps you keep track of the project's history. Who changed what and when, and who made it?
First, download and install git.
Using the terminal, we can manipulate file structures. Here are a few useful commands you should know about.
List all the commands in a folder →
ls
Make a directory →
mkdir project
Change directory →
cd project/
To see the history of the project →
git init
To list hidden files →
ls -a
What is inside the folder →
ls .git
Create a file Linux cmd →
touch names.txt
Check what changes are made in the project →
git status
Remove a file →
rm -rd <filename>
Maintaining history →
git add .
/git <individual file name>
Commit the changes →
git commit -m “message”
Add content in a file →
vi/vim <filename>
you can add whatever you want to add.Display the content that is available in the file →
cat <filename>
To unstage changes that have been added to the Git index but not yet committed →
git restore –staged <filename>
To remove a file completely →
git rm <filename>
To remove a file from the working directory but not from the Git index →
git rm — cached <file>
To remove the changes to the file from the index and also discard the changes in the working directory →
git reset HEAD <filename>
To see the history of the changes you made →
git log
Use
git stash
if you don’t want to commit changes to the history of the project.Add another change in a file (modified name) →
git touch <filename>
Try to add some changes →
vi <filename>
If you don’t want to commit and lose changes →
git stash
To retrieve changes later →
git stash apply
To apply the changes and remove them from the stash at the same time →
git stash pop
Want to delete all stash files →
git stash clear
GitHub
GitHub is a cloud-based service that hosts your Git repositories.
First of all, create a repository on GitHub.
To connect that repository to the local project→
git remote add origin <repo url>
To show the attached folders with the project →
git remote -v
To push the changes →
git push origin master
To prevent users from being impacted by your changes (unfinished code), create a new branch →
git branch <branchname>
To switch branches →
git checkout <branchname>
/git switch <branchname>
To combine changes from one branch into another →
git merge
To display information about the remote repositories that your local repository is connected to →
git remote -v
To push all changes →
git push origin master
Contribute to a project on GitHub
First of all fork the project.
Clone the repo →
git clone <url>
From where you have forked the project URL →
git remote add upstream <url>
To show a list of the remote repositories connected to your local Git repository →
git remote -v
Create a new branch →
git branch <branchname>
To switch between different branches in Git →
checkout/switch <branchname>
Add code →
git add .
Give it a message →
git commit -m “message”
Then →
git log
Now you want to push that new branch you made on your account to the original project →
git push upstream
To push changes from a local branch to a remote branch on the GitHub repository →
git push origin <branchname>
Add all the changes →
git add .
To create a new commit with a message →
git commit -m “message”
To push all changes →
git push origin <branchname>
If you’re adding many features with just one pull request, it would be very challenging to review your code. To avoid confusion, create separate branches and separate pull requests for each feature. One pull request can be opened per branch.To see all the commits and find the one you want to remove →
git log
Remove an unnecessary commit (copy the number id from the log you want to delete from commits) →
git reset id
→
git add .
→git status
→git stash
→git log
Make fork repository update with original branch →
git checkout main
→git status
→git log
→git fetch -all –prune
→git reset –hard upstream/main
→git log
→git push origin main
→git pull upstream main
→git push origin main
Squashing
Let’s say you have many commits and you want to merge that
Create a branch named “temp” →
git branch temp
Switch branch →
git checkout temp
Create a file named '1' →
touch 1
Add changes →
git add .
Commit changes with the message "add 1" →
git commit -m “add 1”
Create another file named ‘2’ →
touch 2
Add changes →
git add .
Commit changes with the message “add 2” →
git commit -m “add 2”
To see the history of changes you made →
git log
To rebase the current branch onto the commit specified by
<log id>
→git rebase <log id>
Force push changes to remote repository →
git push -f
Exit out of this →
Esc:x
Merge conflicts
Here are some common commands that you can use to resolve merge conflicts.
To avoid conflicts later on →
git pull
To switch to the branch →
git switch
/git checkout
To merge the changes from another branch into the current branch →
git merge <branchname>
Check conflicts →
git status
to see what changes are causing conflicts →
git diff
Implement the changes after the conflicts are finally resolved →
git add
Commit changes →
git commit
Finally, push the code to the remote repository →
git push
Wrap up
That concludes the discussion. I sincerely hope it is useful to you. If so, clap it, share it with others, and follow me, Ishrat Umar for more. Ask me on Twitter at @ishratUmar18 if you have any questions.
Happy learning!