Understanding Git and GitHub
All important Git and GitHub commands cheat sheet

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 →
lsMake a directory →
mkdir projectChange directory →
cd project/To see the history of the project →
git initTo list hidden files →
ls -aWhat is inside the folder →
ls .gitCreate a file Linux cmd →
touch names.txtCheck what changes are made in the project →
git statusRemove 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 logUse
git stashif 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 stashTo retrieve changes later →
git stash applyTo apply the changes and remove them from the stash at the same time →
git stash popWant 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 -vTo push the changes →
git push origin masterTo 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 mergeTo display information about the remote repositories that your local repository is connected to →
git remote -vTo 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 -vCreate 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 logNow you want to push that new branch you made on your account to the original project →
git push upstreamTo 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 logRemove 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 logMake 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 tempSwitch branch →
git checkout tempCreate a file named '1' →
touch 1Add changes →
git add .Commit changes with the message "add 1" →
git commit -m “add 1”Create another file named ‘2’ →
touch 2Add changes →
git add .Commit changes with the message “add 2” →
git commit -m “add 2”To see the history of changes you made →
git logTo rebase the current branch onto the commit specified by
<log id>→git rebase <log id>Force push changes to remote repository →
git push -fExit 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 pullTo switch to the branch →
git switch/git checkoutTo merge the changes from another branch into the current branch →
git merge <branchname>Check conflicts →
git statusto see what changes are causing conflicts →
git diffImplement the changes after the conflicts are finally resolved →
git addCommit changes →
git commitFinally, 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!




