git ⚓︎
Resources
- Official documentation in Git docs.
- Atlassian's tutorials.
- Git for professionals by FreeCodeCamp
- Advanced Git by FreeCodeCamp
Most used commands⚓︎
git add FILENAME(S)
: it adds the files in "stage", i.e. preparing to commitgit commit -m "MESSAGE"
: it does the commit (i.e. basically saving) the staged changes in the local repositorygit push
: it pushes the last commit towards the remote repositorygit pull
: it updates your current HEAD branch with the latest diffs from remote
Used branches⚓︎
git branch
: it shows the list of current branches
Create a new branch:⚓︎
git checkout -b NEWBRANCH
: it moves towards the newly created branch called NEWBRANCH- Some best practices for naming the new branches here:
- Use grouping tokens (words) at the beginning of your branch names. For example:
Test/DESCRIPTION
: For test branchesNew/DESCRIPTION
: For new features branchesBug/DESCRIPTION
: For bugfixes branchesExp/DESCRIPTION
: Experimental: for throwaway branch, to be trashedVerified/DESCRIPTION
: For verified branches, to be merged to main
- Use grouping tokens (words) at the beginning of your branch names. For example:
git push --set-upstream origin NEWBRANCH
: it creates the remote branch from the local one, pushing to it as well; as epxlained here
Pull from a different remote branch ⚓︎
- Check the remote branches (
git branch -r
) and the local branches (git branch
) - Switch to a specified branch (
git switch BRANCH_NAME
)
Publish a local repo to a new remote repo⚓︎
Requirement: the remote repo must exist, otherwise it will return the error Repository not found
git remote add origin https://github.com/.../REPONAME.git
: (right after the firstcommit
) it defines the upstream origin. It will ask the credentials:username: [insert USERNAME] password: [insert PERSONAL ACCESS TOKEN]
git push -u origin master
: pusha il branch master sull'origin, da qui in poi basterà usaregit push
Delete an unused branch:⚓︎
git branch --delete|-d BRANCHNAME
: it deletes the local branch BRANCHNAMEgit push -d origin BRANCHNAME
: it deletes the remote branch BRANCHNAME
Move to an existing local branch:⚓︎
git checkout DESTINATIONBRANCH
: it moves the tracking to the local branch DESTINATIONBRANCH
Merge a branch⚓︎
Sample procedure:
git checkout -b NEW-FEATURE main
: Start a new feature by creating the branch NEW-FEATURE from MAINgit add <file>
: add a new file in the featuregit commit -m "Start a feature"
: commit the editgit checkout main
: move to maingit merge NEW-FEATURE
: merge the branch into maingit branch -d NEW-FEATURE
: delete the branchgit push
: sync with remote repo
Warning
If you get the not something we can merge
error, it's probably because you don't have a local copy of the branch that you want to merge, as explained here. Go on with:
git checkout BRANCH-NAME
git checkout main
git merge BRANCH-NAME
Switch remote URLs from HTTPS to SSH⚓︎
From your local project folder,
$ git remote -v
> origin https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin https://github.com/USERNAME/REPOSITORY.git (push)
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
$ git remote -v
# Verify new remote URL
> origin git@github.com:USERNAME/REPOSITORY.git (fetch)
> origin git@github.com:USERNAME/REPOSITORY.git (push)
.gitignore⚓︎
-
Compile your
\.gitignore
file to prevent git from tracking files. Sample file:*.exe # Ignore _all_ exe files .virtualenvironment/ # Ignore the folder .virtualenvironment and its whole content
-
Make git forget about a file that's been tracked since now:
git rm --cached <file>
- for the single filegit rm -r --cached <folder>
- for a whole folder and all files in it recursively The removal of the file from the head revision will happen on the next commit.Warning: While this will not remove the physical file from your local, it will remove the files from other developers machines on next git pull
Misc.⚓︎
-
Additional stuff:
git gui
: it opens the integrated git GUIgitk
: it opens the commit viewer di Git -
Just reinstall git:
sudo apt-get purge git sudo apt-get autoremove sudo apt-get install git