Let’s Git Up

Let’s Git Up

Git 101 and basic Linux Commands to aid your workflow.

·

5 min read

I believe every month is an opportunity to get started with open source not just October. This is a list of commands that I just wanted to note and share that might be helpful for those just getting started; )

image.png

Reference for getting started with GitHub, setting up SSH keys, etc. — shorturl.at/rEFOZ

Commonly used Git Commands we use during local development:

git init — to initialize a remote git repository
pwd — prints the current working directory
ls -a : to list any files in the current directory including hidden files like .git
mkdir folder_name — to create a new folder from the command line
touch file_name, cd filename — to create and travel to the file
rm -r filename — to delete the file and anything inside it (recursively), Note that rm filename also does the job
If any code changes are made, we use git commit to take a screenshot of the changes at that particular point in time and note that the next commit is dependent on the previous one which means we use the last commit’s hash id to delete/deal with the next one and similarly used when we want to add multiple commits in one go/commit which I felt was a pretty cool trick to know (done using git reset command)
git diff — to track any changes made in a file before it has been staged
git add . / folder/filename : to prepare all the changes to get ready for a screenshot/commit
git restore — — staged fileName : to restore a removed file to the stage
git status — just in case you want to know if all the changes have reached the stage
git commit -m “screenshot message” : the big moment is here
git log — to track all the commits made so far in the current branch
git reset commitID — to remove the commit from the staged area
git stash — if any of the changes feel they’re not ready yet, they’re sent backstage without affecting any other changes
git stash clear — if the changes have withdrawn their interest from being committed yet 😉
git stash list — lists the items/staged commits in the stash
git stash pop — if the changes are ready to get committed (LIFO- a pile of changes & top/most recent change is called using pop)
git remote add origin repoURL , git push -u origin master : to push the files with all changes to the GitHub repo created using git init.
git reset — — hard commitID : use this with caution as it cannot be reversed, used to rewrite the working tree from the required commit

Commonly used Git Commands when working on a project while collaborating:

git checkout -b newBranchName
git checkout existingBranchName
git push — — set-upstream origin branchName : to push the new branch with changes to the repo, to merge all the changes to the main branch, a pull request can be created and merged if all the test cases pass
git push origin branchName -f : to forcefully push the branch after deleting a commit/making a change remotely which is different from the git branch
git pull/fetch — to stay up to date with any changes in the main repo and keep code changes in sync with your working directory. Alternatively, git provides an option of fetch & merge on noticing the changes to get this job done.
git merge alias/branch — to merge the specified branch’s history into the current one.

Commonly used Git Commands when working with a forked Github repo:

git clone URL or click on Fork button on GitHub
git checkout branchName(main of forked rep)
git fetch — — all — — prune : to fetch all the changes in the main branch from git repo including deleted ones
git reset — — hard upstream/main : to reset the main branch of origin to main branch of upstream/original repo(to sync main branches of forked and original repo)
git push origin main: to push the main branch of local to the forked repo
git pull upstream main: to pull all code changes in one line of command
An alternative to these commands is using the fetch upstream icon appearing on the forked Github repo
Merge conflicts can be resolved manually, so leave that for now

Good to know commands:

git rebase -i commitID : to squash(overwrite pick with “s” or pick required commits).
echo message > fileName — to add a message to the file
cat fileName — to print/see the contents of the file
find ./ —name “fileName” (to be more specific) — self-explanatory, alternative to it can be: grep message fileName — to find the message in the file and display it
clear — to clean up the cmd console
ls -l — used to display long format of files based on read-write type permissions
ls -al — displays the long format of read-write files including hidden ones
cd .. — to go back to the root directory, same as cd /
cd ~ — to go to the home directory
cd — : to go back to the previous directory
nano fileName — to make any changes in the file from the cmd
ping websiteName

It’s definitely worth having some fun with these newly learned commands, for which you can check out these — overthewire.org/wargames/bandit & learngitbranching.js.org

Finally, I’d like to share a resource to get started with open source with proper guidance on projects while contributing to a cause — skills.yourlearning.ibm.com/activity/PLAN-5..

CheatSheet by GitHub — education.github.com/git-cheat-sheet-educat..

Hope this helps, let me know if you’d like me to add a cool/tricky command you’re aware of to this list. Thanks for reading.