Git Practices
Dec-2021
Late but not hate. Everything has its own time to come up. I just wanted to add some more practices on git in the blog, but I noticed that I never had started one post for Git. So that means this blog became a natural notebook, lives parallel to my experiences.
Installing Git in linux from original repository
Resource:
https://launchpad.net/~git-core/+archive/ubuntu/ppa
~> sudo add-apt-repository ppa:git-core/ppa
~> sudo apt update
Download a project from remote repo
~> git clone https://github.com/pulsar-edit/pulsar.git
Clone only the latest commit, less data:
~> git clone --depth=1 https://github.com/rails/rails.git
See what’s going on
~> git status
See who did what
~> git log
List the local branches
~> git branch
Get to the branch
~> git checkout <branch name>
Rename the branch
~> git branch -M <new branch name>
Put everything to stage list
~> git add .
Put files in unstaged list to stage list to commit later
~> git add file1 file2 file66
Put interactively, wicked!
Dude, it has details, forget it now!
~> git add -i
See what is staged
~> git diff --staged --name-only
Or…
~> git status -s
Unstage all
~> git reset
Saving the staged files in the local repo
~> git commit -m <"message">
Merging new changes to the main branch (Simple)
~> git merge <checked-out branch (main)> <branch name of the new features>
Merging new changes to the main branch (With a Commit)
~> git merge --squash <checked-out branch (main)> <branch name of the new features>
~> git commit -m "new features is merged to the main."
Stash the last changes (unstaged) to get back them later
~> git stash -m <"some_reason_for_stashing">
- “-m” not required, only for naming the stash
Pop out the last stash to keep working on
~> git stash pop
Listing saved stashes
~> git stash list
Deleting stashes
~> git stash clear
Squash commits
#KnowWhatIamDoin
Smashing commits into one by rebase command in interactive mode.
~> git rebase HEAD~10
That brings the last 10 commits to choose to squash by writing ‘squash’ instead of pick to beginning of the line for the ones, like from second to the most recent one. Then save the file and at the next window comment the commit comments to finalize the squash.
Putting the things to the remote repo
~> git push origin <branch name>
Check your remote setup
~> git remote -v
You don’t have a remote repo setup?
~> git remote add origin https://github.com/<user name>/<repo name>.git
Different source to Push only?
~> git remote set-url --push <origin/repo title> <repo url>
Can’t push to a repo which has a different history?
#IKnowWhatIamDoin
~> git pull origin main --allow-unrelated-histories && git push <etc...>
Get a copy of the branch of a repo to local folder means download the project
~> git clone <repository url>
See the history of a file
~> git log -p --follow -- <FILENAME>
- “-p” is for “show all patch information”
- “–” follow is to keep tracking even the filename has changed.
Config
User
Set user for the work folder or globally in the machine
~> git config --global user.name "<your name>"
~> git config --global user.email "<you email>"
Set user only for a specific repository
Cloning with your password withour being propmted
~> git clone https://<username>:<password>@github.com/<path/to/repo>"
~> git config user.name "<github user name>"
~> git config user.email "<github user email>"
That will save your credentials to the current repo, in the ./.git/config file
~> git config credential.helper store
Some Notes
- when you have tons of unstaged changes
- you wanna make another branch for a new feature
- which might not will be used in the main application,
- you need to stash the unstaged changes of your current branch.
When you do that, you will save last changes which are unstaged, check, but you can’t stash the unstaged-NEW-arrived-files, but that’s alright, they will always remain in the unstaged-changes lists even while switching between the branches.
Save
Bash script that sets git config up
#!/bin/bash
echo ""
echo "Setting up Git configuration for this folder,"
echo "Make sure you have initialized Git in here:"
echo -e "\e[34m$(pwd)\e[0m"
echo ""
read -r -p "Username: " _username;
read -r -p "Email : " _email;
echo ""
git config user.name $ $$_username; $
git config user.email $$_email; $
git config credentials.helper store;
echo ""
echo "Setup done: "
echo "./git/config file: ";
cat ./.git/config | grep "name";
cat ./.git/config | grep "email";
echo ""