In the spirit of learning in public, I am writing my own Git cheatsheet.
I am still new, so if I got any of the terminologies wrong, please correct me :)
This cheatsheet is going to be really basic and assumes that you only make changes and commit in the master branch. In the future I will discuss more about branches.
There are two main ways of getting a git repo in your local machine and then pushing it onto GitHub. These are:
- Fetching a repo from GitHub, make the changes and push it back to GitHub
- Create a new directory locally, initialise git, track the changes and push it up to GitHub
The first way is more common, we can do it using the workflow below:
git clone <the SSH key for your repository> # This fetches the GitHub repo to your local machine and starts the tracking process # You will now make some changes to files inside the repo # Once you're done, you wanna check what are the changes git status # git status tell you what are the modified files, as well as new files you've created git add <file_names> # git add adds the files you want to commit on the stage # you don't have to add all the files that are changed onto the stage # for example, you might actually have made a change in one of the files and you prefer the previous version, in that case you don't have to git add the change # You would probably want to check the git status again to make sure you have added all the correct files git status # Now, you would be able to see all the tracked and untracked files. Check for any errors before committing git commit -m "add meaningful commit message" # this will add a commit message and take a snapshot of your repository with the change you wish to document # Once you've done that, you might want to check git status again git status # double and triple checking is always good, now your command prompt should tell you that there is no untracked files. # This is all good for you, but if you want to collaborate with others and for others to also see the changes you've made, you'd want to push this commit to GitHub. # Assuming that you've been working on the master branch, you will do the following git push origin master # This pushes the changes in the master branch to the remote called origin, which should be your GitHub repo. Sometimes, you're branch is not master and your remote is not origin, you can use the format # git push <remote_name> <branch_name> # Now this will become a pull request ready to be merged into the main assuming that there are no conflicts
Making a repo locally and then pushing it on GitHub is less common, but we will go through it nonetheless:
# First, you want to make the folder, cd into it and then initialise version control using git mkdir <folder_name> # This creates a new folder cd <folder_name> # This moves your current position into the folder git init # This initalises git. Please do this only in a folder for a specific project, don't do something like this in your user directory because you will be tracking EVERY SINGLE CHANGE in that directory. # If you track your user directory, this will soon become very messy # Once you've initalised git, you can start making changes in your repository and save them the same way you do above. So I will skip through the explantions. git status git add <folder_and_file_names> git status git commit -m "add meaningful commit messages" git status # Now, we want to push this onto GitHub again for collaboration. # But you might have noticed that we don't have a remote to push this to because everything we did was local. # So now, you have to go into your GitHub and create a new repo, once you've done that, you can get the SSH key of that repo. # Now you tell git the remote you're using: git remote add origin <SSH Key of the repo you just created> # After this step, you just push your changes to the GitHub repo the same way you did before as git now knows where your remote is: git push origin master
And these are the two ways you can work locally and then push changes onto GitHub.
One useful tool is
git log, this basically give you a log of all the commits. It will show you important information like:
- Who made the change
- When was the change made
- What is the change You can then use that to debug codes or roll back to previous versions of codes. I will talk more about how to do that in the future.
For now this is it :) Hope this is useful! This only covers the basics of the basics. There are so many other powerful things git can do like creating new branches, pulling new changes from GitHub and many more. I will cover those in later blogposts.
Top comments (0)