Git
Handy aliases
Make an alias for add and commit (and make the message in vim)
git config --global alias.ac '!git add -A && git commit'
Reset your local repo to the remote and overwrite changes
git fetch origin
git reset --hard origin/master
Git stash with a name
git stash save "WIP: New feature or what not"
The command to list all branches in local and remote repositories is:
git branch -a
If you require only listing the remote branches from Git Bash then use this command:
git branch -r
You may also use the show-branch command for seeing the branches and their commits as follows:
git show-branch
Se changes at last commit
git whatchanged <option>…
Clean repository for sensitive data
Download BGF from the its homepage or install it (and Homebrew) First clone a fresh copy of your repo, using the — mirror flag:
$ git clone --mirror git://example.com/some-big-repo.git
Building replacements.txt file
PASSWORD1 #Replace string 'PASSWORD1' with '***REMOVED***' (default)
PASSWORD2==>examplePass # replace with 'examplePass' instead
PASSWORD3==> # replace with the empty string
Run the BFG programe
java -jar bfg --replace replacement.txt
Push the repository up again
$ git reflog expire --expire=now --all && git gc --prune=now -- aggressive
$ git push
Make a new branch
Git checkout -b “newer-branch”
and when you want to push it back
git push --set-upstream origin “newer-branch”
Deleting local branches in Git
git branch -d branchToDelete
Deleting remote branches in Git
git push origin --delete branchToDelete
Deleting both a local and a remote branch
Just a side note: please keep in mind that local and remote branches actually have nothing to do with each other. They are completely separate objects in Git.
Even if you’ve established a tracking connection (which you should for most scenarios), this still does not mean that deleting one would delete the other, too!
If you want any branch item to be deleted, you need to delete it explicitly.
After git 2.16 git outputs to VIM instead of the console
According to this link. Get back to normal like this:
git config --global pager.branch false # global setting
git --no-pager <your command> # for each command
Links
- Git’s home page where you can download Git and learn about it as well.
- Exelent book from Tower about learning GIT and there are some videos also
Renaming branches
If you want to rename a branch while pointing to any branch, do:
git branch -m <oldname> <newname>
If you want to rename the current branch, you can do:
git branch -m <newname>
If you want to push the local branch and reset the upstream branch:
git push origin -u <newname>
Or delete local branch
git branch -d <branch>
And finally if you want to Delete the remote branch:
git push origin --delete <oldname>
A way to remember this is -m is for “move” (or mv), which is also how you rename files. Adding an alias could also help. To do so, run the following:
git config --global alias.rename 'branch -m'
Reset local branch to be exact copy of remote
git fetch origin
git reset --hard origin/branch-name
What is the meaning of GIT info signs
| Symbol | meaning |— |— | * | unstaged | + | staged | $ | stash | % | untracked files | < | behind | > | ahead | <> | diverged | = | no difference | | | operation in progress | ? | sparse checkout
Git with oh-my-zsh
Very informative web page can be found here
Having more github accounts on your working station
If you are using both a company account and also would like to use you personal account, you can have both accounts by following these simple steps or by following the instructions here This is a short version Make a private/public key using ssh-keygen (feel freee to use any name)
ssh-keygen -t rsa -C "email@personal.com" -f "github-personal"
ssh-keygen -t rsa -C "email@company.com" -f "github-company"
Add the keys to the Agent
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/github-personal
ssh-add --apple-use-keychain ~/.ssh/github-company
Add the public keys to your github accounts (PS one at a time)
pbcopy < ~/.ssh/github-personal.pub
pbcopy < ~/.ssh/github-company.pub
Next, go to your GitHub account settings, navigate to “SSH and GPG keys,” click “New SSH Key,” add a title , paste the public key content into the “Key” field, and save. Repeat this step for all your GitHub accounts.
Configure SSH Alias
open ~/.ssh/config
# or if it does not exists
touch ~/.ssh/config
Add the aliases to the config file
Host github.com-personal
AddKeysToAgent yes
AddKeysToAgent yes
HostName github.com
User git
IdentityFile ~/.ssh/github-personal
Host github.com-company
AddKeysToAgent yes
AddKeysToAgent yes
HostName github.com
User git
IdentityFile ~/.ssh/github-company
Clone and Configure Repositories
git clone git@github.com-company:github-company/{repo-name}.git
git clone git@github.com-personal:github-personal/{repo-name}.git
If you have “old repo’s” you can get assign them to one of your accounts by going into the .git folder and open the config file and change the url to either git@github.com-company/….. or git@github.com-personal/….. Then you don’t have to “re-clone” the repo.
Thanks to Hesham Osama
Most Useful Git Commands from chat gpt
git init- Usage: Initializes a new Git repository and starts tracking an existing directory.
- Explanation: Adds a hidden subfolder for version control.
git clone [URL]- Usage: Clones a repository from an existing URL.
- Explanation: Copies a remote repository to your local machine.
git status- Usage: Shows the status of changes.
- Explanation: View changes in your working directory.
git add [file/directory]- Usage: Adds changes to the staging area.
- Explanation: Prepares changes for commit.
git commit -m "[Descriptive message]"- Usage: Captures a snapshot of the project’s changes.
- Explanation: Saves your changes to the repository.
git log- Usage: Shows the commit logs.
- Explanation: View your commit history.
git diff- Usage: Shows unstaged file differences.
- Explanation: See changes since last commit.
git diff --staged- Usage: Shows staged differences.
- Explanation: View changes you’ve staged.
git reset [file]- Usage: Unstages a file.
- Explanation: Remove a file from the staging area.
git checkout [branch-name]- Usage: Switches branches.
- Explanation: Move between different branches.
git branch- Usage: Lists all branches.
- Explanation: View all local branches.
git branch -a- Usage: Lists all branches, local and remote.
- Explanation: View all branches.
git branch [branch-name]- Usage: Creates a new branch.
- Explanation: Start a new line of development.
git push [remote-name] [branch-name]- Usage: Pushes changes to remote.
- Explanation: Updates the remote with your commits.
git pull [remote-name] [branch-name]- Usage: Fetches and merges remote changes.
- Explanation: Updates local codebase.
git merge [branch-name]- Usage: Merges another branch into the current branch.
- Explanation: Combine branch histories.
git remote- Usage: Lists remote repositories.
- Explanation: View linked remote repositories.
git remote add [name] [URL]- Usage: Adds a remote repository.
- Explanation: Link to another repository.
git fetch- Usage: Fetches changes from remote.
- Explanation: Get changes without merging.
git rm [file]- Usage: Deletes a file and stages the change.
- Explanation: Remove a file from your repository.