I recommend printing this post and/or having it handy on another device while working on Git on your PC.
Project Dev. Sequence
This is a useful and common command sequence used to pass changes to a Github/Git project:
git status
git diff
git add .
git commit -m "changes to all files"
git push
Short explanations to each of them:
git status
inspect / check the files that have been changed in the working directory
git diff [filename]
check the specific changes made on the selected, changed file. Useful for writing the commit message.
git add [filename]
stage changes to the selected file. This command adds the change to the staging area
git add .
stage changes to all changed files
git commit -m "[commit message]"
pass changes to the local repository (i.e. to the folder in your local computer)
git push
push these passed changes to the server (the Github page / internet)
Create a Repository
Locally
In computer:
- Make a folder, name it with your desired repository name, add stuff to it.
- Open terminal in that folder and type
git init
. - Keep the terminal open.
In Github:
- Click on "+" sign on top-right corner of / New Repository...
- Add info on that page and then click on "Create Repository" green button.
- Copy-paste manual guidelines on next page (starts with "echo README" I think) to open terminal from above.
-
git push
. You're done!
Easier way
In Github:
- Click on "+" sign on top-right corner of / New Repository...
- Add info on that page and then click on "Create Repository" green button.
- ... Ignore next page and go to the next option.
- While in new repository, click on green "Code" button and copy https/ssh address (I typically copy the ssh one).
In computer:
- Open terminal and type
git clone [copied-address-from-GitHub]
. You're done!
Branches
Basic commands
# create a new branch
git checkout -b [your-new-branch-name]
# check the names of all your branches
git branch
# go to a particular branch
git checkout [your-branch-name]
# transfer all information from a "source" branch to a "sink" branch
git checkout [sink-branch-name]
git merge [source-branch-name]
# delete branch locally (computer)
git branch -d [your-branch-name]
# delete branch remotely ("on GitHub")
git push origin --delete [your-branch-name]
Rename branch
# checkout to master branch
git checkout main
# local
git branch -m [old-branch-name] [new-branch-name]
# remote (Github)
git push origin [new-branch-name]
git push origin --delete [old-branch-name]
Add all content from a fork to a larger repository
To add all the content from your GitHub fork to the larger repository, you can follow these steps:
# Clone the larger library repository to your local machine using the git clone command
git clone https://github.com/larger-repo.git
# Add the fork as a remote to your local clone of the larger library repository using the git remote add command.
git remote add fork https://github.com/[your-username]/[forked-repo-name].git
# Fetch the changes from the fork and switch to a new local branch that will track the forked repository's changes
git fetch fork
git switch -c branch-fork fork/main
# Checkout to the main branch of the larger repo
git checkout main
# Merge the changes from the fork into your local clone of the larger library repository using the git merge command with the --allow-unrelated-histories flag
git merge --allow-unrelated-histories branch-fork
# Commit the changes
git commit -m "Merge contents from fork to larger repo"
# Push the changes to the main branch of the larger repository
git push origin main
Undoing Stuff
Revert modified files not yet staged for commit
git restore [filename]
Let's say you have a file with code that you had previously pushed and you made changes to the code; this command does an UNDO on those changes made.
Undo a commit
On your Github repository, Click on the commits counter below the Code button
Let's say I want to undo the first commit, then I would copy its hash (95f0e33) and run the following command:
git revert 95f0e33
Temporarily revert to previous commit
git checkout [commit_ID] #go to the former code commit / version
git checkout master #go back to current version
Remove files listed on .gitignore
which have been previously committed (i.e. "not gitignored")
git rm -r --cached .;
git add .;
git commit -m "gitignored files deleted from remote!"
Make Git Remember Your Username and Password
Watch this video around the 3:29 timestamp
Generating an SSH Key 3:29
Adapt SSH key to repositories
'''new repository'''
git clone git@github.com:[user]/[repository].git
'''or if already created repository'''
cat .git/config #displays local information of repository
git remote set-url origin git@github.com:[user]/[repository].git #this is the command which sets the key to your local repository.
Find all folders which are linked to Github repositories in a specific folder (Linux users)
While in folder to search, you may open a Terminal and type the following:
find -name ".git" #simple
'''or'''
find -name ".git" -print0 | sort -z | xargs -0 cat #in alphabetical order (may take long to compile IF several git repositories synced)
Top comments (2)
Informative
thank you Himanshu